File size: 2,604 Bytes
5b7f918
 
 
 
d961861
2f0ce6b
d961861
5b7f918
 
 
2f0ce6b
5b7f918
 
 
 
d961861
 
 
5b7f918
 
 
 
d961861
 
 
 
 
5b7f918
 
 
 
 
d961861
5b7f918
fcd7ffe
5b7f918
fcd7ffe
 
d961861
fcd7ffe
5b7f918
fcd7ffe
5b7f918
d961861
fcd7ffe
d961861
5b7f918
 
 
 
d961861
5b7f918
 
 
 
 
 
 
 
 
 
d961861
2f0ce6b
d961861
2f0ce6b
5b7f918
 
 
 
fcd7ffe
5b7f918
 
 
d961861
5b7f918
 
d961861
5b7f918
 
 
 
 
d961861
5b7f918
d961861
5b7f918
 
 
 
 
 
 
 
d961861
 
 
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import os
import gradio as gr
from groq import Groq

# -----------------------------
# Groq Client (HF Secrets)
# -----------------------------
api_key = os.environ.get("GROQ_API_KEY")
if not api_key:
    raise RuntimeError(
        "GROQ_API_KEY is not set. Add it in Hugging Face Spaces → Settings → Secrets."
    )

client = Groq(api_key=api_key)

# -----------------------------
# Chat Function
# -----------------------------
def chat(user_input, history):
    if not user_input or not user_input.strip():
        return history, ""

    # Keep only valid messages for Groq
    messages = [{"role": m["role"], "content": m["content"]}
                for m in history if "role" in m and "content" in m]

    # Add latest user message
    messages.append({"role": "user", "content": user_input})

    try:
        completion = client.chat.completions.create(
            model="llama-3.3-70b-versatile",
            messages=messages
        )
        assistant_reply = completion.choices[0].message.content
    except Exception as e:
        assistant_reply = f"⚠️ Error: {str(e)}"

    # Add assistant reply to messages
    messages.append({"role": "assistant", "content": assistant_reply})

    return messages, ""

# -----------------------------
# Theme & CSS
# -----------------------------
theme = gr.themes.Soft(
    primary_hue="blue",
    secondary_hue="gray",
    neutral_hue="gray",
    font=["Inter", "sans-serif"]
)

custom_css = """
#container {
    max-width: 900px;
    margin: auto;
}
footer {display: none;}
"""

# -----------------------------
# UI
# -----------------------------
with gr.Blocks() as demo:
    with gr.Column(elem_id="container"):
        gr.Markdown(
            """
            # 🤖 Groq AI Chatbot  
            **Fast • Responsive • HCI-Friendly Interface**
            """
        )

        # ✅ Just remove type="messages"
        chatbot = gr.Chatbot(
            height=420,
            show_label=False
        )

        with gr.Row():
            user_input = gr.Textbox(
                placeholder="Type your message here…",
                show_label=False
            )
            send_btn = gr.Button("Send", variant="primary")

        with gr.Row():
            clear_btn = gr.Button("Clear Chat", variant="secondary")

    send_btn.click(chat, [user_input, chatbot], [chatbot, user_input])
    user_input.submit(chat, [user_input, chatbot], [chatbot, user_input])
    clear_btn.click(lambda: [], None, chatbot)

# -----------------------------
# Launch
# -----------------------------
demo.launch(theme=theme, css=custom_css)