Spaces:
Sleeping
Sleeping
hmmm
Browse files
app.py
CHANGED
@@ -1,11 +1,38 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
-
import
|
|
|
4 |
|
5 |
# Use a smaller model
|
6 |
client = InferenceClient("distilgpt2")
|
7 |
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
message,
|
10 |
history: list[tuple[str, str]],
|
11 |
system_message,
|
@@ -19,24 +46,11 @@ async def respond(
|
|
19 |
prompt += f"Human: {user_msg}\nAI: {bot_msg}\n"
|
20 |
prompt += f"Human: {message}\nAI:"
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
max_new_tokens=max_tokens,
|
28 |
-
temperature=temperature,
|
29 |
-
top_p=top_p,
|
30 |
-
do_sample=True,
|
31 |
-
),
|
32 |
-
timeout=10 # 10 seconds timeout
|
33 |
-
)
|
34 |
-
|
35 |
-
# Extract only the AI's response
|
36 |
-
ai_response = response.split("AI:")[-1].strip()
|
37 |
-
return ai_response
|
38 |
-
except asyncio.TimeoutError:
|
39 |
-
return "I'm sorry, but I'm having trouble generating a response right now. Could you try again?"
|
40 |
|
41 |
demo = gr.ChatInterface(
|
42 |
respond,
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
+
import threading
|
4 |
+
import time
|
5 |
|
6 |
# Use a smaller model
|
7 |
client = InferenceClient("distilgpt2")
|
8 |
|
9 |
+
def generate_with_timeout(prompt, max_new_tokens, temperature, top_p, timeout=10):
|
10 |
+
result = []
|
11 |
+
def target():
|
12 |
+
try:
|
13 |
+
response = client.text_generation(
|
14 |
+
prompt,
|
15 |
+
max_new_tokens=max_new_tokens,
|
16 |
+
temperature=temperature,
|
17 |
+
top_p=top_p,
|
18 |
+
do_sample=True,
|
19 |
+
)
|
20 |
+
result.append(response)
|
21 |
+
except Exception as e:
|
22 |
+
result.append(str(e))
|
23 |
+
|
24 |
+
thread = threading.Thread(target=target)
|
25 |
+
thread.start()
|
26 |
+
thread.join(timeout)
|
27 |
+
|
28 |
+
if thread.is_alive():
|
29 |
+
return "I'm sorry, but I'm having trouble generating a response right now. Could you try again?"
|
30 |
+
elif result:
|
31 |
+
return result[0]
|
32 |
+
else:
|
33 |
+
return "An error occurred while generating the response."
|
34 |
+
|
35 |
+
def respond(
|
36 |
message,
|
37 |
history: list[tuple[str, str]],
|
38 |
system_message,
|
|
|
46 |
prompt += f"Human: {user_msg}\nAI: {bot_msg}\n"
|
47 |
prompt += f"Human: {message}\nAI:"
|
48 |
|
49 |
+
response = generate_with_timeout(prompt, max_tokens, temperature, top_p)
|
50 |
+
|
51 |
+
# Extract only the AI's response
|
52 |
+
ai_response = response.split("AI:")[-1].strip()
|
53 |
+
return ai_response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
demo = gr.ChatInterface(
|
56 |
respond,
|