hassan773 commited on
Commit
acfb71e
·
verified ·
1 Parent(s): 2569b9b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -62
app.py CHANGED
@@ -3,122 +3,121 @@ import gradio as gr
3
  from groq import Groq
4
 
5
  # 1. Setup API Client
 
6
  api_key = os.environ.get("GROQ_API_KEY")
7
  client = Groq(api_key=api_key)
8
 
9
- # 2. UI Styling for Sidebar Layout
10
  custom_css = """
11
- .gradio-container { max-width: 1200px; margin: auto; }
12
- #chatbot-window { height: 500px !important; border-radius: 12px; border: 1px solid #e0e0e0; }
13
- .sidebar-column {
14
- background-color: rgba(128, 128, 128, 0.05);
15
- padding: 20px !important;
16
- border-radius: 12px;
17
- min-height: 500px;
18
- }
19
- .linkedin-icon:hover { transform: scale(1.1); transition: transform 0.3s ease; }
20
- .sidebar-footer {
21
- margin-top: 30px;
22
- text-align: center;
23
- border-top: 1px solid #ddd;
24
- padding-top: 20px;
25
  }
26
  """
27
 
28
  # 3. Secure Chat Logic
29
  def chat_with_groq(message, history, model, temperature, system_prompt):
 
 
 
 
30
  cleaned_messages = [{"role": "system", "content": system_prompt}]
 
 
31
  for msg in history:
32
- cleaned_messages.append({"role": msg["role"], "content": msg["content"]})
 
 
 
 
 
33
  cleaned_messages.append({"role": "user", "content": message})
34
-
35
  try:
 
36
  response = client.chat.completions.create(
37
  model=model,
38
  messages=cleaned_messages,
39
  temperature=temperature,
40
  stream=True
41
  )
 
42
  partial_message = ""
43
  for chunk in response:
44
  if chunk.choices[0].delta.content:
45
  partial_message += chunk.choices[0].delta.content
46
  yield partial_message
 
47
  except Exception as e:
48
  yield f"⚠️ API Error: {str(e)}"
49
 
50
- # 4. Define Event Functions
51
- def user_turn(msg, hist):
52
- if not msg:
53
- return "", hist
54
- hist.append({"role": "user", "content": msg})
55
- return "", hist
56
-
57
- def bot_turn(hist, m, t, s):
58
- if not hist or hist[-1]["role"] != "user":
59
- return hist
60
- user_msg = hist[-1]["content"]
61
- gen = chat_with_groq(user_msg, hist[:-1], m, t, s)
62
- hist.append({"role": "assistant", "content": ""})
63
- for chunk in gen:
64
- hist[-1]["content"] = chunk
65
- yield hist
66
-
67
- # 5. Build the Interface
68
  with gr.Blocks() as demo:
69
  gr.Markdown("# ⚡ Groq Professional Studio")
70
-
 
71
  with gr.Row():
72
- # LEFT COLUMN: Main Chat Interface
73
  with gr.Column(scale=4):
74
- chatbot = gr.Chatbot(elem_id="chatbot-window", height=500)
75
  with gr.Row():
76
- msg_input = gr.Textbox(placeholder="Ask me anything...", container=False, scale=9)
 
 
 
 
77
  submit_btn = gr.Button("Send", variant="primary", scale=1)
78
 
79
- # RIGHT COLUMN: The Sidebar (All Settings & Support moved here)
80
- with gr.Column(scale=1, elem_classes="sidebar-column"):
81
  gr.Markdown("### ⚙️ Settings")
82
-
83
  model_choice = gr.Dropdown(
84
  choices=["llama-3.3-70b-versatile", "llama-3.1-8b-instant"],
85
  value="llama-3.3-70b-versatile",
86
  label="Select Model"
87
  )
88
-
89
  temp = gr.Slider(0.0, 1.0, 0.7, label="Temperature")
90
-
91
  sys_msg = gr.Textbox(
92
  value="You are a professional assistant.",
93
  label="System Prompt",
94
- lines=4
95
  )
96
-
97
- # Moved from below input bar to here
98
- clear = gr.Button("Clear Chat", variant="secondary")
99
 
100
- # LinkedIn Support correctly placed at the bottom of the sidebar
101
- gr.HTML("""
102
- <div class="sidebar-footer">
103
- <p style="font-size: 0.9em; margin-bottom: 12px; color: #555;"><b>Developer Support</b></p>
104
- <a href="https://www.linkedin.com/in/YOUR_LINKEDIN_USERNAME" target="_blank" style="text-decoration: none;">
105
- <svg class="linkedin-icon" xmlns="http://www.w3.org/2000/svg" width="45" height="45" viewBox="0 0 24 24" fill="#0077b5">
106
- <path d="M19 0h-14c-2.761 0-5 2.239-5 5v14c0 2.761 2.239 5 5 5h14c2.762 0 5-2.239 5-5v-14c0-2.761-2.239-5-5-5zm-11 19h-3v-11h3v11zm-1.5-12.268c-.966 0-1.75-.79-1.75-1.764s.784-1.764 1.75-1.764 1.75.79 1.75 1.764-.783 1.764-1.75 1.764zm13.5 12.268h-3v-5.604c0-3.368-4-3.113-4 0v5.604h-3v-11h3v1.765c1.396-2.586 7-2.777 7 2.476v6.759z"/>
107
- </svg>
108
- </a>
109
- <p style="font-size: 0.8em; color: #777; margin-top: 10px;">Hassan Naseer<br>NUTECH BSCS-24</p>
110
- </div>
111
- """)
 
 
 
