samunder12 commited on
Commit
eda1383
·
verified ·
1 Parent(s): 64f07e3

hello again

Browse files
Files changed (1) hide show
  1. app.py +24 -17
app.py CHANGED
@@ -1,9 +1,11 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
3
 
4
- client = InferenceClient("facebook/opt-350m")
 
5
 
6
- def respond(
7
  message,
8
  history: list[tuple[str, str]],
9
  system_message,
@@ -17,30 +19,35 @@ def respond(
17
  prompt += f"Human: {user_msg}\nAI: {bot_msg}\n"
18
  prompt += f"Human: {message}\nAI:"
19
 
20
- # Generate response
21
- response = client.text_generation(
22
- prompt,
23
- max_new_tokens=max_tokens,
24
- temperature=temperature,
25
- top_p=top_p,
26
- do_sample=True,
27
- )
 
 
 
 
28
 
29
- # Extract only the AI's response
30
- ai_response = response.split("AI:")[-1].strip()
31
-
32
- return ai_response
 
33
 
34
  demo = gr.ChatInterface(
35
  respond,
36
  additional_inputs=[
37
  gr.Textbox(value="You are a helpful AI assistant.", label="System message"),
38
- gr.Slider(minimum=1, maximum=256, value=128, step=1, label="Max new tokens"),
39
- gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"),
40
  gr.Slider(
41
  minimum=0.1,
42
  maximum=1.0,
43
- value=0.95,
44
  step=0.05,
45
  label="Top-p (nucleus sampling)",
46
  ),
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
+ import asyncio
4
 
5
+ # Use a smaller model
6
+ client = InferenceClient("distilgpt2")
7
 
8
+ async def respond(
9
  message,
10
  history: list[tuple[str, str]],
11
  system_message,
 
19
  prompt += f"Human: {user_msg}\nAI: {bot_msg}\n"
20
  prompt += f"Human: {message}\nAI:"
21
 
22
+ try:
23
+ # Generate response with a timeout
24
+ response = await asyncio.wait_for(
25
+ client.text_generation(
26
+ prompt,
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,
43
  additional_inputs=[
44
  gr.Textbox(value="You are a helpful AI assistant.", label="System message"),
45
+ gr.Slider(minimum=1, maximum=100, value=50, step=1, label="Max new tokens"),
46
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Temperature"),
47
  gr.Slider(
48
  minimum=0.1,
49
  maximum=1.0,
50
+ value=0.9,
51
  step=0.05,
52
  label="Top-p (nucleus sampling)",
53
  ),