File size: 2,190 Bytes
3536717
 
1e9af20
3536717
 
 
 
 
 
 
 
 
 
 
1e9af20
 
 
3536717
 
 
 
1e9af20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3536717
1e9af20
 
3536717
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e9af20
3536717
 
1e9af20
 
 
3536717
 
 
 
 
 
 
 
 
 
 
 
 
1e9af20
3536717
1e9af20
 
3536717
1e9af20
 
 
 
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
98
import os

import spaces
import gradio as gr
from dotenv import load_dotenv
from groq import Groq


# Load environment variables
load_dotenv()

# Get Groq API key
api_key = os.getenv("GROQ_API_KEY")

if not api_key:
    raise ValueError("GROQ_API_KEY is not set.")

# Create Groq client
client = Groq(api_key=api_key)


# --------------------------------------------------
# ZeroGPU startup compatibility
# --------------------------------------------------

@spaces.GPU(duration=1)
def _zerogpu_startup_probe():
    """

    This function exists only to satisfy

    Hugging Face ZeroGPU's startup detector.



    It is never called.

    """
    return None


# --------------------------------------------------
# Chatbot function
# --------------------------------------------------

def chatbot(message, history):

    messages = [
        {
            "role": "system",
            "content": "You are a helpful and friendly AI assistant."
        }
    ]

    # Add previous conversation
    for item in history:
        messages.append({
            "role": item["role"],
            "content": item["content"]
        })

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

    # Call Groq
    response = client.chat.completions.create(
        model="openai/gpt-oss-20b",
        messages=messages,
        temperature=0.7,
        max_tokens=500
    )

    return response.choices[0].message.content


# --------------------------------------------------
# Gradio UI
# --------------------------------------------------

demo = gr.ChatInterface(
    fn=chatbot,
    title="🤖 Groq AI Chatbot",
    description="Simple chatbot powered by Groq",
    examples=[
        "What is artificial intelligence?",
        "Explain Python in simple words",
        "What is machine learning?"
    ]
)


# --------------------------------------------------
# Start application
# --------------------------------------------------

if __name__ == "__main__":
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860
    )