Spaces:
Runtime error
Runtime error
Commit
路
02ac619
1
Parent(s):
b228d02
Initial commit
Browse files
app.py
CHANGED
@@ -1,14 +1,48 @@
|
|
1 |
-
import os
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
from typing import List, Dict, Tuple
|
|
|
|
|
|
|
5 |
|
6 |
-
# Define the API URL
|
7 |
-
API_URL = "http://
|
8 |
|
9 |
History = List[Tuple[str, str]]
|
10 |
Messages = List[Dict[str, str]]
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
def clear_session() -> History:
|
13 |
return []
|
14 |
|
@@ -32,16 +66,28 @@ def model_chat(query: str, history: History) -> Tuple[str, History]:
|
|
32 |
messages = history_to_messages(history)
|
33 |
messages.append({'role': 'user', 'content': query.strip()})
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
response_json = response.json()
|
40 |
-
response_text = response_json["response"]
|
41 |
-
|
42 |
history.append((query.strip(), response_text.strip()))
|
43 |
return response_text.strip(), history
|
44 |
|
|
|
45 |
with gr.Blocks(css='''
|
46 |
.gr-group {direction: rtl;}
|
47 |
.chatbot{text-align:right;}
|
@@ -90,7 +136,7 @@ with gr.Blocks(css='''
|
|
90 |
</a>
|
91 |
<div class="dicta-intro-text">
|
92 |
<h1>爪'讗讟 诪注专讻讬 - 讛讚讙诪讛 专讗砖讜谞讬转</h1>
|
93 |
-
<span dir='rtl'>讘专讜讻讬诐 讛讘讗讬诐 诇讚诪讜 讛讗讬谞讟专讗拽讟讬讘讬 讛专讗砖讜谉. 讞拽专讜 讗转 讬讻讜诇讜转 讛诪讜讚诇 讜专讗讜 讻讬爪讚 讛讜讗 讬讻讜诇 诇住讬讬注
|
94 |
<span dir='rtl'>讛讚诪讜 谞讻转讘 注诇 讬讚讬 住专谉 专讜注讬 专转诐 转讜讱 砖讬诪讜砖 讘诪讜讚诇 砖驻讛 讚讬拽讟讛 砖驻讜转讞 注诇 讬讚讬 诪驻讗"转</span><br/>
|
95 |
</div>
|
96 |
</div>
|
@@ -101,7 +147,9 @@ with gr.Blocks(css='''
|
|
101 |
clear_btn = gr.Button("谞拽讛 砖讬讞讛")
|
102 |
|
103 |
def respond(query, history):
|
|
|
104 |
response, history = model_chat(query, history)
|
|
|
105 |
return history, gr.update(value="", interactive=True)
|
106 |
|
107 |
demo_state = gr.State([])
|
@@ -109,4 +157,4 @@ with gr.Blocks(css='''
|
|
109 |
query.submit(respond, [query, demo_state], [chatbot, query, demo_state])
|
110 |
clear_btn.click(clear_session, [], demo_state, chatbot)
|
111 |
|
112 |
-
demo.launch(
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
from typing import List, Dict, Tuple
|
4 |
+
from flask import Flask, request, jsonify
|
5 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
6 |
+
import threading
|
7 |
|
8 |
+
# Define the API URL to use the internal server
|
9 |
+
API_URL = "http://localhost:5000/chat"
|
10 |
|
11 |
History = List[Tuple[str, str]]
|
12 |
Messages = List[Dict[str, str]]
|
13 |
|
14 |
+
app = Flask(__name__)
|
15 |
+
|
16 |
+
# Load the model and tokenizer
|
17 |
+
model_name = "path/to/your/dictalm2.0"
|
18 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
19 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
20 |
+
|
21 |
+
@app.route('/chat', methods=['POST'])
|
22 |
+
def chat():
|
23 |
+
data = request.json
|
24 |
+
messages = data.get('messages', [])
|
25 |
+
|
26 |
+
if not messages:
|
27 |
+
return jsonify({"response": "No messages provided"}), 400
|
28 |
+
|
29 |
+
# Concatenate all user inputs into a single string
|
30 |
+
user_input = " ".join([msg['content'] for msg in messages if msg['role'] == 'user'])
|
31 |
+
|
32 |
+
inputs = tokenizer.encode(user_input, return_tensors='pt')
|
33 |
+
outputs = model.generate(inputs)
|
34 |
+
response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
35 |
+
|
36 |
+
return jsonify({"response": response_text})
|
37 |
+
|
38 |
+
# Function to run the Flask app
|
39 |
+
def run_flask():
|
40 |
+
app.run(host='0.0.0.0', port=5000)
|
41 |
+
|
42 |
+
# Start the Flask app in a separate thread
|
43 |
+
threading.Thread(target=run_flask).start()
|
44 |
+
|
45 |
+
# Gradio interface functions
|
46 |
def clear_session() -> History:
|
47 |
return []
|
48 |
|
|
|
66 |
messages = history_to_messages(history)
|
67 |
messages.append({'role': 'user', 'content': query.strip()})
|
68 |
|
69 |
+
try:
|
70 |
+
response = requests.post(API_URL, json={"messages": messages})
|
71 |
+
response.raise_for_status() # This will raise an HTTPError if the HTTP request returned an unsuccessful status code
|
72 |
+
response_json = response.json()
|
73 |
+
response_text = response_json.get("response", "Error: Response format is incorrect")
|
74 |
+
except requests.exceptions.HTTPError as e:
|
75 |
+
response_text = f"HTTPError: {str(e)}"
|
76 |
+
print(f"HTTPError: {e.response.text}") # Detailed error message
|
77 |
+
except requests.exceptions.RequestException as e:
|
78 |
+
response_text = f"RequestException: {str(e)}"
|
79 |
+
print(f"RequestException: {e}") # Debug print statement
|
80 |
+
except ValueError as e:
|
81 |
+
response_text = "ValueError: Invalid JSON response"
|
82 |
+
print(f"ValueError: {e}") # Debug print statement
|
83 |
+
except Exception as e:
|
84 |
+
response_text = f"Exception: {str(e)}"
|
85 |
+
print(f"General Exception: {e}") # Debug print statement
|
86 |
|
|
|
|
|
|
|
87 |
history.append((query.strip(), response_text.strip()))
|
88 |
return response_text.strip(), history
|
89 |
|
90 |
+
# Gradio interface setup
|
91 |
with gr.Blocks(css='''
|
92 |
.gr-group {direction: rtl;}
|
93 |
.chatbot{text-align:right;}
|
|
|
136 |
</a>
|
137 |
<div class="dicta-intro-text">
|
138 |
<h1>爪'讗讟 诪注专讻讬 - 讛讚讙诪讛 专讗砖讜谞讬转</h1>
|
139 |
+
<span dir='rtl'>讘专讜讻讬诐 讛讘讗讬诐 诇讚诪讜 讛讗讬谞讟专讗拽讟讬讘讬 讛专讗砖讜谉. 讞拽专讜 讗转 讬讻讜诇讜转 讛诪讜讚诇 讜专讗讜 讻讬爪讚 讛讜讗 讬讻讜诇 诇住讬讬注 诇讻诐 讘诪砖讬诪讜转讬讻诐</span><br/>
|
140 |
<span dir='rtl'>讛讚诪讜 谞讻转讘 注诇 讬讚讬 住专谉 专讜注讬 专转诐 转讜讱 砖讬诪讜砖 讘诪讜讚诇 砖驻讛 讚讬拽讟讛 砖驻讜转讞 注诇 讬讚讬 诪驻讗"转</span><br/>
|
141 |
</div>
|
142 |
</div>
|
|
|
147 |
clear_btn = gr.Button("谞拽讛 砖讬讞讛")
|
148 |
|
149 |
def respond(query, history):
|
150 |
+
print(f"Query: {query}") # Debug print statement
|
151 |
response, history = model_chat(query, history)
|
152 |
+
print(f"Response: {response}") # Debug print statement
|
153 |
return history, gr.update(value="", interactive=True)
|
154 |
|
155 |
demo_state = gr.State([])
|
|
|
157 |
query.submit(respond, [query, demo_state], [chatbot, query, demo_state])
|
158 |
clear_btn.click(clear_session, [], demo_state, chatbot)
|
159 |
|
160 |
+
demo.launch()
|