Yordann commited on
Commit
f2b1c55
·
verified ·
1 Parent(s): f1ad27a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -221
app.py CHANGED
@@ -7,17 +7,17 @@ generator = pipeline("text-generation", model="gpt2")
7
  bg_to_en = pipeline("translation", model="Helsinki-NLP/opus-mt-bg-en")
8
  en_to_bg = pipeline("translation", model="Helsinki-NLP/opus-mt-en-bg")
9
 
10
- NUM_THREADS = 2
11
-
12
  def generate_response(user_input, top_p, temperature, chat_counter, chatbot, history, request: gr.Request):
13
  lang = detect(user_input)
14
  print(f"Detected language: {lang}")
15
 
 
16
  if lang == "bg":
17
  user_input_translated = bg_to_en(user_input)[0]["translation_text"]
18
  else:
19
  user_input_translated = user_input
20
 
 
21
  prompt = (
22
  "You are a helpful e-commerce business assistant for beginners.\n"
23
  "Here is what the user said:\n"
@@ -25,36 +25,32 @@ def generate_response(user_input, top_p, temperature, chat_counter, chatbot, his
25
  "Give simple, friendly, clear advice:"
26
  )
27
 
28
- response = generator(prompt, max_length=200, top_p=top_p, temperature=temperature, do_sample=True)[0]["generated_text"]
 
29
 
30
  if lang == "bg":
31
- response = en_to_bg(response)[0]["translation_text"]
32
 
33
- chat_counter += 1
34
- history.append((user_input, response))
 
35
 
 
36
  return history, history, chat_counter, "✅ Success", gr.update(value=''), gr.update(interactive=True)
37
 
38
  def reset_textbox():
39
  return gr.update(value='', interactive=False), gr.update(interactive=False)
40
 
41
- # Custom CSS to match MasterBrand website design
42
- custom_css = """
43
- /* Import Google Fonts */
44
  @import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800;900&display=swap');
45
-
46
- /* Global styles */
47
  * {
48
  font-family: 'Inter', sans-serif !important;
49
  }
50
-
51
- /* Main container styling */
52
  .gradio-container {
53
  background: linear-gradient(135deg, #1a1a1a 0%, #2a2a2a 100%) !important;
54
  min-height: 100vh !important;
55
  }
56
-
57
- /* Header styling */
58
  .main-header {
59
  background: linear-gradient(135deg, #1a1a1a 0%, #2a2a2a 100%) !important;
60
  padding: 2rem 0 !important;
@@ -62,41 +58,23 @@ custom_css = """
62
  border-bottom: 2px solid #ff4d00 !important;
63
  margin-bottom: 2rem !important;
64
  }
65
-
66
  .main-header h1 {
67
  color: #ffffff !important;
68
  font-size: 3rem !important;
69
  font-weight: 800 !important;
70
- margin-bottom: 0.5rem !important;
71
  text-shadow: 0 0 20px rgba(255, 77, 0, 0.3) !important;
72
  }
73
-
74
  .main-header p {
75
  color: #cccccc !important;
76
  font-size: 1.2rem !important;
77
- font-weight: 400 !important;
78
- margin: 0 !important;
79
  }
80
-
81
- /* Chatbot container */
82
- #chatbot-container {
83
  background: rgba(42, 42, 42, 0.8) !important;
84
  border: 2px solid #ff4d00 !important;
85
  border-radius: 20px !important;
86
  box-shadow: 0 10px 30px rgba(255, 77, 0, 0.2) !important;
87
  margin-bottom: 2rem !important;
88
- min-height: 400px !important;
89
- }
90
-
91
- /* Input styling */
92
- #input-container {
93
- background: rgba(42, 42, 42, 0.9) !important;
94
- border: 2px solid #ff4d00 !important;
95
- border-radius: 15px !important;
96
- padding: 1rem !important;
97
- margin-bottom: 1rem !important;
98
  }
