aneesqumar commited on
Commit
2f0ce6b
·
verified ·
1 Parent(s): b9084cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -14
app.py CHANGED
@@ -3,27 +3,27 @@ import gradio as gr
3
  from groq import Groq
4
 
5
  # -------------------------------------------------
6
- # Groq Client (reads from HF Secrets automatically)
7
  # -------------------------------------------------
8
  api_key = os.environ.get("GROQ_API_KEY")
9
  if not api_key:
10
  raise RuntimeError(
11
- "GROQ_API_KEY is not set. Please add it in Hugging Face Spaces → Settings → Secrets."
12
  )
13
 
14
  client = Groq(api_key=api_key)
15
 
16
  # -------------------------------------------------
17
- # Chat Function
18
  # -------------------------------------------------
19
  def chat(user_input, history):
20
  if not user_input or not user_input.strip():
21
  return history, ""
22
 
23
  messages = []
24
- for user_msg, bot_msg in history:
25
- messages.append({"role": "user", "content": user_msg})
26
- messages.append({"role": "assistant", "content": bot_msg})
27
 
28
  messages.append({"role": "user", "content": user_input})
29
 
@@ -34,13 +34,13 @@ def chat(user_input, history):
34
  )
35
  response = completion.choices[0].message.content
36
  except Exception as e:
37
- response = f"⚠️ Error communicating with model: {str(e)}"
38
 
39
  history.append((user_input, response))
40
  return history, ""
41
 
42
  # -------------------------------------------------
43
- # Theme & Styling (HCI-friendly)
44
  # -------------------------------------------------
45
  theme = gr.themes.Soft(
46
  primary_hue="blue",
@@ -58,9 +58,9 @@ footer {display: none;}
58
  """
59
 
60
  # -------------------------------------------------
61
- # Gradio UI
62
  # -------------------------------------------------
63
- with gr.Blocks(theme=theme, css=custom_css) as demo:
64
  with gr.Column(elem_id="container"):
65
  gr.Markdown(
66
  """
@@ -71,7 +71,6 @@ with gr.Blocks(theme=theme, css=custom_css) as demo:
71
 
72
  chatbot = gr.Chatbot(
73
  height=420,
74
- bubble_full_width=False,
75
  show_label=False
76
  )
77
 
@@ -86,11 +85,12 @@ with gr.Blocks(theme=theme, css=custom_css) as demo:
86
  with gr.Row():
87
  clear_btn = gr.Button("Clear Chat", variant="secondary")
88
 
89
- # -----------------------------
90
  # Interactions
91
- # -----------------------------
92
  send_btn.click(chat, [user_input, chatbot], [chatbot, user_input])
93
  user_input.submit(chat, [user_input, chatbot], [chatbot, user_input])
94
  clear_btn.click(lambda: [], None, chatbot)
95
 
96
- demo.launch()
 
 
 
 
3
  from groq import Groq
4
 
5
  # -------------------------------------------------
6
+ # Groq Client (HF Secrets)
7
  # -------------------------------------------------
8
  api_key = os.environ.get("GROQ_API_KEY")
9
  if not api_key:
10
  raise RuntimeError(
11
+ "GROQ_API_KEY is not set. Add it in Hugging Face Spaces → Settings → Secrets."
12
  )
13
 
14
  client = Groq(api_key=api_key)
15
 
16
  # -------------------------------------------------
17
+ # Chat Logic
18
  # -------------------------------------------------
19
  def chat(user_input, history):
20
  if not user_input or not user_input.strip():
21
  return history, ""
22
 
23
  messages = []
24
+ for u, a in history:
25
+ messages.append({"role": "user", "content": u})
26
+ messages.append({"role": "assistant", "content": a})
27
 
28
  messages.append({"role": "user", "content": user_input})
29
 
 
34
  )
35
  response = completion.choices[0].message.content
36
  except Exception as e:
37
+ response = f"⚠️ Error: {str(e)}"
38
 
39
  history.append((user_input, response))
40
  return history, ""
41
 
42
  # -------------------------------------------------
43
+ # Theme & CSS (Gradio 6 compatible)
44
  # -------------------------------------------------
45
  theme = gr.themes.Soft(
46
  primary_hue="blue",
 
58
  """
59
 
60
  # -------------------------------------------------
61
+ # UI
62
  # -------------------------------------------------
63
+ with gr.Blocks() as demo:
64
  with gr.Column(elem_id="container"):
65
  gr.Markdown(
66
  """
 
71
 
72
  chatbot = gr.Chatbot(
73
  height=420,
 
74
  show_label=False
75
  )
76
 
 
85
  with gr.Row():
86
  clear_btn = gr.Button("Clear Chat", variant="secondary")
87
 
 
88
  # Interactions
 
89
  send_btn.click(chat, [user_input, chatbot], [chatbot, user_input])
90
  user_input.submit(chat, [user_input, chatbot], [chatbot, user_input])
91
  clear_btn.click(lambda: [], None, chatbot)
92
 
93
+ # -------------------------------------------------
94
+ # Launch (theme & css moved here for Gradio 6)
95
+ # -------------------------------------------------
96
+ demo.launch(theme=theme, css=custom_css)