sagar007 commited on
Commit
b57d11e
·
verified ·
1 Parent(s): b270c1f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -135
app.py CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
2
  import spaces
3
  import torch
4
  from transformers import AutoTokenizer, AutoModelForCausalLM
5
- from googlesearch import search # Or use Serper API for better results
6
  import requests
7
  from bs4 import BeautifulSoup
8
 
@@ -20,7 +20,8 @@ def fetch_web_content(url):
20
  response = requests.get(url, timeout=10)
21
  soup = BeautifulSoup(response.text, 'html.parser')
22
  return ' '.join(p.get_text() for p in soup.find_all('p'))
23
- except:
 
24
  return "Could not fetch content from this URL"
25
 
26
  def web_search(query, num_results=3):
@@ -34,7 +35,8 @@ def web_search(query, num_results=3):
34
  "content": content[:1000] # Limit content length
35
  })
36
  return results
37
- except:
 
38
  return []
39
 
40
  @spaces.GPU(duration=120)
@@ -63,7 +65,7 @@ def generate_text(prompt, max_length, temperature, use_web):
63
 
64
  return tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True)
65
 
66
- # Enhanced CSS
67
  css = """
68
  :root {
69
  --primary: #e94560;
@@ -96,25 +98,6 @@ body {
96
  border-radius: 15px 15px 0 0;
97
  text-align: center;
98
  margin-bottom: 2rem;
99
- position: relative;
100
- overflow: hidden;
101
- }
102
-
103
- .header::before {
104
- content: '';
105
- position: absolute;
106
- top: -50%;
107
- left: -50%;
108
- width: 200%;
109
- height: 200%;
110
- background: radial-gradient(circle, rgba(233,69,96,0.1) 0%, transparent 70%);
111
- animation: pulse 8s infinite;
112
- }
113
-
114
- @keyframes pulse {
115
- 0% { transform: scale(0.8); opacity: 0.5; }
116
- 50% { transform: scale(1.2); opacity: 0.2; }
117
- 100% { transform: scale(0.8); opacity: 0.5; }
118
  }
119
 
120
  .header h1 {
@@ -122,14 +105,6 @@ body {
122
  font-size: 2.8rem;
123
  margin-bottom: 1rem;
124
  font-weight: 700;
125
- position: relative;
126
- }
127
-
128
- .header p {
129
- color: #a0a0a0;
130
- font-size: 1.1rem;
131
- max-width: 800px;
132
- margin: 0 auto;
133
  }
134
 
135
  .input-group, .output-group {
@@ -138,119 +113,33 @@ body {
138
  border-radius: 12px;
139
  margin-bottom: 2rem;
140
  border: 1px solid #2d2d4d;
141
- transition: transform 0.3s ease, box-shadow 0.3s ease;
142
- }
143
-
144
- .input-group:hover, .output-group:hover {
145
- transform: translateY(-2px);
146
- box-shadow: 0 6px 15px rgba(0, 0, 0, 0.3);
147
  }
148
 
149
  .generate-btn {
150
  background: linear-gradient(135deg, var(--primary) 0%, #c81e45 100%) !important;
151
  color: white !important;
152
- border: none !important;
153
  border-radius: 8px !important;
154
  padding: 12px 28px !important;
155
- font-size: 1.1rem !important;
156
- cursor: pointer !important;
157
- transition: transform 0.2s ease, box-shadow 0.2s ease !important;
158
- }
159
-
160
- .generate-btn:hover {
161
- transform: scale(1.05);
162
- box-shadow: 0 4px 15px rgba(233, 69, 96, 0.4) !important;
163
- }
164
-
165
- .example-prompts {
166
- background-color: #1f2b47;
167
- padding: 1.5rem;
168
- border-radius: 12px;
169
- margin-bottom: 2rem;
170
- border: 1px solid #3d3d6d;
171
- }
172
-
173
- .example-prompts h3 {
174
- color: var(--primary);
175
- margin-bottom: 1rem;
176
- font-size: 1.3rem;
177
  }
178
 
179
  .example-prompts ul {
180
- list-style: none;
181
- padding: 0;
182
- display: grid;
183
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
184
  gap: 1rem;
185
  }
186
-
187
- .example-prompts li {
188
- background-color: var(--secondary);
189
- padding: 1rem;
190
- border-radius: 8px;
191
- cursor: pointer;
192
- transition: all 0.2s ease;
193
- border: 1px solid #3d3d6d;
194
- }
195
-
196
- .example-prompts li:hover {
197
- background-color: #2d2d4d;
198
- transform: translateY(-2px);
199
- box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
200
- }
201
-
202
- .param-group {
203
- display: grid;
204
- grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
205
- gap: 1.5rem;
206
- margin-bottom: 1.5rem;
207
- }
208
-
209
- .web-toggle {
210
- display: flex;
211
- align-items: center;
212
- gap: 1rem;
213
- padding: 1rem;
214
- background-color: var(--secondary);
215
- border-radius: 8px;
216
- margin-bottom: 1.5rem;
217
- }
218
-
219
- .output-controls {
220
- display: flex;
221
- gap: 1rem;
222
- margin-top: 1.5rem;
223
- }
224
-
225
- #copy-btn {
226
- background: #2d2d4d !important;
227
- border: 1px solid #3d3d6d !important;
228
- }
229
-
230
- #copy-btn:hover {
231
- background: #3d3d6d !important;
232
- }
233
  """