99
-
100
  .input-container textarea {
101
  background: #2a2a2a !important;
102
  border: 1px solid #ff4d00 !important;
@@ -106,15 +84,12 @@ custom_css = """
106
  padding: 1rem !important;
107
  resize: none !important;
108
  }
109
-
110
  .input-container textarea:focus {
111
  border-color: #ff6d20 !important;
112
  box-shadow: 0 0 15px rgba(255, 77, 0, 0.3) !important;
113
  outline: none !important;
114
  }
115
-
116
- /* Button styling */
117
- #submit-button {
118
  background: linear-gradient(135deg, #ff4d00 0%, #ff6d20 100%) !important;
119
  border: none !important;
120
  border-radius: 12px !important;
@@ -125,246 +100,89 @@ custom_css = """
125
  transition: all 0.3s ease !important;
126
  box-shadow: 0 5px 15px rgba(255, 77, 0, 0.3) !important;
127
  }
128
-
129
  .submit-button:hover {
130
  background: linear-gradient(135deg, #ff6d20 0%, #ff8d40 100%) !important;
131
  transform: translateY(-2px) !important;
132
  box-shadow: 0 8px 25px rgba(255, 77, 0, 0.4) !important;
133
  }
134
-
135
- /* Slider styling */
136
- #slider-container {
137
- background: rgba(42, 42, 42, 0.6) !important;
138
- border: 1px solid #ff4d00 !important;
139
- border-radius: 10px !important;
140
- padding: 1rem !important;
141
- margin-bottom: 1rem !important;
142
- }
143
-
144
  .slider-container label {
145
  color: #ffffff !important;
146
  font-weight: 600 !important;
147
  font-size: 1rem !important;
148
- margin-bottom: 0.5rem !important;
149
- display: block !important;
150
- }
151
-
152
- /* Status box styling */
153
- #status-container {
154
- background: rgba(42, 42, 42, 0.8) !important;
155
- border: 1px solid #4CAF50 !important;
156
- border-radius: 10px !important;
157
- padding: 1rem !important;
158
  }
159
-
160
  .status-container textarea {
161
  background: transparent !important;
162
  border: none !important;
163
  color: #4CAF50 !important;
164
  font-weight: 600 !important;
165
  }
166
-
167
- /* Advanced styling for chat messages */
168
- .message {
169
- animation: fadeInUp 0.5s ease-out !important;
170
- }
171
-
172
- @keyframes fadeInUp {
173
- from {
174
- opacity: 0;
175
- transform: translateY(20px);
176
- }
177
- to {
178
- opacity: 1;
179
- transform: translateY(0);
180
- }
181
- }
182
-
183
- /* Responsive design */
184
- @media (max-width: 768px) {
185
- .main-header h1 {
186
- font-size: 2rem !important;
187
- }
188
-
189
- .main-header p {
190
- font-size: 1rem !important;
191
- }
192
-
193
- .submit-button {
194
- font-size: 1rem !important;
195
- padding: 0.8rem 1.5rem !important;
196
- }
197
- }
198
-
199
- /* Hide Gradio branding */
200
  .footer {
201
  display: none !important;
202
  }
