Spaces:
Runtime error
Runtime error
| import os | |
| import gradio as gr | |
| from groq import Groq | |
| client = Groq(api_key=os.environ["Groq_api"]) | |
| def chatbot(user_input, history): | |
| if history is None: | |
| history = [] | |
| # ✅ history is already in correct format now | |
| messages = history.copy() | |
| # add current user message | |
| messages.append({ | |
| "role": "user", | |
| "content": user_input | |
| }) | |
| try: | |
| response = client.chat.completions.create( | |
| model="llama3-8b-8192", | |
| messages=messages | |
| ) | |
| bot_reply = response.choices[0].message.content | |
| except Exception as e: | |
| bot_reply = f"Error: {str(e)}" | |
| # add assistant reply | |
| messages.append({ | |
| "role": "assistant", | |
| "content": bot_reply | |
| }) | |
| return messages, messages | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## 🤖 Simple Groq Chatbot") | |
| # ✅ IMPORTANT CHANGE HERE | |
| chatbot_ui = gr.Chatbot() | |
| msg = gr.Textbox(placeholder="Type your message...") | |
| state = gr.State([]) | |
| clear = gr.Button("Clear") | |
| msg.submit(chatbot, [msg, state], [chatbot_ui, state]) | |
| clear.click(lambda: ([], []), None, [chatbot_ui, state], queue=False) | |
| demo.launch() |