File size: 1,258 Bytes
87cf4a1
 
 
 
 
75c8b47
87cf4a1
 
 
 
 
 
 
472a4a6
5b2d449
472a4a6
 
 
 
 
 
 
 
 
 
87cf4a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8172709
 
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
import os
import gradio as gr
from groq import Groq

# Fetch the key from Hugging Face Secrets
api_key = os.environ.get("Groq_Api")
client = Groq(api_key=api_key)

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

def chat_groq(current_message_content, history):
    try:
        system_prompt = 'You are a helpful ai assistant.'
        api_messages = [{"role": "system", "content": system_prompt}]
    
        # Updated loop for dictionary-style history
        for message in history:
            # message is now a dict: {"role": "user", "content": "..."}
            api_messages.append({
                "role": message['role'], 
                "content": message['content']
            })

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

        completion = client.chat.completions.create(
            model=MODEL_NAME,
            messages=api_messages,
            temperature=0.7,
        )
        return completion.choices[0].message.content
    except Exception as e:
        return f'Error: {e}'

ui = gr.ChatInterface(
    fn=chat_groq,
    title='Siyam GenAi App (Groq)',
    description='Simple Llama-3 chat app running on Groq',
)

# if __name__ == "__main__":
ui.launch()