203
-
204
- /* Custom scrollbar */
205
- ::-webkit-scrollbar {
206
- width: 8px;
207
- }
208
-
209
- ::-webkit-scrollbar-track {
210
- background: #2a2a2a;
211
- }
212
-
213
- ::-webkit-scrollbar-thumb {
214
- background: #ff4d00;
215
- border-radius: 4px;
216
- }
217
-
218
- ::-webkit-scrollbar-thumb:hover {
219
- background: #ff6d20;
220
- }
221
  """
222
 
223
- # Enhanced theme matching MasterBrand
224
- masterbrand_theme = gr.themes.Base(
225
- primary_hue="orange",
226
- secondary_hue="blue",
227
- neutral_hue="slate",
228
- ).set(
229
- # Background colors
230
  body_background_fill="#1a1a1a",
231
- body_background_fill_dark="#1a1a1a",
232
- background_fill_primary="#2a2a2a",
233
- background_fill_primary_dark="#2a2a2a",
234
- background_fill_secondary="#3a3a3a",
235
- background_fill_secondary_dark="#3a3a3a",
236
-
237
- # Button colors
238
- button_primary_background_fill="#ff4d00",
239
- button_primary_background_fill_dark="#ff4d00",
240
- button_primary_background_fill_hover="#ff6d20",
241
- button_primary_background_fill_hover_dark="#ff6d20",
242
- button_primary_text_color="#ffffff",
243
- button_primary_text_color_dark="#ffffff",
244
-
245
- # Text colors
246
  body_text_color="#ffffff",
247
- body_text_color_dark="#ffffff",
248
- body_text_color_subdued="#cccccc",
249
- body_text_color_subdued_dark="#cccccc",
250
-
251
- # Border and accent colors
252
  border_color_accent="#ff4d00",
253
- border_color_accent_dark="#ff4d00",
254
- border_color_primary="#3a3a3a",
255
- border_color_primary_dark="#3a3a3a",
256
- color_accent="#ff4d00",
257
- color_accent_soft="#ff4d00",
258
-
259
- # Input colors
260
- input_background_fill="#2a2a2a",
261
- input_background_fill_dark="#2a2a2a",
262
- input_background_fill_focus="#3a3a3a",
263
- input_background_fill_focus_dark="#3a3a3a",
264
- input_border_color="#ff4d00",
265
- input_border_color_dark="#ff4d00",
266
- input_border_color_focus="#ff6d20",
267
- input_border_color_focus_dark="#ff6d20",
268
-
269
- # Link colors
270
- link_text_color="#ff4d00",
271
- link_text_color_dark="#ff4d00",
272
- link_text_color_active="#ff6d20",
273
- link_text_color_active_dark="#ff6d20",
274
- link_text_color_hover="#ff6d20",
275
- link_text_color_hover_dark="#ff6d20",
276
- link_text_color_visited="#ff4d00",
277
- link_text_color_visited_dark="#ff4d00",
278
-
279
- # Slider colors
280
- slider_color="#ff4d00",
281
- slider_color_dark="#ff4d00",
282
  )
283
 
 
284
  with gr.Blocks(theme=masterbrand_theme, css=custom_css, title="MasterBrand AI Assistant") as demo:
285
- gr.Textbox(label="Test Box", value="If you see this, layout works")
286
- # Header section
287
  gr.HTML("""
288
  <div class="main-header">
289
  <h1>🛍️ MASTERBRAND AI ASSISTANT</h1>
290
  <p>Your Personal E-commerce Business Expert - Available in English & Bulgarian</p>
291
  </div>
