import gradio as gr from groq import Groq class ConversationalAI: def __init__(self): self.client = Groq() self.system_prompt = { "role": "system", "content": "You are a useful assistant. You reply with efficient answers." } async def chat_groq(self, message, history): messages = [self.system_prompt] for msg in history: messages.append({"role": "user", "content": str(msg[0])}) messages.append({"role": "assistant", "content": str(msg[1])}) messages.append({"role": "user", "content": str(message)}) response_content = '' stream = self.client.chat.completions.create( model="llama3-70b-8192", messages=messages, max_tokens=1024, temperature=1.3, stream=True ) for chunk in stream: content = chunk.choices[0].delta.content if content: response_content += chunk.choices[0].delta.content yield response_content if __name__ == "__main__": ai = ConversationalAI() interface = gr.Interface(ai.chat_groq, inputs=gr.Textbox(label="Message", placeholder="Type your message here..."), outputs=gr.Textbox(label="Response", placeholder="Response will appear here..."), theme="compact") interface.launch()