Jyothikamalesh commited on
Commit
0cce7a0
·
verified ·
1 Parent(s): ab1fb5e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -7
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  from openai import OpenAI, APIError
3
  import os
4
  import tenacity
 
5
 
6
  ACCESS_TOKEN = os.getenv("HF_TOKEN")
7
 
@@ -11,7 +12,7 @@ client = OpenAI(
11
  )
12
 
13
  @tenacity.retry(wait=tenacity.wait_exponential(multiplier=1, min=4, max=10))
14
- def respond(
15
  message,
16
  history,
17
  system_message,
@@ -30,14 +31,18 @@ def respond(
30
 
31
  messages.append({"role": "user", "content": message})
32
 
33
- response = client.chat.completions.create(
 
34
  model="NousResearch/Hermes-3-Llama-3.1-8B",
35
  max_tokens=max_tokens,
 
36
  temperature=temperature,
37
  top_p=top_p,
38
  messages=messages,
39
- )
40
- return response.choices[0].delta.content
 
 
41
  except APIError as e:
42
  error_details = e.body
43
  error_type = error_details.get("type")
@@ -51,10 +56,10 @@ def respond(
51
  error_str = "An error occurred during streaming"
52
 
53
  print(f"Error: {error_str}")
54
- return error_str
55
  except Exception as e:
56
  print(f"Error: {e}")
57
- return "Error occurred. Please try again."
58
 
59
  def launch_app():
60
  try:
@@ -74,7 +79,7 @@ def launch_app():
74
  label="Top-P",
75
  ),
76
  ],
77
- "text",
78
  title="Chatbot",
79
  description="A chatbot that responds to user input",
80
  )
 
2
  from openai import OpenAI, APIError
3
  import os
4
  import tenacity
5
+ import asyncio
6
 
7
  ACCESS_TOKEN = os.getenv("HF_TOKEN")
8
 
 
12
  )
13
 
14
  @tenacity.retry(wait=tenacity.wait_exponential(multiplier=1, min=4, max=10))
15
+ async def respond(
16
  message,
17
  history,
18
  system_message,
 
31
 
32
  messages.append({"role": "user", "content": message})
33
 
34
+ response = ""
35
+ async for message in client.chat.completions.create(
36
  model="NousResearch/Hermes-3-Llama-3.1-8B",
37
  max_tokens=max_tokens,
38
+ stream=True,
39
  temperature=temperature,
40
  top_p=top_p,
41
  messages=messages,
42
+ ):
43
+ token = message.choices[0].text
44
+ response += token
45
+ yield response
46
  except APIError as e:
47
  error_details = e.body
48
  error_type = error_details.get("type")
 
56
  error_str = "An error occurred during streaming"
57
 
58
  print(f"Error: {error_str}")
59
+ yield error_str
60
  except Exception as e:
61
  print(f"Error: {e}")
62
+ yield "Error occurred. Please try again."
63
 
64
  def launch_app():
65
  try:
 
79
  label="Top-P",
80
  ),
81
  ],
82
+ "queue",
83
  title="Chatbot",
84
  description="A chatbot that responds to user input",
85
  )