292
  """)
293
-
294
- # Main chat interface
295
  with gr.Row():
296
  with gr.Column(scale=1):
297
  chatbot = gr.Chatbot(
298
  label="💬 Chat with MasterBrand AI",
299
  height=400,
300
- elem_id="chatbot-container",
301
  type="messages"
302
  )
303
-
304
- with gr.Row():
305
- inputs = gr.Textbox(
306
- placeholder="Ask me anything about your e-commerce business... (e.g., 'What product should I sell?', 'How do I start dropshipping?')",
307
- label="Your Question",
308
- lines=2,
309
- elem_id="input-container"
310
- )
311
-
312
  with gr.Row():
313
  submit_btn = gr.Button(
314
  "🚀 Get Expert Advice",
315
  variant="primary",
316
  size="lg",
317
- elem_id="submit-button"
318
  )
319
  status_box = gr.Textbox(
320
  label="Status",
321
  interactive=False,
322
- elem_id="status-container"
323
  )
324
-
325
- # Advanced settings (collapsible)
326
  with gr.Accordion("⚙️ Advanced Settings", open=False):
327
- with gr.Row():
328
- with gr.Column():
329
- top_p = gr.Slider(
330
- minimum=0.1,
331
- maximum=1.0,
332
- value=0.9,
333
- step=0.05,
334
- label="🎯 Creativity Level (Top-p)",
335
- info="Higher values = more creative responses",
336
- elem_id="slider-container"
337
- )
338
- with gr.Column():
339
- temperature = gr.Slider(
340
- minimum=0.1,
341
- maximum=2.0,
342
- value=1.0,
343
- step=0.1,
344
- label="🌡️ Response Temperature",
345
- info="Higher values = more varied responses",
346
- elem_id="slider-container"
347
- )
348
-
349
- # Hidden state variables
350
  state = gr.State([])
351
  chat_counter = gr.Number(value=0, visible=False)
352
-
353
- # Footer
354
- gr.HTML("""
355
- <div style="text-align: center; padding: 2rem; color: #cccccc; border-top: 1px solid #3a3a3a; margin-top: 2rem;">
356
- <p>🚀 Powered by MasterBrand AI Technology | Built for E-commerce Success</p>
357
- <p style="font-size: 0.9rem; opacity: 0.7;">Get personalized advice for your online business journey</p>
358
- </div>
359
- """)
360
-
361
- # Event handlers
362
  inputs.submit(reset_textbox, [], [inputs, submit_btn], queue=False)
363
  inputs.submit(generate_response, [inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, status_box, inputs, submit_btn])
364
  submit_btn.click(reset_textbox, [], [inputs, submit_btn], queue=False)
365
  submit_btn.click(generate_response, [inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, status_box, inputs, submit_btn])
366
 
 
367
  if __name__ == "__main__":
368
  demo.queue(max_size=10).launch()
369
-
370
-
 
7
  bg_to_en = pipeline("translation", model="Helsinki-NLP/opus-mt-bg-en")
8
  en_to_bg = pipeline("translation", model="Helsinki-NLP/opus-mt-en-bg")
9
 
 
 
10
  def generate_response(user_input, top_p, temperature, chat_counter, chatbot, history, request: gr.Request):
11
  lang = detect(user_input)
12
  print(f"Detected language: {lang}")
13
 
14
+ # Translate if needed
15
  if lang == "bg":
16
  user_input_translated = bg_to_en(user_input)[0]["translation_text"]
17
  else:
18
  user_input_translated = user_input
19
 
20
+ # Create prompt
21
  prompt = (
22
  "You are a helpful e-commerce business assistant for beginners.\n"
23
  "Here is what the user said:\n"
 
25
  "Give simple, friendly, clear advice:"
26
  )
27
 
28
+ # Generate response
29
+ response_text = generator(prompt, max_length=200, top_p=top_p, temperature=temperature, do_sample=True)[0]["generated_text"]
30
 
31
  if lang == "bg":
32
+ response_text = en_to_bg(response_text)[0]["translation_text"]
33
 
34
+ # Format for type="messages"
35
+ history.append({"role": "user", "content": user_input})
36
+ history.append({"role": "assistant", "content": response_text})
37
 
38
+ chat_counter += 1
39
  return history, history, chat_counter, "✅ Success", gr.update(value=''), gr.update(interactive=True)
40
 
41
  def reset_textbox():
42
  return gr.update(value='', interactive=False), gr.update(interactive=False)
43
 
44
+ # ==== Custom CSS ====
45
+ custom_css = """
 
46
  @import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800;900&display=swap');
 
 
47
  * {
48
  font-family: 'Inter', sans-serif !important;
49
  }
 
 