234
 
235
  example_prompts = [
236
  "Explain quantum computing in simple terms",
237
  "Latest developments in AI research",
238
- "How does blockchain technology work?",
239
  "Compare React and Vue.js frameworks",
240
- "Best practices for Python async programming",
241
- "Impact of climate change on marine life",
242
- "Recent advancements in cancer treatment",
243
- "Guide to starting a tech startup in 2024"
244
  ]
245
 
246
  with gr.Blocks(css=css, theme=gr.themes.Default()) as iface:
247
  gr.HTML("""
248
  <div class="header">
249
  <h1>Llama-3.1-Storm-8B AI Assistant</h1>
250
- <p>Enhanced with real-time web search capabilities and multimodal interaction</p>
251
- <img src="https://cdn-uploads.huggingface.co/production/uploads/64c75c1237333ccfef30a602/tmOlbERGKP7JSODa6T06J.jpeg"
252
- alt="Llama"
253
- style="border-radius: 12px; margin: 1.5rem 0;">
254
  </div>
255
  """)
256
 
@@ -260,17 +149,16 @@ with gr.Blocks(css=css, theme=gr.themes.Default()) as iface:
260
  with gr.Column(scale=3):
261
  with gr.Group(elem_classes="example-prompts"):
262
  gr.Markdown("## Example Queries")
263
- example_btns = [gr.Button(prompt, scale=0) for prompt in example_prompts]
264
 
265
  with gr.Group(elem_classes="input-group"):
266
- prompt = gr.Textbox(label="Your Query", placeholder="Enter your question or prompt...",
267
- lines=5, elem_id="main-input")
268
 
269
- with gr.Group(elem_classes="web-toggle"):
270
  web_search_toggle = gr.Checkbox(label="Enable Web Search", value=False)
271
- num_results = gr.Slider(1, 5, value=3, step=1, label="Search Results to Use")
272
 
273
- with gr.Group(elem_classes="param-group"):
274
  max_length = gr.Slider(32, 1024, value=256, step=32, label="Response Length")
275
  temperature = gr.Slider(0.1, 2.0, value=0.7, step=0.1, label="Creativity")
276
 
@@ -278,15 +166,15 @@ with gr.Blocks(css=css, theme=gr.themes.Default()) as iface:
278
 
279
  with gr.Column(scale=2):
280
  with gr.Group(elem_classes="output-group"):
281
- output = gr.Textbox(label="Generated Response", lines=12, elem_id="main-output")
282
- with gr.Group(elem_classes="output-controls"):
283
- copy_btn = gr.Button("Copy to Clipboard", elem_id="copy-btn")
284
- clear_btn = gr.Button("Clear Output", elem_id="copy-btn")
285
 
286
- with gr.TabItem("Web Search Results"):
287
- web_results = gr.JSON(label="Search Results Preview", visible=True)
288
 
