akhaliq HF staff commited on
Commit
8ec4444
1 Parent(s): efa449e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -12
app.py CHANGED
@@ -1,20 +1,30 @@
1
  import gradio as gr
2
  from groq import Groq
 
3
 
4
- def chatbot(message, history, api_key):
5
- # Initialize Groq client with the provided API key
6
- client = Groq(api_key=api_key)
7
 
8
- # Prepare the messages including the conversation history
9
- messages = [
10
- {"role": "system", "content": "You are a helpful assistant."}
11
- ]
12
- for human, assistant in history:
13
- messages.append({"role": "user", "content": human})
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(