msamiathar commited on
Commit
4f7a7d2
·
verified ·
1 Parent(s): 5e9dc94

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from groq import Groq
3
+ import os
4
+
5
+ # Replace with your Groq API Key (or use HF Secrets on Spaces)
6
+ GROQ_API_KEY = os.getenv("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")
7
+ client = Groq(api_key=GROQ_API_KEY)
8
+ MODEL = "llama3-8b-8192"
9
+
10
+ chat_history = []
11
+
12
+ def chat(user_input, history):
13
+ chat_history.append({"role": "user", "content": user_input})
14
+ response = client.chat.completions.create(
15
+ model=MODEL,
16
+ messages=chat_history,
17
+ temperature=0.7
18
+ )
19
+ reply = response.choices[0].message.content
20
+ chat_history.append({"role": "assistant", "content": reply})
21
+ history.append((user_input, reply))
22
+ return "", history
23
+
24
+ def clear_chat():
25
+ global chat_history
26
+ chat_history = []
27
+ return [], ""
28
+
29
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
30
+ gr.Markdown("<h1 style='text-align:center;'>🤖 AI Chatbot with Groq + LLaMA 3</h1>")
31
+
32
+ chatbot = gr.Chatbot(label="Chat", bubble_full_width=False)
33
+ msg = gr.Textbox(placeholder="Ask me anything...", show_label=False)
34
+
35
+ with gr.Row():
36
+ send_btn = gr.Button("Send 🚀", variant="primary")
37
+ clear_btn = gr.Button("Clear 🧹")
38
+
39
+ send_btn.click(chat, [msg, chatbot], [msg, chatbot])
40
+ msg.submit(chat, [msg, chatbot], [msg, chatbot])
41
+ clear_btn.click(clear_chat, outputs=[chatbot, msg])
42
+
43
+ demo.launch()