Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import gradio as gr
|
|
| 3 |
import datetime as dt
|
| 4 |
import pytz
|
| 5 |
from groq import Groq
|
|
|
|
| 6 |
|
| 7 |
# Get the API key from an environment variable
|
| 8 |
groq_api_key = os.getenv("GROQ_API_KEY")
|
|
@@ -33,24 +34,32 @@ def predict(message,history):
|
|
| 33 |
history_list.append({"role": "assistant", "content": ai})
|
| 34 |
history_list.append({"role": "user", "content": message})
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
demo = gr.ChatInterface(
|
| 56 |
predict,
|
|
|
|
| 3 |
import datetime as dt
|
| 4 |
import pytz
|
| 5 |
from groq import Groq
|
| 6 |
+
import logging
|
| 7 |
|
| 8 |
# Get the API key from an environment variable
|
| 9 |
groq_api_key = os.getenv("GROQ_API_KEY")
|
|
|
|
| 34 |
history_list.append({"role": "assistant", "content": ai})
|
| 35 |
history_list.append({"role": "user", "content": message})
|
| 36 |
|
| 37 |
+
try:
|
| 38 |
+
response = client.chat.completions.create(
|
| 39 |
+
model="lama-3.1-70b-versatile", # Ensure the correct model name
|
| 40 |
+
messages=history_list,
|
| 41 |
+
temperature=1.0,
|
| 42 |
+
max_tokens=4000,
|
| 43 |
+
stream=True # Use streaming
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
partial_message = ""
|
| 47 |
+
chunk_count = 0
|
| 48 |
+
|
| 49 |
+
# Stream the response in chunks
|
| 50 |
+
for chunk in response:
|
| 51 |
+
chunk_content = chunk.choices[0].delta.content
|
| 52 |
+
if chunk_content:
|
| 53 |
+
chunk_count += 1
|
| 54 |
+
partial_message += chunk_content
|
| 55 |
+
yield partial_message # Send partial message to Gradio
|
| 56 |
+
|
| 57 |
+
pprint(f"[tokens = {chunk_count}] {message}")
|
| 58 |
+
|
| 59 |
+
except Exception as e:
|
| 60 |
+
logging.error(f"API request failed: {e}")
|
| 61 |
+
yield "Error: Unable to connect to Groq API."
|
| 62 |
+
|
| 63 |
|
| 64 |
demo = gr.ChatInterface(
|
| 65 |
predict,
|