File size: 1,355 Bytes
a50704f
 
 
 
b07f5bd
efac976
a50704f
b07f5bd
 
 
 
 
 
 
 
e493800
b07f5bd
 
 
 
 
 
 
 
 
 
 
 
 
efac976
a50704f
b07f5bd
 
 
 
 
 
b8468ba
b07f5bd
 
a50704f
b07f5bd
 
a50704f
57f97a1
 
 
b0db5e7
0a5720a
 
 
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
import os
import gradio as gr
from groq import Groq

# Read API key from environment (Hugging Face Spaces secret)
api_key = os.environ.get("GROQ_API")

if not api_key:
    raise ValueError("No Groq API key found. Please add it as a secret.")

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

MODEL_NAME = "openai/gpt-oss-safeguard-20b"

def chat_groq(message, history):
    try:
        system_prompt = "You are a helpful AI assistant."
        messages = [{"role": "system", "content": system_prompt}]

        # Handle history safely
        for turn in history:
            if isinstance(turn, list) and len(turn) == 2:
                user_msg, bot_msg = turn
                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})

        completion = client.chat.completions.create(
            model=MODEL_NAME,
            messages=messages,
            max_tokens=1024,
            temperature=0.7
        )

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

    except Exception as e:
        return f"Error: {e}"


ui = gr.ChatInterface(
    fn=chat_groq,
    title="Affan's GenAI App(Groq)"
)

ui.launch()