Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,34 +2,36 @@ import gradio as gr
|
|
| 2 |
import openai
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
|
| 6 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 7 |
openai.api_base = "https://api.groq.com/openai/v1"
|
| 8 |
|
| 9 |
def get_groq_response(message):
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
|
| 22 |
-
def chatbot(
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
|
|
|
|
| 27 |
chat_interface = gr.Interface(
|
| 28 |
fn=chatbot,
|
| 29 |
-
inputs=
|
| 30 |
outputs=["chatbot", "state"],
|
| 31 |
live=False,
|
| 32 |
title="PYAAR_KA_SAATHI",
|
| 33 |
-
description="
|
| 34 |
)
|
| 35 |
-
|
|
|
|
|
|
| 2 |
import openai
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# Set up OpenAI API key and base URL
|
| 6 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 7 |
openai.api_base = "https://api.groq.com/openai/v1"
|
| 8 |
|
| 9 |
def get_groq_response(message):
|
| 10 |
+
try:
|
| 11 |
+
response = openai.ChatCompletion.create(
|
| 12 |
+
model="llama-3.1-70b-versatile",
|
| 13 |
+
messages=[
|
| 14 |
+
{"role": "system", "content": "You are a friendly and helpful assistant."},
|
| 15 |
+
{"role": "user", "content": message}
|
| 16 |
+
]
|
| 17 |
+
)
|
| 18 |
+
return response.choices[0].message["content"]
|
| 19 |
+
except Exception as e:
|
| 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))
|
| 25 |
+
return history, history
|
| 26 |
|
| 27 |
+
# Gradio Interface
|
| 28 |
chat_interface = gr.Interface(
|
| 29 |
fn=chatbot,
|
| 30 |
+
inputs=["text", "state"],
|
| 31 |
outputs=["chatbot", "state"],
|
| 32 |
live=False,
|
| 33 |
title="PYAAR_KA_SAATHI",
|
| 34 |
+
description="A friendly and conversational chatbot for students."
|
| 35 |
)
|
| 36 |
+
|
| 37 |
+
chat_interface.launch()
|