AbuSaleh28 commited on
Commit
2a8660c
·
verified ·
1 Parent(s): 71bf23c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -30
app.py CHANGED
@@ -2,12 +2,14 @@ import gradio as gr
2
  import os
3
  from groq import Groq
4
 
5
- # Initialize the Groq client
6
- # Retrieve API key from Colab's userdata
7
- os.environ["GROQ_API_KEY"] = userdata.get('ChatBot_API')
8
- client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
9
 
10
- # Custom CSS to give it a sleek, modern look
 
 
11
  custom_css = """
12
  .gradio-container { background-color: #0b0f19; font-family: 'Inter', sans-serif; }
13
  #title-header { text-align: center; margin-bottom: 20px; }
@@ -18,20 +20,31 @@ custom_css = """
18
 
19
  def chat_stream(message, history, model, system_prompt, temperature, max_tokens):
20
  """
21
- Custom chat function built for gr.Chatbot component format.
22
- history is a list of dicts: [{"role": "user", "content": "..."}, ...]
23
  """
24
- # 1. Start with the custom system prompt
 
 
 
 
25
  messages = [{"role": "system", "content": system_prompt}]
26
 
27
- # 2. Append the conversation history
28
  for turn in history:
29
- messages.append({"role": turn["role"], "content": turn["content"]})
 
 
 
 
30
 
31
- # 3. Append current user message
32
  messages.append({"role": "user", "content": message})
 
 
 
 
33
 
34
- # 4. Stream response from Groq
35
  try:
36
  stream = client.chat.completions.create(
37
  model=model,
@@ -45,16 +58,17 @@ def chat_stream(message, history, model, system_prompt, temperature, max_tokens)
45
  for chunk in stream:
46
  if chunk.choices[0].delta.content:
47
  partial_response += chunk.choices[0].delta.content
48
- yield partial_response
 
 
49
 
50
  except Exception as e:
51
- yield f"⚠️ Error connecting to Groq: {str(e)}"
 
52
 
 
 
53
 
54
- # Building the Interactive Interface using Blocks
55
- with gr.Blocks() as demo: # Removed css and theme from Blocks constructor
56
-
57
- # Title Section - Replaced gr.Div with gr.Markdown
58
  gr.Markdown("# 🚀 Personal AI ChatBot", elem_id="title-header")
59
  gr.Markdown("A fully customizable, hyper-fast LLM workspace.")
60
 
@@ -94,27 +108,29 @@ with gr.Blocks() as demo: # Removed css and theme from Blocks constructor
94
 
95
  # --- RIGHT COLUMN: CHAT INTERFACE ---
96
  with gr.Column(scale=3):
97
- chatbot = gr.Chatbot(
98
- elem_classes="chat-window"
99
- # Removed bubble_full_width as it's not a valid argument for gr.Chatbot
100
- )
101
 
102
- # Textbox and setup for user interaction
103
  msg_input = gr.Textbox(
104
  placeholder="Type your message here and press Enter...",
105
  show_label=False,
106
  container=False
107
  )
 
 
108
 
109
- # Wrap everything into Gradio's chat system
110
- gr.ChatInterface(
111
  fn=chat_stream,
112
- chatbot=chatbot,
113
- textbox=msg_input,
114
- additional_inputs=[model_select, system_input, temp_slider, tokens_slider]
115
- # Removed type="messages" as it's not a valid argument for gr.ChatInterface
 
 
116
  )
 
 
 
117
 
118
  if __name__ == "__main__":
119
- # Moved css and theme to launch()
120
  demo.launch(css=custom_css, theme=gr.themes.Soft())
 
2
  import os
3
  from groq import Groq
4
 
5
+ # Initialize the Groq client via Hugging Face Secrets
6
+ api_key = os.environ.get("GROQ_API_KEY")
7
+ if not api_key:
8
+ raise ValueError("GROQ_API_KEY environment variable not found. Please add it as a Secret in your Space Settings.")
9
 
10
+ client = Groq(api_key=api_key)
11
+
12
+ # Custom CSS for modern styling
13
  custom_css = """
14
  .gradio-container { background-color: #0b0f19; font-family: 'Inter', sans-serif; }
15
  #title-header { text-align: center; margin-bottom: 20px; }
 
20
 
21
  def chat_stream(message, history, model, system_prompt, temperature, max_tokens):
22
  """
23
+ Handles streaming responses using the standard history list layout.
 
24
  """
25
+ if not message.strip():
26
+ yield history
27
+ return
28
+
29
+ # 1. Initialize message list with system prompt
30
  messages = [{"role": "system", "content": system_prompt}]
31
 
32
+ # 2. Add conversation history
33
  for turn in history:
34
+ # turn is typically [user_message, assistant_message]
35
+ if turn[0]:
36
+ messages.append({"role": "user", "content": turn[0]})
37
+ if turn[1]:
38
+ messages.append({"role": "assistant", "content": turn[1]})
39
 
40
+ # 3. Add the current user message
41
  messages.append({"role": "user", "content": message})
42
+
43
+ # 4. Append user message to history visually before generating
44
+ history.append([message, ""])
45
+ yield history
46
 
47
+ # 5. Stream from Groq
48
  try:
49
  stream = client.chat.completions.create(
50
  model=model,
 
58
  for chunk in stream:
59
  if chunk.choices[0].delta.content:
60
  partial_response += chunk.choices[0].delta.content
61
+ # Update the last assistant turn in history
62
+ history[-1][1] = partial_response
63
+ yield history
64
 
65
  except Exception as e:
66
+ history[-1][1] = f"⚠️ Error connecting to Groq: {str(e)}"
67
+ yield history
68
 
69
+ # Build the layout manually without gr.ChatInterface macros
70
+ with gr.Blocks() as demo:
71
 
 
 
 
 
72
  gr.Markdown("# 🚀 Personal AI ChatBot", elem_id="title-header")
73
  gr.Markdown("A fully customizable, hyper-fast LLM workspace.")
74
 
 
108
 
109
  # --- RIGHT COLUMN: CHAT INTERFACE ---
110
  with gr.Column(scale=3):
111
+ chatbot = gr.Chatbot(elem_classes="chat-window")
 
 
 
112
 
 
113
  msg_input = gr.Textbox(
114
  placeholder="Type your message here and press Enter...",
115
  show_label=False,
116
  container=False
117
  )
118
+
119
+ clear_btn = gr.Button("🗑️ Clear Conversation")
120
 
121
+ # Native component wiring
122
+ msg_input.submit(
123
  fn=chat_stream,
124
+ inputs=[msg_input, chatbot, model_select, system_input, temp_slider, tokens_slider],
125
+ outputs=[chatbot]
126
+ ).then(
127
+ fn=lambda: "",
128
+ inputs=None,
129
+ outputs=[msg_input]
130
  )
131
+
132
+ # Clear chat utility logic
133
+ clear_btn.click(fn=lambda: [], inputs=None, outputs=[chatbot])
134
 
135
  if __name__ == "__main__":
 
136
  demo.launch(css=custom_css, theme=gr.themes.Soft())