Felguk commited on
Commit
8b34ee3
·
verified ·
1 Parent(s): 6c68f28

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -34
app.py CHANGED
@@ -123,6 +123,9 @@ async def fetch_model_file_content(model_url, file_path):
123
  try:
124
  # Construct the full URL to the file
125
  if "huggingface.co" in model_url:
 
 
 
126
  # Hugging Face URL format: https://huggingface.co/{model}/raw/main/{file_path}
127
  full_url = f"{model_url}/raw/main/{file_path}"
128
  elif "github.com" in model_url:
@@ -138,6 +141,24 @@ async def fetch_model_file_content(model_url, file_path):
138
  except Exception as e:
139
  return f"Error: {e}"
140
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
  # HTML and JavaScript for the "Copy Code" button
142
  copy_button_html = """
143
  <script>
@@ -176,25 +197,6 @@ with gr.Blocks(css=css) as demo:
176
  gr.HTML("<button onclick='copyCode(\"output-text\")'>Copy Code</button>") # Add the "Copy Code" button
177
  file_output = gr.File(label="Download File", visible=False) # Hidden file download component
178
 
179
- # Добавляем примеры для URL to Text Converter
180
- examples_url = [
181
- ["https://huggingface.co"],
182
- ["https://github.com"],
183
- ["https://gradio.app"]
184
- ]
185
- gr.Examples(
186
- examples=examples_url,
187
- inputs=url_input,
188
- outputs=[
189
- results_output, text_output, file_output,
190
- gr.Textbox(label="CSS Files"), gr.Textbox(label="JS Files"), gr.Textbox(label="Images"),
191
- gr.Textbox(label="CSS Content"), gr.Textbox(label="JS Content")
192
- ],
193
- fn=convert_to_text,
194
- cache_examples=True,
195
- label="Примеры URL"
196
- )
197
-
198
  submit_button = gr.Button("Fetch Content")
199
  submit_button.click(
200
  fn=convert_to_text,
@@ -242,21 +244,6 @@ with gr.Blocks(css=css) as demo:
242
  with gr.Row():
243
  gr.HTML("<button onclick='copyCode(\"model-content-output\")'>Copy Code</button>") # Add the "Copy Code" button
244
 
245
- # Добавляем примеры для Model to Text Converter
246
- examples_model = [
247
- ["https://huggingface.co/microsoft/phi-2", "README.md"],
248
- ["https://huggingface.co/openai/whisper-large-v3", "config.json"],
249
- ["https://huggingface.co/stabilityai/stable-diffusion-2", "README.md"]
250
- ]
251
- gr.Examples(
252
- examples=examples_model,
253
- inputs=[model_url_input, file_path_input],
254
- outputs=[model_description_output, install_instructions_output, model_content_output],
255
- fn=lambda url, path: asyncio.run(fetch_model_info(url)) + (asyncio.run(fetch_model_file_content(url, path)),),
256
- cache_examples=True,
257
- label="Примеры моделей"
258
- )
259
-
260
  submit_model_button = gr.Button("Fetch Model Info and File Content")
261
  submit_model_button.click(
262
  fn=fetch_model_info,
@@ -269,5 +256,27 @@ with gr.Blocks(css=css) as demo:
269
  outputs=[model_content_output]
270
  )
271
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
272
  # Launch the interface
273
  demo.launch()
 
123
  try:
124
  # Construct the full URL to the file
125
  if "huggingface.co" in model_url:
126
+ # Убираем /blob/main/ из URL, если он есть
127
+ if "/blob/main/" in model_url:
128
+ model_url = model_url.replace("/blob/main/", "/")
129
  # Hugging Face URL format: https://huggingface.co/{model}/raw/main/{file_path}
130
  full_url = f"{model_url}/raw/main/{file_path}"
131
  elif "github.com" in model_url:
 
141
  except Exception as e:
142
  return f"Error: {e}"
143
 
144
+ # Space to Text Converter
145
+ async def fetch_space_file_content(space_url, file_path):
146
+ """Fetches the content of a file from a Hugging Face Space."""
147
+ try:
148
+ # Construct the full URL to the file
149
+ if "huggingface.co/spaces" in space_url:
150
+ # Hugging Face Spaces URL format: https://huggingface.co/spaces/{user}/{space}/raw/main/{file_path}
151
+ full_url = f"{space_url}/raw/main/{file_path}"
152
+ else:
153
+ return "Error: Unsupported repository. Please provide a Hugging Face Space URL."
154
+
155
+ # Fetch the file content
156
+ response = await asyncio.to_thread(requests.get, full_url, timeout=5)
157
+ response.raise_for_status()
158
+ return response.text
159
+ except Exception as e:
160
+ return f"Error: {e}"
161
+
162
  # HTML and JavaScript for the "Copy Code" button
163
  copy_button_html = """
164
  <script>
 
197
  gr.HTML("<button onclick='copyCode(\"output-text\")'>Copy Code</button>") # Add the "Copy Code" button
198
  file_output = gr.File(label="Download File", visible=False) # Hidden file download component
199
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
200
  submit_button = gr.Button("Fetch Content")
201
  submit_button.click(
202
  fn=convert_to_text,
 
244
  with gr.Row():
245
  gr.HTML("<button onclick='copyCode(\"model-content-output\")'>Copy Code</button>") # Add the "Copy Code" button
246
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
247
  submit_model_button = gr.Button("Fetch Model Info and File Content")
248
  submit_model_button.click(
249
  fn=fetch_model_info,
 
256
  outputs=[model_content_output]
257
  )
258
 
259
+ # Tab 3: Space to Text Converter
260
+ with gr.Tab("Space to Text Converter"):
261
+ gr.Markdown("## Space to Text Converter")
262
+ gr.Markdown("Enter a link to a Hugging Face Space and specify the file path to fetch its content.")
263
+
264
+ with gr.Row():
265
+ space_url_input = gr.Textbox(label="Space URL", placeholder="https://huggingface.co/spaces/...")
266
+ space_file_path_input = gr.Textbox(label="File Path", placeholder="e.g., app.py or README.md")
267
+
268
+ with gr.Row():
269
+ space_content_output = gr.Textbox(label="File Content", interactive=True, elem_id="space-content-output")
270
+
271
+ with gr.Row():
272
+ gr.HTML("<button onclick='copyCode(\"space-content-output\")'>Copy Code</button>") # Add the "Copy Code" button
273
+
274
+ submit_space_button = gr.Button("Fetch File Content")
275
+ submit_space_button.click(
276
+ fn=fetch_space_file_content,
277
+ inputs=[space_url_input, space_file_path_input],
278
+ outputs=[space_content_output]
279
+ )
280
+
281
  # Launch the interface
282
  demo.launch()