Spaces:
Runtime error
Runtime error
Download app.py from Siyamansari/ChatBot: direct link, hf CLI and curl.
- Browser
- Download file 1.26 kB
-
https://huggingface.co/spaces/Siyamansari/ChatBot/resolve/main/app.py
- Command line
-
hf download hf://spaces/Siyamansari/ChatBot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Siyamansari/ChatBot/resolve/main/app.py
1.26 kB
| 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() |