289
- # Event handling
290
  generate_btn.click(
291
  generate_text,
292
  inputs=[prompt, max_length, temperature, web_search_toggle],
@@ -300,11 +188,12 @@ with gr.Blocks(css=css, theme=gr.themes.Default()) as iface:
300
  for btn in example_btns:
301
  btn.click(lambda x: x, inputs=[btn], outputs=[prompt])
302
 
303
- copy_btn.click(lambda x: x, inputs=[output], outputs=[]).then(
304
  None,
305
- _js="() => navigator.clipboard.writeText(document.getElementById('main-output').value)"
 
306
  )
307
-
308
  clear_btn.click(lambda: "", outputs=[output])
309
 
310
  iface.launch()
 
2
  import spaces
3
  import torch
4
  from transformers import AutoTokenizer, AutoModelForCausalLM
5
+ from googlesearch import search
6
  import requests
7
  from bs4 import BeautifulSoup
8
 
 
20
  response = requests.get(url, timeout=10)
21
  soup = BeautifulSoup(response.text, 'html.parser')
22
  return ' '.join(p.get_text() for p in soup.find_all('p'))
23
+ except Exception as e:
24
+ print(f"Error fetching {url}: {str(e)}")
25
  return "Could not fetch content from this URL"
26
 
27
  def web_search(query, num_results=3):
 
35
  "content": content[:1000] # Limit content length
36
  })
37
  return results
38
+ except Exception as e:
39
+ print(f"Search error: {str(e)}")
40
  return []
41
 
42
  @spaces.GPU(duration=120)
 
65
 
66
  return tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True)
67
 
68
+ # CSS and UI components
69
  css = """
70
  :root {
71
  --primary: #e94560;
 
98
  border-radius: 15px 15px 0 0;
99
  text-align: center;
100
  margin-bottom: 2rem;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  }
102
 
103
  .header h1 {
 
105
  font-size: 2.8rem;
106
  margin-bottom: 1rem;
107
  font-weight: 700;
 
 
 
 
 
 
 
 
108
  }
109
 
110
  .input-group, .output-group {
 
113
  border-radius: 12px;
114
  margin-bottom: 2rem;
115
  border: 1px solid #2d2d4d;
 
 
 
 
 
 
116
  }
117
 
118
  .generate-btn {
119
  background: linear-gradient(135deg, var(--primary) 0%, #c81e45 100%) !important;
120
  color: white !important;
 
121
  border-radius: 8px !important;
122
  padding: 12px 28px !important;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
  }
124
 
125
  .example-prompts ul {
 
 
 
126
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
127
  gap: 1rem;
128
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
  """
130
 
131
  example_prompts = [
132
  "Explain quantum computing in simple terms",
133
  "Latest developments in AI research",
 
134
  "Compare React and Vue.js frameworks",
135
+ "Recent advancements in cancer treatment"
 
 
 
136
  ]
137
 
138
  with gr.Blocks(css=css, theme=gr.themes.Default()) as iface:
139
  gr.HTML("""
140
  <div class="header">
141
  <h1>Llama-3.1-Storm-8B AI Assistant</h1>
142
+ <p>Enhanced with real-time web search capabilities</p>
 
 
 
143
  </div>
144
  """)
145
 
 
149
  with gr.Column(scale=3):
150
  with gr.Group(elem_classes="example-prompts"):
151
  gr.Markdown("## Example Queries")
152
+ example_btns = [gr.Button(prompt) for prompt in example_prompts]
153
 
154
  with gr.Group(elem_classes="input-group"):
155
+ prompt = gr.Textbox(label="Your Query", placeholder="Enter your question...", lines=5)
 
156
 
157
+ with gr.Row():
158
  web_search_toggle = gr.Checkbox(label="Enable Web Search", value=False)
159
+ num_results = gr.Slider(1, 5, value=3, step=1, label="Search Results")
160
 
161
+ with gr.Row():
162
  max_length = gr.Slider(32, 1024, value=256, step=32, label="Response Length")
163
  temperature = gr.Slider(0.1, 2.0, value=0.7, step=0.1, label="Creativity")
164
 
 
166
 
167
  with gr.Column(scale=2):
168
  with gr.Group(elem_classes="output-group"):
169
+ output = gr.Textbox(label="Generated Response", lines=12)
170
+ with gr.Row():
171
+ copy_btn = gr.Button("Copy")
172
+ clear_btn = gr.Button("Clear")
173
 
174
+ with gr.TabItem("Web Results"):
175
+ web_results = gr.JSON(label="Search Results Preview")
176
 
177
+ # Event handlers
178
  generate_btn.click(
179
  generate_text,
180
  inputs=[prompt, max_length, temperature, web_search_toggle],
 
188
  for btn in example_btns:
189
  btn.click(lambda x: x, inputs=[btn], outputs=[prompt])
190
 
191
+ copy_btn.click(
192
  None,
193
+ inputs=[output],
194
+ _js="(text) => { navigator.clipboard.writeText(text); return [] }"
195
  )
196
+
197
  clear_btn.click(lambda: "", outputs=[output])
198
 
199
  iface.launch()