50
  .gradio-container {
51
  background: linear-gradient(135deg, #1a1a1a 0%, #2a2a2a 100%) !important;
52
  min-height: 100vh !important;
53
  }
 
 
54
  .main-header {
55
  background: linear-gradient(135deg, #1a1a1a 0%, #2a2a2a 100%) !important;
56
  padding: 2rem 0 !important;
 
58
  border-bottom: 2px solid #ff4d00 !important;
59
  margin-bottom: 2rem !important;
60
  }
 
61
  .main-header h1 {
62
  color: #ffffff !important;
63
  font-size: 3rem !important;
64
  font-weight: 800 !important;
 
65
  text-shadow: 0 0 20px rgba(255, 77, 0, 0.3) !important;
66
  }
 
67
  .main-header p {
68
  color: #cccccc !important;
69
  font-size: 1.2rem !important;
 
 
70
  }
71
+ .chatbot-container {
 
 
72
  background: rgba(42, 42, 42, 0.8) !important;
73
  border: 2px solid #ff4d00 !important;
74
  border-radius: 20px !important;
75
  box-shadow: 0 10px 30px rgba(255, 77, 0, 0.2) !important;
76
  margin-bottom: 2rem !important;
 
 
 
 
 
 
 
 
 
 
77
  }
 
78
  .input-container textarea {
79
  background: #2a2a2a !important;
80
  border: 1px solid #ff4d00 !important;
 
84
  padding: 1rem !important;
85
  resize: none !important;
86
  }
 
87
  .input-container textarea:focus {
88
  border-color: #ff6d20 !important;
89
  box-shadow: 0 0 15px rgba(255, 77, 0, 0.3) !important;
90
  outline: none !important;
91
  }
92
+ .submit-button {
 
 
93
  background: linear-gradient(135deg, #ff4d00 0%, #ff6d20 100%) !important;
94
  border: none !important;
95
  border-radius: 12px !important;
 
100
  transition: all 0.3s ease !important;
101
  box-shadow: 0 5px 15px rgba(255, 77, 0, 0.3) !important;
102
  }
 
103
  .submit-button:hover {
104
  background: linear-gradient(135deg, #ff6d20 0%, #ff8d40 100%) !important;
105
  transform: translateY(-2px) !important;
106
  box-shadow: 0 8px 25px rgba(255, 77, 0, 0.4) !important;
107
  }
 
 
 
 
 
 
 
 
 
 
108
  .slider-container label {
109
  color: #ffffff !important;
110
  font-weight: 600 !important;
111
  font-size: 1rem !important;
 
 
 
 
 
 
 
 
 
 
112
  }
 
113
  .status-container textarea {
114
  background: transparent !important;
115
  border: none !important;
116
  color: #4CAF50 !important;
117
  font-weight: 600 !important;
118
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  .footer {
120
  display: none !important;
121
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
  """
123
 
124
+ # ==== Theme ====
125
+ masterbrand_theme = gr.themes.Base(primary_hue="orange").set(
 
 
 
 
 
126
  body_background_fill="#1a1a1a",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
  body_text_color="#ffffff",
 
 
 
 
 
128
  border_color_accent="#ff4d00",
129
+ button_primary_background_fill="#ff4d00",
130
+ button_primary_background_fill_hover="#ff6d20",
131
+ button_primary_text_color="#ffffff"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
  )
133
 
134
+ # ==== UI ====
135
  with gr.Blocks(theme=masterbrand_theme, css=custom_css, title="MasterBrand AI Assistant") as demo:
 
 
136
  gr.HTML("""
137
  <div class="main-header">
138
  <h1>🛍️ MASTERBRAND AI ASSISTANT</h1>
139
  <p>Your Personal E-commerce Business Expert - Available in English & Bulgarian</p>
140
  </div>
141
  """)
142
+
 
143
  with gr.Row():
144
  with gr.Column(scale=1):
145
  chatbot = gr.Chatbot(
146
  label="💬 Chat with MasterBrand AI",
147
  height=400,
148
+ elem_classes=["chatbot-container"],
149
  type="messages"
150
  )
151
+
152
+ inputs = gr.Textbox(
153
+ placeholder="Ask me anything about your e-commerce business...",
154
+ label="Your Question",
155
+ lines=2,
156
+ elem_classes=["input-container"]
157
+ )
158
+
 
159
  with gr.Row():
160
  submit_btn = gr.Button(
161
  "🚀 Get Expert Advice",
162
  variant="primary",
163
  size="lg",
164
+ elem_classes=["submit-button"]
165
  )
166
  status_box = gr.Textbox(
167
  label="Status",
168
  interactive=False,
169
+ elem_classes=["status-container"]
170
  )
171
+
 
172
  with gr.Accordion("⚙️ Advanced Settings", open=False):
173
+ top_p = gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="🎯 Creativity (Top-p)", elem_classes=["slider-container"])
174
+ temperature = gr.Slider(0.1, 2.0, value=1.0, step=0.1, label="🌡️ Temperature", elem_classes=["slider-container"])
175
+
176
+ # State
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177
  state = gr.State([])
178
  chat_counter = gr.Number(value=0, visible=False)
179
+
180
+ # Event Handlers
 
 
 
 
 
 
 
 
181
  inputs.submit(reset_textbox, [], [inputs, submit_btn], queue=False)
182
  inputs.submit(generate_response, [inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, status_box, inputs, submit_btn])
183
  submit_btn.click(reset_textbox, [], [inputs, submit_btn], queue=False)
184
  submit_btn.click(generate_response, [inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, status_box, inputs, submit_btn])
185
 
186
+ # ==== Launch ====
187
  if __name__ == "__main__":
188
  demo.queue(max_size=10).launch()