CultriX commited on
Commit
bb7c504
·
verified ·
1 Parent(s): b2fd468

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -20
app.py CHANGED
@@ -7,6 +7,10 @@ from bs4 import BeautifulSoup
7
  import io
8
  import os
9
  import base64
 
 
 
 
10
 
11
  # Input data with links to Hugging Face repositories
12
  data_full = [
@@ -61,7 +65,9 @@ def plot_average_scores():
61
  img_buffer.seek(0)
62
  img_base64 = base64.b64encode(img_buffer.read()).decode('utf-8')
63
  plt.close()
64
- return img_base64, "average_performance.png"
 
 
65
 
66
 
67
  def plot_task_performance():
@@ -85,7 +91,8 @@ def plot_task_performance():
85
  img_buffer.seek(0)
86
  img_base64 = base64.b64encode(img_buffer.read()).decode('utf-8')
87
  plt.close()
88
- return img_base64, "task_performance.png"
 
89
 
90
  def plot_task_specific_top_models():
91
  top_models = df_full.iloc[:, 2:].idxmax()
@@ -106,7 +113,8 @@ def plot_task_specific_top_models():
106
  img_buffer.seek(0)
107
  img_base64 = base64.b64encode(img_buffer.read()).decode('utf-8')
108
  plt.close()
109
- return img_base64, "task_specific_top_models.png"
 
110
 
111
  def scrape_mergekit_config(model_name):
112
  """
@@ -134,7 +142,8 @@ def plot_heatmap():
134
  img_buffer.seek(0)
135
  img_base64 = base64.b64encode(img_buffer.read()).decode('utf-8')
136
  plt.close()
137
- return img_base64, "performance_heatmap.png"
 
138
 
139
 
140
  def download_yaml(yaml_content, model_name):
@@ -154,26 +163,28 @@ def download_all_data():
154
  csv_data = csv_buffer.getvalue().encode('utf-8')
155
 
156
  # Prepare all plots
157
- average_plot_b64, average_plot_name = plot_average_scores()
158
- task_plot_b64, task_plot_name = plot_task_performance()
159
- top_models_plot_b64, top_models_plot_name = plot_task_specific_top_models()
160
- heatmap_plot_b64, heatmap_plot_name = plot_heatmap()
161
 
162
  plot_dict = {
163
- "average_performance": (average_plot_b64, average_plot_name),
164
- "task_performance": (task_plot_b64, task_plot_name),
165
- "top_models": (top_models_plot_b64, top_models_plot_name),
166
- "heatmap": (heatmap_plot_b64, heatmap_plot_name)
167
  }
168
 
169
  zip_buffer = io.BytesIO()
170
- import zipfile
171
  with zipfile.ZipFile(zip_buffer, 'w') as zf:
172
  zf.writestr("model_scores.csv", csv_data)
173
 
174
- for name, (b64, filename) in plot_dict.items():
175
- img_data = base64.b64decode(b64)
176
- zf.writestr(filename, img_data)
 
 
 
177
 
178
  for model_name in df_full["Model Configuration"].to_list():
179
  yaml_content = scrape_mergekit_config(model_name)
@@ -184,6 +195,38 @@ def download_all_data():
184
 
185
  return zip_buffer, "analysis_data.zip"
186
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
187
 
188
  # Gradio app
189
  with gr.Blocks() as demo:
@@ -191,25 +234,25 @@ with gr.Blocks() as demo:
191
 
192
  with gr.Row():
193
  btn1 = gr.Button("Show Average Performance")
194
- img1 = gr.Image(type="bytes", label="Average Performance Plot")
195
  img1_download = gr.File(label="Download Average Performance")
196
  btn1.click(plot_average_scores, outputs=[img1,img1_download])
197
 
198
  with gr.Row():
199
  btn2 = gr.Button("Show Task Performance")
200
- img2 = gr.Image(type="bytes", label="Task Performance Plot")
201
  img2_download = gr.File(label="Download Task Performance")
202
  btn2.click(plot_task_performance, outputs=[img2, img2_download])
203
 
204
  with gr.Row():
205
  btn3 = gr.Button("Task-Specific Top Models")
206
- img3 = gr.Image(type="bytes", label="Task-Specific Top Models Plot")
207
  img3_download = gr.File(label="Download Top Models")
208
  btn3.click(plot_task_specific_top_models, outputs=[img3, img3_download])
209
 
210
  with gr.Row():
211
  btn4 = gr.Button("Plot Performance Heatmap")
212
- heatmap_img = gr.Image(type="bytes", label="Performance Heatmap")
213
  heatmap_download = gr.File(label="Download Heatmap")
214
  btn4.click(plot_heatmap, outputs=[heatmap_img, heatmap_download])
215
 
@@ -229,6 +272,14 @@ with gr.Blocks() as demo:
229
  download_all_btn = gr.Button("Download Everything")
230
  all_downloads = gr.File(label="Download All Data")
231
  download_all_btn.click(download_all_data, outputs=all_downloads)
 
 
 
 
 
 
 
 
232
 
233
 
234
  demo.launch()
 
7
  import io
8
  import os
9
  import base64
10
+ import zipfile
11
+ from PIL import Image
12
+ from io import BytesIO
13
+
14
 
15
  # Input data with links to Hugging Face repositories
16
  data_full = [
 
65
  img_buffer.seek(0)
66
  img_base64 = base64.b64encode(img_buffer.read()).decode('utf-8')
67
  plt.close()
68
+
69
+ pil_image = Image.open(BytesIO(base64.b64decode(img_base64)))
70
+ return pil_image, "average_performance.png"
71
 
72
 
73
  def plot_task_performance():
 
91
  img_buffer.seek(0)
92
  img_base64 = base64.b64encode(img_buffer.read()).decode('utf-8')
93
  plt.close()
94
+ pil_image = Image.open(BytesIO(base64.b64decode(img_base64)))
95
+ return pil_image, "task_performance.png"
96
 
97
  def plot_task_specific_top_models():
98
  top_models = df_full.iloc[:, 2:].idxmax()
 
113
  img_buffer.seek(0)
114
  img_base64 = base64.b64encode(img_buffer.read()).decode('utf-8')
115
  plt.close()
116
+ pil_image = Image.open(BytesIO(base64.b64decode(img_base64)))
117
+ return pil_image, "task_specific_top_models.png"
118
 
119
  def scrape_mergekit_config(model_name):
120
  """
 
142
  img_buffer.seek(0)
143
  img_base64 = base64.b64encode(img_buffer.read()).decode('utf-8')
144
  plt.close()
145
+ pil_image = Image.open(BytesIO(base64.b64decode(img_base64)))
146
+ return pil_image, "performance_heatmap.png"
147
 
148
 
149
  def download_yaml(yaml_content, model_name):
 
163
  csv_data = csv_buffer.getvalue().encode('utf-8')
164
 
165
  # Prepare all plots
166
+ average_plot_pil, average_plot_name = plot_average_scores()
167
+ task_plot_pil, task_plot_name = plot_task_performance()
168
+ top_models_plot_pil, top_models_plot_name = plot_task_specific_top_models()
169
+ heatmap_plot_pil, heatmap_plot_name = plot_heatmap()
170
 
171
  plot_dict = {
172
+ "average_performance": (average_plot_pil, average_plot_name),
173
+ "task_performance": (task_plot_pil, task_plot_name),
174
+ "top_models": (top_models_plot_pil, top_models_plot_name),
175
+ "heatmap": (heatmap_plot_pil, heatmap_plot_name)
176
  }
177
 
178
  zip_buffer = io.BytesIO()
 
179
  with zipfile.ZipFile(zip_buffer, 'w') as zf:
180
  zf.writestr("model_scores.csv", csv_data)
181
 
182
+ for name, (pil_image, filename) in plot_dict.items():
183
+ image_bytes = io.BytesIO()
184
+ pil_image.save(image_bytes, format='PNG')
185
+ image_bytes.seek(0)
186
+ zf.writestr(filename, image_bytes.read())
187
+
188
 
189
  for model_name in df_full["Model Configuration"].to_list():
190
  yaml_content = scrape_mergekit_config(model_name)
 
195
 
196
  return zip_buffer, "analysis_data.zip"
197
 
198
+ def scrape_model_page(model_url):
199
+ """
200
+ Scrapes the Hugging Face model page for YAML configuration and other details.
201
+ """
202
+ try:
203
+ # Fetch the model page
204
+ response = requests.get(model_url)
205
+ if response.status_code != 200:
206
+ return f"Error: Unable to fetch the page (Status Code: {response.status_code})"
207
+
208
+ soup = BeautifulSoup(response.text, "html.parser")
209
+
210
+ # Extract YAML configuration (usually inside <pre> tags)
211
+ yaml_config = soup.find("pre")
212
+ yaml_text = yaml_config.text.strip() if yaml_config else "No YAML configuration found."
213
+
214
+ # Extract additional metadata or performance (if available)
215
+ metadata_section = soup.find("div", class_="metadata")
216
+ metadata_text = metadata_section.text.strip() if metadata_section else "No metadata found."
217
+
218
+ # Return the scraped details
219
+ return f"**YAML Configuration:**\n{yaml_text}\n\n**Metadata:**\n{metadata_text}"
220
+
221
+ except Exception as e:
222
+ return f"Error: {str(e)}"
223
+
224
+ def display_scraped_model_data(model_url):
225
+ """
226
+ Displays YAML configuration and metadata for a given model URL.
227
+ """
228
+ return scrape_model_page(model_url)
229
+
230
 
231
  # Gradio app
232
  with gr.Blocks() as demo:
 
234
 
235
  with gr.Row():
236
  btn1 = gr.Button("Show Average Performance")
237
+ img1 = gr.Image(type="pil", label="Average Performance Plot",source="upload")
238
  img1_download = gr.File(label="Download Average Performance")
239
  btn1.click(plot_average_scores, outputs=[img1,img1_download])
240
 
241
  with gr.Row():
242
  btn2 = gr.Button("Show Task Performance")
243
+ img2 = gr.Image(type="pil", label="Task Performance Plot", source="upload")
244
  img2_download = gr.File(label="Download Task Performance")
245
  btn2.click(plot_task_performance, outputs=[img2, img2_download])
246
 
247
  with gr.Row():
248
  btn3 = gr.Button("Task-Specific Top Models")
249
+ img3 = gr.Image(type="pil", label="Task-Specific Top Models Plot", source="upload")
250
  img3_download = gr.File(label="Download Top Models")
251
  btn3.click(plot_task_specific_top_models, outputs=[img3, img3_download])
252
 
253
  with gr.Row():
254
  btn4 = gr.Button("Plot Performance Heatmap")
255
+ heatmap_img = gr.Image(type="pil", label="Performance Heatmap", source="upload")
256
  heatmap_download = gr.File(label="Download Heatmap")
257
  btn4.click(plot_heatmap, outputs=[heatmap_img, heatmap_download])
258
 
 
272
  download_all_btn = gr.Button("Download Everything")
273
  all_downloads = gr.File(label="Download All Data")
274
  download_all_btn.click(download_all_data, outputs=all_downloads)
275
+
276
+ # Live scraping feature
277
+ gr.Markdown("## Live Scraping Features")
278
+ with gr.Row():
279
+ url_input = gr.Textbox(label="Enter Hugging Face Model URL", placeholder="https://huggingface.co/<model>")
280
+ live_scrape_btn = gr.Button("Scrape Model Page")
281
+ live_scrape_output = gr.Textbox(label="Scraped Data", lines=15)
282
+ live_scrape_btn.click(display_scraped_model_data, inputs=url_input, outputs=live_scrape_output)
283
 
284
 
285
  demo.launch()