Ahmad-01 commited on
Commit
6834052
·
verified ·
1 Parent(s): 44fb3d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -4
app.py CHANGED
@@ -2,21 +2,28 @@ import os
2
  import gradio as gr
3
  from groq import Groq
4
 
5
- # Load API key from Hugging Face secrets
 
 
6
  GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
7
  if not GROQ_API_KEY:
8
  raise ValueError("GROQ_API_KEY not found. Add it in Space Settings → Variables.")
9
 
10
  client = Groq(api_key=GROQ_API_KEY)
11
 
 
12
  # Chat function
 
13
  def chat_with_groq(message, history):
 
14
  messages = []
15
  for user_msg, bot_msg in history:
16
  if user_msg:
17
  messages.append({"role": "user", "content": str(user_msg)})
18
  if bot_msg:
19
  messages.append({"role": "assistant", "content": str(bot_msg)})
 
 
20
  messages.append({"role": "user", "content": str(message)})
21
 
22
  try:
@@ -25,17 +32,21 @@ def chat_with_groq(message, history):
25
  messages=messages,
26
  )
27
  reply = response.choices[0].message.content
 
28
  history.append((message, reply))
29
  return "", history
30
  except Exception as e:
31
  history.append((message, f"Error: {str(e)}"))
32
  return "", history
33
 
34
- # CSS for Gen-Z look
 
 
35
  custom_css = """
36
  body {
37
  background: linear-gradient(135deg, #667eea, #764ba2, #ff6ec4);
38
  }
 
39
  #chatbox {
40
  backdrop-filter: blur(20px);
41
  background: rgba(255, 255, 255, 0.12);
@@ -43,6 +54,7 @@ body {
43
  padding: 15px;
44
  box-shadow: 0px 8px 32px rgba(0, 0, 0, 0.3);
45
  }
 
46
  button {
47
  background: linear-gradient(90deg, #ff6ec4, #7873f5);
48
  color: white;
@@ -50,19 +62,29 @@ button {
50
  border: none;
51
  font-weight: bold;
52
  }
 
53
  button:hover {
54
  opacity: 0.85;
55
  }
56
  """
57
 
58
- # Gradio UI for Gradio 6+
 
 
59
  with gr.Blocks(css=custom_css) as demo:
60
- gr.Markdown("<h1 style='text-align:center;color:white;'>🤖 Groq Gen-Z Chatbot</h1>")
 
 
 
 
61
  chatbot = gr.Chatbot(elem_id="chatbox", height=450)
62
  msg = gr.Textbox(placeholder="Type something cool...")
63
  clear = gr.Button("✨ Clear Chat")
64
 
 
65
  msg.submit(chat_with_groq, [msg, chatbot], [msg, chatbot])
 
 
66
  clear.click(lambda: ([], ""), None, [chatbot, msg], queue=False)
67
 
68
  demo.launch(theme=gr.themes.Soft(), css=custom_css)
 
2
  import gradio as gr
3
  from groq import Groq
4
 
5
+ # ---------------------------
6
+ # Load API Key from Hugging Face Secrets
7
+ # ---------------------------
8
  GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
9
  if not GROQ_API_KEY:
10
  raise ValueError("GROQ_API_KEY not found. Add it in Space Settings → Variables.")
11
 
12
  client = Groq(api_key=GROQ_API_KEY)
13
 
14
+ # ---------------------------
15
  # Chat function
16
+ # ---------------------------
17
  def chat_with_groq(message, history):
18
+ # Filter out None or empty messages for Groq
19
  messages = []
20
  for user_msg, bot_msg in history:
21
  if user_msg:
22
  messages.append({"role": "user", "content": str(user_msg)})
23
  if bot_msg:
24
  messages.append({"role": "assistant", "content": str(bot_msg)})
25
+
26
+ # Add current user message
27
  messages.append({"role": "user", "content": str(message)})
28
 
29
  try:
 
32
  messages=messages,
33
  )
34
  reply = response.choices[0].message.content
35
+ # Append to history as tuple
36
  history.append((message, reply))
37
  return "", history
38
  except Exception as e:
39
  history.append((message, f"Error: {str(e)}"))
40
  return "", history
41
 
42
+ # ---------------------------
43
+ # Gen-Z CSS
44
+ # ---------------------------
45
  custom_css = """
46
  body {
47
  background: linear-gradient(135deg, #667eea, #764ba2, #ff6ec4);
48
  }
49
+
50
  #chatbox {
51
  backdrop-filter: blur(20px);
52
  background: rgba(255, 255, 255, 0.12);
 
54
  padding: 15px;
55
  box-shadow: 0px 8px 32px rgba(0, 0, 0, 0.3);
56
  }
57
+
58
  button {
59
  background: linear-gradient(90deg, #ff6ec4, #7873f5);
60
  color: white;
 
62
  border: none;
63
  font-weight: bold;
64
  }
65
+
66
  button:hover {
67
  opacity: 0.85;
68
  }
69
  """
70
 
71
+ # ---------------------------
72
+ # Gradio UI
73
+ # ---------------------------
74
  with gr.Blocks(css=custom_css) as demo:
75
+ gr.Markdown(
76
+ "<h1 style='text-align:center;color:white;'>🤖 Groq Gen-Z Chatbot</h1>"
77
+ "<p style='text-align:center;color:white;'>Fast. Smart. Stylish. ⚡</p>"
78
+ )
79
+
80
  chatbot = gr.Chatbot(elem_id="chatbox", height=450)
81
  msg = gr.Textbox(placeholder="Type something cool...")
82
  clear = gr.Button("✨ Clear Chat")
83
 
84
+ # Submit user message
85
  msg.submit(chat_with_groq, [msg, chatbot], [msg, chatbot])
86
+
87
+ # Clear chat
88
  clear.click(lambda: ([], ""), None, [chatbot, msg], queue=False)
89
 
90
  demo.launch(theme=gr.themes.Soft(), css=custom_css)