Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,14 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import openai
|
| 3 |
import requests
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
headers = {"Authorization": f"Bearer {OPENAI_API_KEY}", "Content-Type": "application/json"}
|
| 12 |
|
| 13 |
messages = [{"role": "system", "content": "You are Faraz-Chatbot, a helpful assistant."}]
|
| 14 |
|
| 15 |
-
# Convert history to
|
| 16 |
-
for user_msg, bot_msg
|
| 17 |
-
messages.append({"role": "user", "content": user_msg})
|
| 18 |
-
messages.append({"role": "assistant", "content": bot_msg})
|
| 19 |
-
|
| 20 |
-
messages.append({"role": "user", "content": prompt}) # Add latest user query
|
| 21 |
-
|
| 22 |
-
payload = {"model": "gpt-3.5-turbo", "messages": messages}
|
| 23 |
-
|
| 24 |
-
try:
|
| 25 |
-
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
|
| 26 |
-
response_data = response.json()
|
| 27 |
-
|
| 28 |
-
if response.status_code == 200:
|
| 29 |
-
return response_data["choices"][0]["message"]["content"]
|
| 30 |
-
else:
|
| 31 |
-
return f"API Error: {response_data.get('error', {}).get('message', 'Unknown error')}"
|
| 32 |
-
|
| 33 |
-
except Exception as e:
|
| 34 |
-
return f"Exception Error: {str(e)}"
|
| 35 |
-
|
| 36 |
-
# Function to handle user queries
|
| 37 |
-
def chatbot(query, history=[]):
|
| 38 |
-
if not query.strip():
|
| 39 |
-
return history # Return previous history if query is empty
|
| 40 |
-
|
| 41 |
-
response = chat_with_openai(query, history)
|
| 42 |
-
|
| 43 |
-
# Append user query and bot response as a tuple
|
| 44 |
-
history.append((query, response))
|
| 45 |
-
|
| 46 |
-
return history # Return updated history
|
| 47 |
-
|
| 48 |
-
# Gradio Interface
|
| 49 |
-
with gr.Blocks() as demo:
|
| 50 |
-
gr.HTML("<h1 style='text-align: center; color: #00FF00; font-family: monospace;'>☠️ FARAZ-CHATBOT ☠️</h1>")
|
| 51 |
-
|
| 52 |
-
chatbot_interface = gr.Chatbot()
|
| 53 |
-
user_input = gr.Textbox(placeholder="Ask me anything...", show_label=False)
|
| 54 |
-
|
| 55 |
-
history = gr.State([]) # Stores conversation history
|
| 56 |
-
|
| 57 |
-
# Automatically trigger response when Enter is pressed
|
| 58 |
-
user_input.submit(fn=chatbot, inputs=[user_input, history], outputs=[chatbot_interface])
|
| 59 |
-
|
| 60 |
-
# Launch the chatbot
|
| 61 |
-
demo.launch()
|
| 62 |
-
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import requests
|
| 3 |
|
| 4 |
+
# Groq API Key (Replace with your actual key)
|
| 5 |
+
GROQ_API_KEY = "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"
|
| 6 |
|
| 7 |
+
# Function to interact with Groq's LLM
|
| 8 |
+
def chat_with_groq(prompt, history):
|
| 9 |
+
headers = {"Authorization": f"Bearer {GROQ_API_KEY}", "Content-Type": "application/json"}
|
|
|
|
| 10 |
|
| 11 |
messages = [{"role": "system", "content": "You are Faraz-Chatbot, a helpful assistant."}]
|
| 12 |
|
| 13 |
+
# Convert history to Groq format
|
| 14 |
+
for user_msg, bot_msg
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|