Ahmad-01 commited on
Commit
8c878ce
·
verified ·
1 Parent(s): cda735c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -42
app.py CHANGED
@@ -7,81 +7,101 @@ from groq import Groq
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 with proper validation
16
  # ---------------------------
17
  def chat_with_groq(message, history):
18
- # 1. Ignore empty messages
19
- if not message or not message.strip():
20
- return "", history # do nothing
21
-
22
- # 2. Build message list from history (skip None/empty entries)
23
- messages = []
24
- for user_msg, bot_msg in history:
25
- if user_msg:
26
- messages.append({"role": "user", "content": str(user_msg)})
27
- if bot_msg:
28
- messages.append({"role": "assistant", "content": str(bot_msg)})
29
 
30
- # 3. Add current user message (already non‑empty)
31
- messages.append({"role": "user", "content": message.strip()})
32
 
33
- try:
34
- # 4. Use a valid Groq model
35
- response = client.chat.completions.create(
36
- model="mixtral-8x7b-32768", # ✅ change to a valid model
37
  messages=messages,
 
38
  )
39
- reply = response.choices[0].message.content
40
- history.append((message, reply))
41
- return "", history
42
  except Exception as e:
43
- # 5. Show the full error for debugging
44
- error_msg = f"Error: {str(e)}"
45
- history.append((message, error_msg))
46
- return "", history
47
 
48
  # ---------------------------
49
- # (Your existing CSS and UI remain the same)
50
  # ---------------------------
51
  custom_css = """
52
  body {
53
  background: linear-gradient(135deg, #667eea, #764ba2, #ff6ec4);
 
 
 
 
54
  }
55
  #chatbox {
56
  backdrop-filter: blur(20px);
57
- background: rgba(255, 255, 255, 0.12);
58
  border-radius: 20px;
59
  padding: 15px;
60
  box-shadow: 0px 8px 32px rgba(0, 0, 0, 0.3);
61
  }
 
 
 
62
  button {
63
- background: linear-gradient(90deg, #ff6ec4, #7873f5);
64
- color: white;
65
- border-radius: 12px;
66
- border: none;
67
- font-weight: bold;
68
  }
69
  button:hover {
70
- opacity: 0.85;
71
  }
72
  """
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
- msg.submit(chat_with_groq, [msg, chatbot], [msg, chatbot])
85
- clear.click(lambda: ([], ""), None, [chatbot, msg], queue=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
86
 
87
- demo.launch(theme=gr.themes.Soft(), css=custom_css)
 
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 → Secrets.")
11
 
12
  client = Groq(api_key=GROQ_API_KEY)
13
+ print("Groq Client Ready 🚀") # This will appear in the HF Space logs
14
 
15
  # ---------------------------
16
+ # Core chat function (identical to Colab version)
17
  # ---------------------------
18
  def chat_with_groq(message, history):
19
+ try:
20
+ # Convert Gradio history into Groq message format
21
+ messages = []
22
+ for human, assistant in history:
23
+ messages.append({"role": "user", "content": human})
24
+ messages.append({"role": "assistant", "content": assistant})
25
+ messages.append({"role": "user", "content": message})
 
 
 
 
26
 
27
+ # 👇 Debug: print the exact message list (check logs!)
28
+ print("Sending messages to Groq:", messages)
29
 
30
+ # Call Groq API with the same model that works in Colab
31
+ chat_completion = client.chat.completions.create(
 
 
32
  messages=messages,
33
+ model="openai/gpt-oss-120b", # keep the working model
34
  )
35
+
36
+ reply = chat_completion.choices[0].message.content
37
+ return reply
38
  except Exception as e:
39
+ print("Groq API error:", str(e)) # 👈 also print error
40
+ return f"Error: {str(e)}"
 
 
41
 
42
  # ---------------------------
43
+ # Gradio UI (mirrors Colab pattern)
44
  # ---------------------------
45
  custom_css = """
46
  body {
47
  background: linear-gradient(135deg, #667eea, #764ba2, #ff6ec4);
48
+ font-family: 'Poppins', sans-serif;
49
+ }
50
+ .gradio-container {
51
+ background: transparent !important;
52
  }
53
  #chatbox {
54
  backdrop-filter: blur(20px);
55
+ background: rgba(255, 255, 255, 0.1);
56
  border-radius: 20px;
57
  padding: 15px;
58
  box-shadow: 0px 8px 32px rgba(0, 0, 0, 0.3);
59
  }
60
+ textarea {
61
+ border-radius: 15px !important;
62
+ }
63
  button {
64
+ background: linear-gradient(90deg, #ff6ec4, #7873f5) !important;
65
+ color: white !important;
66
+ border-radius: 12px !important;
67
+ font-weight: bold !important;
68
+ border: none !important;
69
  }
70
  button:hover {
71
+ opacity: 0.85 !important;
72
  }
73
  """
74
 
75
+ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
76
  gr.Markdown(
77
+ """
78
+ <h1 style='text-align: center; color: white;'>
79
+ 🤖 Groq Gen-Z Chatbot
80
+ </h1>
81
+ <p style='text-align: center; color: white; font-size: 18px;'>
82
+ Fast. Smart. Stylish. ⚡
83
+ </p>
84
+ """
85
  )
86
 
87
+ chatbot = gr.Chatbot(elem_id="chatbox", height=450, bubble_full_width=False)
88
+ msg = gr.Textbox(placeholder="Type something cool...", scale=8)
89
  clear = gr.Button("✨ Clear Chat")
90
 
91
+ # Two-step interaction exactly as in Colab
92
+ def user_message(message, history):
93
+ return "", history + [[message, None]]
94
+
95
+ def bot_response(history):
96
+ user_msg = history[-1][0]
97
+ reply = chat_with_groq(user_msg, history[:-1])
98
+ history[-1][1] = reply
99
+ return history
100
+
101
+ msg.submit(user_message, [msg, chatbot], [msg, chatbot]).then(
102
+ bot_response, chatbot, chatbot
103
+ )
104
+
105
+ clear.click(lambda: [], None, chatbot, queue=False)
106
 
107
+ demo.launch()