Elishaaa321 commited on
Commit
27f176a
·
verified ·
1 Parent(s): ab96ab2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -8
app.py CHANGED
@@ -1,9 +1,9 @@
1
  import os
2
  import gradio as gr
3
  from groq import Groq
4
- from google.colab import userdata
5
 
6
- client = Groq(api_key=userdata.get("Groq_api"))
 
7
 
8
  def chatbot(user_input, history):
9
  if history is None:
@@ -11,20 +11,19 @@ def chatbot(user_input, history):
11
 
12
  messages = []
13
 
14
- # add history
15
  for human, bot in history:
16
  messages.append({"role": "user", "content": human})
17
  messages.append({"role": "assistant", "content": bot})
18
 
19
- # current input
20
  messages.append({"role": "user", "content": user_input})
21
 
22
  try:
23
  response = client.chat.completions.create(
24
- model="meta-llama/llama-4-scout-17b-16e-instruct",
25
  messages=messages
26
  )
27
-
28
  bot_reply = response.choices[0].message.content
29
 
30
  except Exception as e:
@@ -36,12 +35,14 @@ def chatbot(user_input, history):
36
 
37
 
38
  with gr.Blocks() as demo:
 
 
39
  chatbot_ui = gr.Chatbot()
40
  msg = gr.Textbox(placeholder="Type your message...")
41
- state = gr.State([]) # ✅ important fix
42
  clear = gr.Button("Clear")
43
 
44
  msg.submit(chatbot, [msg, state], [chatbot_ui, state])
45
  clear.click(lambda: ([], []), None, [chatbot_ui, state], queue=False)
46
 
47
- demo.launch(debug=True)
 
1
  import os
2
  import gradio as gr
3
  from groq import Groq
 
4
 
5
+ # Get API key from Hugging Face Secrets
6
+ client = Groq(api_key=os.environ["Groq_api"])
7
 
8
  def chatbot(user_input, history):
9
  if history is None:
 
11
 
12
  messages = []
13
 
14
+ # Add previous chat history
15
  for human, bot in history:
16
  messages.append({"role": "user", "content": human})
17
  messages.append({"role": "assistant", "content": bot})
18
 
19
+ # Add current input
20
  messages.append({"role": "user", "content": user_input})
21
 
22
  try:
23
  response = client.chat.completions.create(
24
+ model="llama3-8b-8192", # ✅ safer + recommended
25
  messages=messages
26
  )
 
27
  bot_reply = response.choices[0].message.content
28
 
29
  except Exception as e:
 
35
 
36
 
37
  with gr.Blocks() as demo:
38
+ gr.Markdown("## 🤖 Simple Groq Chatbot")
39
+
40
  chatbot_ui = gr.Chatbot()
41
  msg = gr.Textbox(placeholder="Type your message...")
42
+ state = gr.State([])
43
  clear = gr.Button("Clear")
44
 
45
  msg.submit(chatbot, [msg, state], [chatbot_ui, state])
46
  clear.click(lambda: ([], []), None, [chatbot_ui, state], queue=False)
47
 
48
+ demo.launch()