Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,38 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import openai
|
| 3 |
import os
|
| 4 |
|
|
|
|
| 5 |
openai.api_key = os.getenv("GROQ_API_KEY")
|
| 6 |
openai.api_base = "https://api.groq.com/openai/v1"
|
| 7 |
|
| 8 |
def get_groq_response(message):
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
def chatbot(user_input
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
| 25 |
|
| 26 |
chat_interface = gr.Interface(
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
|
| 6 |
openai.api_key = os.getenv("GROQ_API_KEY")
|
| 7 |
openai.api_base = "https://api.groq.com/openai/v1"
|
| 8 |
|
| 9 |
def get_groq_response(message):
|
| 10 |
+
try:
|
| 11 |
+
|
| 12 |
+
response = openai.ChatCompletion.create(
|
| 13 |
+
model="llama-3.1-70b-versatile",
|
| 14 |
+
messages=[{"role": "user", "content": message}]
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
return response.choices[0].message['content']
|
| 18 |
+
except Exception as e:
|
| 19 |
+
# Return the error message if an exception occurs
|
| 20 |
+
return f"Error: {str(e)}"
|
| 21 |
+
|
| 22 |
+
def chatbot(user_input, history=[]):
|
| 23 |
+
bot_response = get_groq_response(user_input)
|
| 24 |
+
history.append((user_input, bot_response)) # Add user input and bot response to history
|
| 25 |
+
return history, history # Return updated history for state management
|
| 26 |
+
|
| 27 |
|
| 28 |
chat_interface = gr.Interface(
|
| 29 |
+
fn=chatbot,
|
| 30 |
+
inputs=["text", "state"],
|
| 31 |
+
outputs=["chatbot", "state"],
|
| 32 |
+
live=False,
|
| 33 |
+
title="My Chatbot",
|
| 34 |
+
description="Mom: We have ChatGPT at home,\nChatGPT at Home:"
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
chat_interface.launch()
|