File size: 1,856 Bytes
ebe8242
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c32242
ebe8242
 
 
 
 
 
 
 
 
7df0a57
ebe8242
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16e1ce8
ebe8242
 
 
 
 
 
 
 
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
import os
import gradio as gr
from groq import Groq

# Read API key from Hugging Face Space secret
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")

if not GROQ_API_KEY:
    raise ValueError(
        "GROQ_API_KEY is not set. "
        "Go to your Space Settings -> Variables and secrets -> add it as a secret."
    )

# Initialize Groq client
client = Groq(api_key=GROQ_API_KEY)

# You can change this later if needed
DEFAULT_MODEL = os.environ.get("GROQ_MODEL", "llama-3.1-8b-instant")


def chat_with_groq(message, history):
    """
    Gradio passes:
      - message: latest user input (str)
      - history: list of [user, bot] pairs
    """
    try:
        system_prompt = "You are a helpful AI assistant for Sajid. Answer clearly and simply."

        messages = [{"role": "system", "content": system_prompt}]

        # Add previous messages
        for user_msg, bot_msg in history:
            messages.append({"role": "user", "content": user_msg})
            if bot_msg:
                messages.append({"role": "assistant", "content": bot_msg})

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

        response = client.chat.completions.create(
            model=DEFAULT_MODEL,
            messages=messages,
            max_tokens=512,
            temperature=0.7,
        )

        reply = response.choices[0].message.content
        return reply

    except Exception as e:
        # Show error inside the UI, useful if model name is wrong, etc.
        return f"⚠️ Error from Groq API: {e}"


demo = gr.ChatInterface(
    fn=chat_with_groq,
    title="Sajid's GenAI Chat (Groq + Hugging Face Space)",
    description=(
        "A simple LLM chat app using Groq's API, deployed on Hugging Face Spaces. "
        "Powered by Gradio."
    ),
)

if __name__ == "__main__":
    demo.launch()