mfraz commited on
Commit
f02b085
·
verified ·
1 Parent(s): 72dd052

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -1
app.py CHANGED
@@ -11,4 +11,49 @@ def chat_with_groq(prompt, history):
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 in history:
15
+ messages.append({"role": "user", "content": user_msg})
16
+ messages.append({"role": "assistant", "content": bot_msg})
17
+
18
+ messages.append({"role": "user", "content": prompt}) # Add latest user query
19
+
20
+ payload = {"model": "llama3-8b-8192", "messages": messages} # Use Groq's LLaMA 3 model
21
+
22
+ try:
23
+ response = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=payload)
24
+ response_data = response.json()
25
+
26
+ if response.status_code == 200:
27
+ return response_data["choices"][0]["message"]["content"]
28
+ else:
29
+ return f"API Error: {response_data.get('error', {}).get('message', 'Unknown error')}"
30
+
31
+ except Exception as e:
32
+ return f"Exception Error: {str(e)}"
33
+
34
+ # Function to handle user queries
35
+ def chatbot(query, history):
36
+ if not query.strip():
37
+ return history # Return previous history if query is empty
38
+
39
+ response = chat_with_groq(query, history)
40
+
41
+ # Append user query and bot response as a tuple
42
+ history.append((query, response))
43
+
44
+ return history # Return updated history
45
+
46
+ # Gradio Interface
47
+ with gr.Blocks() as demo:
48
+ gr.HTML("<h1 style='text-align: center; color: #00FF00; font-family: monospace;'>☠️ FARAZ-CHATBOT ☠️</h1>")
49
+
50
+ chatbot_interface = gr.Chatbot()
51
+ user_input = gr.Textbox(placeholder="Ask me anything...", show_label=False)
52
+
53
+ history = gr.State([]) # Stores conversation history
54
+
55
+ # Automatically trigger response when Enter is pressed
56
+ user_input.submit(fn=chatbot, inputs=[user_input, history], outputs=[chatbot_interface, history])
57
+
58
+ # Launch the chatbot
59
+ demo.launch()