Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,32 @@
|
|
1 |
import gradio as gr
|
2 |
-
from huggingface_hub import InferenceClient
|
3 |
import os
|
4 |
from groq import Groq
|
5 |
-
import groq_gradio
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
api_key
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
src=groq_gradio.registry, # Tells Gradio to use our custom interface registry as the source
|
15 |
-
title='Groq-Gradio Integration', # The title shown at the top of our UI
|
16 |
-
description="Chat with the Llama 3.3 70B model powered by Groq.", # Subtitle
|
17 |
-
examples=["Explain quantum gravity to a 5-year old.", "How many R are there in the word Strawberry?"] # Pre-written prompts users can click to try
|
18 |
-
).launch() # Creates and starts the web server!
|
19 |
|
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import os
|
3 |
from groq import Groq
|
|
|
4 |
|
5 |
+
def chat_with_groq(user_input, history):
|
6 |
+
api_key = os.getenv("GROQ_API_KEY") # Set this in your environment variables
|
7 |
+
if not api_key:
|
8 |
+
return "Error: API key not found. Set GROQ_API_KEY as an environment variable."
|
9 |
+
|
10 |
+
client = Groq(api_key=api_key)
|
11 |
+
|
12 |
+
# Format the history into Groq's expected format
|
13 |
+
messages = [{"role": "system", "content": "You are a helpful assistant."}]
|
14 |
+
for user_msg, bot_msg in history:
|
15 |
+
messages.append({"role": "user", "content": user_msg})
|
16 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
17 |
+
|
18 |
+
# Append the latest user input
|
19 |
+
messages.append({"role": "user", "content": user_input})
|
20 |
+
|
21 |
+
chat_completion = client.chat.completions.create(
|
22 |
+
messages=messages,
|
23 |
+
model="llama-3.3-70b-versatile",
|
24 |
+
)
|
25 |
+
|
26 |
+
response = chat_completion.choices[0].message.content
|
27 |
+
return response
|
28 |
|
29 |
+
# Gradio Interface
|
30 |
+
demo = gr.ChatInterface(chat_with_groq)
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
+
demo.launch()
|