Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from groq import Groq
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 6 |
+
|
| 7 |
+
def chat(message, history):
|
| 8 |
+
messages = [{"role": "system", "content": "You are a helpful assistant."}]
|
| 9 |
+
|
| 10 |
+
for user_msg, assistant_msg in history:
|
| 11 |
+
messages.append({"role": "user", "content": user_msg})
|
| 12 |
+
if assistant_msg:
|
| 13 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 14 |
+
|
| 15 |
+
messages.append({"role": "user", "content": message})
|
| 16 |
+
|
| 17 |
+
# Stream response
|
| 18 |
+
stream = client.chat.completions.create(
|
| 19 |
+
model="llama-3.3-70b-versatile",
|
| 20 |
+
messages=messages,
|
| 21 |
+
temperature=0.7,
|
| 22 |
+
stream=True,
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
partial = ""
|
| 26 |
+
for chunk in stream:
|
| 27 |
+
if chunk.choices[0].delta.content:
|
| 28 |
+
partial += chunk.choices[0].delta.content
|
| 29 |
+
yield partial
|
| 30 |
+
|
| 31 |
+
demo = gr.ChatInterface(
|
| 32 |
+
fn=chat,
|
| 33 |
+
title="Groq Chat Demo (Streaming)",
|
| 34 |
+
description="Streaming responses from Llama 3.3 70B on Groq",
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
demo.launch()
|