import gradio as gr from groq import Groq import os client = Groq(api_key=os.environ.get("GROQ_API_KEY")) def chat(message, history): messages = [{"role": "system", "content": "You are a helpful assistant."}] for user_msg, assistant_msg in history: messages.append({"role": "user", "content": user_msg}) if assistant_msg: messages.append({"role": "assistant", "content": assistant_msg}) messages.append({"role": "user", "content": message}) # Stream response stream = client.chat.completions.create( model="llama-3.3-70b-versatile", messages=messages, temperature=0.7, stream=True, ) partial = "" for chunk in stream: if chunk.choices[0].delta.content: partial += chunk.choices[0].delta.content yield partial demo = gr.ChatInterface( fn=chat, title="Groq Chat Demo (Streaming)", description="Streaming responses from Llama 3.3 70B on Groq", ) demo.launch()