Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,30 @@
|
|
1 |
import gradio as gr
|
2 |
from groq import Groq
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
client = Groq(api_key=api_key)
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
messages.append({"role": "assistant", "content": assistant})
|
15 |
-
messages.append({"role": "user", "content": message})
|
16 |
|
17 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
# Create the chat completion
|
19 |
completion = client.chat.completions.create(
|
20 |
model="llama-3.2-90b-text-preview",
|
@@ -44,7 +54,8 @@ with gr.Blocks(theme="soft") as iface:
|
|
44 |
api_key_input = gr.Textbox(
|
45 |
label="Enter your Groq API Key",
|
46 |
placeholder="sk-...",
|
47 |
-
type="password"
|
|
|
48 |
)
|
49 |
|
50 |
chatbot = gr.ChatInterface(
|
|
|
1 |
import gradio as gr
|
2 |
from groq import Groq
|
3 |
+
import os
|
4 |
|
5 |
+
# Default API key for examples (replace with a dummy value or leave empty)
|
6 |
+
DEFAULT_API_KEY = os.environ.get("GROQ_API_KEY", "")
|
|
|
7 |
|
8 |
+
def chatbot(message, history, api_key):
|
9 |
+
# Use the provided API key, or fall back to the default for examples
|
10 |
+
api_key = api_key or DEFAULT_API_KEY
|
11 |
+
|
12 |
+
if not api_key:
|
13 |
+
return "Please enter a valid Groq API key to use the chatbot."
|
|
|
|
|
14 |
|
15 |
try:
|
16 |
+
# Initialize Groq client with the API key
|
17 |
+
client = Groq(api_key=api_key)
|
18 |
+
|
19 |
+
# Prepare the messages including the conversation history
|
20 |
+
messages = [
|
21 |
+
{"role": "system", "content": "You are a helpful assistant."}
|
22 |
+
]
|
23 |
+
for human, assistant in history:
|
24 |
+
messages.append({"role": "user", "content": human})
|
25 |
+
messages.append({"role": "assistant", "content": assistant})
|
26 |
+
messages.append({"role": "user", "content": message})
|
27 |
+
|
28 |
# Create the chat completion
|
29 |
completion = client.chat.completions.create(
|
30 |
model="llama-3.2-90b-text-preview",
|
|
|
54 |
api_key_input = gr.Textbox(
|
55 |
label="Enter your Groq API Key",
|
56 |
placeholder="sk-...",
|
57 |
+
type="password",
|
58 |
+
value=DEFAULT_API_KEY # Pre-fill with default key if available
|
59 |
)
|
60 |
|
61 |
chatbot = gr.ChatInterface(
|