amkj84 commited on
Commit
bc2c65e
·
verified ·
1 Parent(s): 26e4452

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -15
app.py CHANGED
@@ -3,29 +3,55 @@ import gradio as gr
3
  from groq import Groq
4
 
5
  # Set up the Groq API client
6
- apikey = os.getenv("apikey") # Ensure your API key is set as a secret
7
  if not apikey:
8
  raise ValueError("API Key is not set. Add it in the Secrets tab.")
9
  client = Groq(api_key=apikey)
10
 
11
  # Function to interact with the LLM
12
- def chatbot(user_input):
13
  try:
 
 
 
 
 
14
  chat_completion = client.chat.completions.create(
15
- messages=[{"role": "user", "content": user_input}],
16
- model="llama3-8b-8192",
17
  )
18
- return chat_completion.choices[0].message.content
 
 
19
  except Exception as e:
20
- return f"An error occurred: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
- # Create the Gradio interface
23
- iface = gr.Interface(
24
- fn=chatbot,
25
- inputs=gr.Textbox(label="Your Message", placeholder="Type your message here..."),
26
- outputs=gr.Textbox(label="Response"),
27
- title="Groq LLM Chatbot",
28
- description="Interact with a powerful LLM via Groq's API."
29
- )
30
 
31
- iface.launch()
 
3
  from groq import Groq
4
 
5
  # Set up the Groq API client
6
+ apikey = os.getenv("apikey") # Fetch the API key from secrets
7
  if not apikey:
8
  raise ValueError("API Key is not set. Add it in the Secrets tab.")
9
  client = Groq(api_key=apikey)
10
 
11
  # Function to interact with the LLM
12
+ def chatbot(user_input, history):
13
  try:
14
+ # Format conversation history
15
+ messages = [{"role": "user", "content": user} for user, _ in history]
16
+ messages.append({"role": "user", "content": user_input})
17
+
18
+ # Send to Groq's API
19
  chat_completion = client.chat.completions.create(
20
+ messages=messages, model="llama3-8b-8192"
 
21
  )
22
+ response = chat_completion.choices[0].message.content
23
+ history.append((user_input, response))
24
+ return history, history
25
  except Exception as e:
26
+ return history, history + [(user_input, f"An error occurred: {str(e)}")]
27
+
28
+ # Enhanced Gradio interface
29
+ with gr.Blocks(title="Groq LLM Chatbot") as demo:
30
+ gr.Markdown(
31
+ """
32
+ # 🧠 **Groq LLM Chatbot**
33
+ Interact with a powerful LLM in real-time using Groq's API.
34
+ """
35
+ )
36
+ with gr.Row():
37
+ logo = gr.Image(value="https://via.placeholder.com/100x100.png?text=LOGO", shape=(100, 100))
38
+ gr.Markdown(
39
+ """
40
+ **Created by ATIF MEHMOOD**
41
+ _Experience the next generation of AI-powered conversations._
42
+ """
43
+ )
44
+ chatbot_ui = gr.Chatbot(label="Chat Interface")
45
+ user_input = gr.Textbox(
46
+ placeholder="Type your message here...",
47
+ label="Your Message",
48
+ lines=2,
49
+ )
50
+ submit_btn = gr.Button(value="Send")
51
 
52
+ # Bind functions
53
+ history_state = gr.State([])
54
+ submit_btn.click(chatbot, [user_input, history_state], [chatbot_ui, history_state])
55
+ user_input.submit(chatbot, [user_input, history_state], [chatbot_ui, history_state])
 
 
 
 
56
 
57
+ demo.launch()