112
 
113
- # --- Event Handling ---
114
  msg_input.submit(user_turn, [msg_input, chatbot], [msg_input, chatbot], queue=False).then(
115
  bot_turn, [chatbot, model_choice, temp, sys_msg], chatbot
116
  )
117
  submit_btn.click(user_turn, [msg_input, chatbot], [msg_input, chatbot], queue=False).then(
118
  bot_turn, [chatbot, model_choice, temp, sys_msg], chatbot
119
  )
 
 
120
  clear.click(lambda: [], None, chatbot, queue=False)
121
 
 
122
  if __name__ == "__main__":
123
  # Settings moved to launch() for Gradio 6 compatibility
124
- demo.launch(theme=gr.themes.Soft(primary_hue="blue"), css=custom_css)
 
 
 
 
3
  from groq import Groq
4
 
5
  # 1. Setup API Client
6
+ # Ensure GROQ_API_KEY is set in your Space Settings > Secrets
7
  api_key = os.environ.get("GROQ_API_KEY")
8
  client = Groq(api_key=api_key)
9
 
10
+ # 2. Responsive UI Styling
11
  custom_css = """
12
+ .gradio-container { max-height: 100vh !important; overflow: hidden !important; }
13
+ #chatbot-window {
14
+ height: 50vh !important;
15
+ border-radius: 12px;
16
+ border: 1px solid #e0e0e0;
17
+ background: white;
 
 
 
 
 
 
 
 
18
  }
19
  """
20
 
21
  # 3. Secure Chat Logic
22
  def chat_with_groq(message, history, model, temperature, system_prompt):
23
+ """
24
+ Cleans history to remove Gradio-specific metadata that causes 400 errors.
25
+ """
26
+ # Initialize with System Prompt
27
  cleaned_messages = [{"role": "system", "content": system_prompt}]
28
+
29
+ # Scrub history for clean role/content pairs
30
  for msg in history:
31
+ cleaned_messages.append({
32
+ "role": msg["role"],
33
+ "content": msg["content"]
34
+ })
35
+
36
+ # Add current user prompt
37
  cleaned_messages.append({"role": "user", "content": message})
38
+
39
  try:
40
+ # High-speed inference via Groq
41
  response = client.chat.completions.create(
42
  model=model,
43
  messages=cleaned_messages,
44
  temperature=temperature,
45
  stream=True
46
  )
47
+
48
  partial_message = ""
49
  for chunk in response:
50
  if chunk.choices[0].delta.content:
51
  partial_message += chunk.choices[0].delta.content
52
  yield partial_message
53
+
54
  except Exception as e:
55
  yield f"⚠️ API Error: {str(e)}"
56
 
57
+ # 4. Build the Interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  with gr.Blocks() as demo:
59
  gr.Markdown("# ⚡ Groq Professional Studio")
60
+ gr.Markdown("Everything is running smoothly on my end. Please let me know if you encounter any errors!")
61
+
62
  with gr.Row():
63
+ # Main Interaction Area
64
  with gr.Column(scale=4):
65
+ chatbot = gr.Chatbot(elem_id="chatbot-window")
66
  with gr.Row():
67
+ msg_input = gr.Textbox(
68
+ placeholder="Ask me anything...",
69
+ container=False,
70
+ scale=9
71
+ )
72
  submit_btn = gr.Button("Send", variant="primary", scale=1)
73
 
74
+ # Settings Sidebar
75
+ with gr.Column(scale=1):
76
  gr.Markdown("### ⚙️ Settings")
 
77
  model_choice = gr.Dropdown(
78
  choices=["llama-3.3-70b-versatile", "llama-3.1-8b-instant"],
79
  value="llama-3.3-70b-versatile",
80
  label="Select Model"
81
  )
 
82
  temp = gr.Slider(0.0, 1.0, 0.7, label="Temperature")
 
83
  sys_msg = gr.Textbox(
84
  value="You are a professional assistant.",
85
  label="System Prompt",
86
+ lines=3
87
  )
88
+ clear = gr.Button("Clear Chat", variant="stop")
 
 
89
 
90
+ # --- UI Event Flow ---
91
+ def user_turn(msg, hist):
92
+ # Update UI state with user message
93
+ hist.append({"role": "user", "content": msg})
94
+ return "", hist
95
+
96
+ def bot_turn(hist, m, t, s):
97
+ # Get response for the last user message
98
+ user_msg = hist[-1]["content"]
99
+ gen = chat_with_groq(user_msg, hist[:-1], m, t, s)
100
+
101
+ hist.append({"role": "assistant", "content": ""})
102
+ for chunk in gen:
103
+ hist[-1]["content"] = chunk
104
+ yield hist
105
 
106
+ # Link Input Actions
107
  msg_input.submit(user_turn, [msg_input, chatbot], [msg_input, chatbot], queue=False).then(
108
  bot_turn, [chatbot, model_choice, temp, sys_msg], chatbot
109
  )
110
  submit_btn.click(user_turn, [msg_input, chatbot], [msg_input, chatbot], queue=False).then(
111
  bot_turn, [chatbot, model_choice, temp, sys_msg], chatbot
112
  )
113
+
114
+ # Clear Workspace
115
  clear.click(lambda: [], None, chatbot, queue=False)
116
 
117
+ # 5. Production Launch
118
  if __name__ == "__main__":
119
  # Settings moved to launch() for Gradio 6 compatibility
120
+ demo.launch(
121
+ theme=gr.themes.Soft(primary_hue="blue"),
122
+ css=custom_css
123
+ )