File size: 1,359 Bytes
4f7a7d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import gradio as gr
from groq import Groq
import os

# Replace with your Groq API Key (or use HF Secrets on Spaces)
GROQ_API_KEY = os.getenv("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")
client = Groq(api_key=GROQ_API_KEY)
MODEL = "llama3-8b-8192"

chat_history = []

def chat(user_input, history):
    chat_history.append({"role": "user", "content": user_input})
    response = client.chat.completions.create(
        model=MODEL,
        messages=chat_history,
        temperature=0.7
    )
    reply = response.choices[0].message.content
    chat_history.append({"role": "assistant", "content": reply})
    history.append((user_input, reply))
    return "", history

def clear_chat():
    global chat_history
    chat_history = []
    return [], ""

with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown("<h1 style='text-align:center;'>🤖 AI Chatbot with Groq + LLaMA 3</h1>")
    
    chatbot = gr.Chatbot(label="Chat", bubble_full_width=False)
    msg = gr.Textbox(placeholder="Ask me anything...", show_label=False)
    
    with gr.Row():
        send_btn = gr.Button("Send 🚀", variant="primary")
        clear_btn = gr.Button("Clear 🧹")

    send_btn.click(chat, [msg, chatbot], [msg, chatbot])
    msg.submit(chat, [msg, chatbot], [msg, chatbot])
    clear_btn.click(clear_chat, outputs=[chatbot, msg])

demo.launch()