Felguk commited on
Commit
d81c533
Β·
verified Β·
1 Parent(s): 704e96a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -1
app.py CHANGED
@@ -83,6 +83,41 @@ async def convert_to_text(url):
83
  return f"Error: {e}", "", None, [], [], [], [], [] # Return error message and empty data
84
 
85
  # Model to Text Converter
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
  async def fetch_model_file_content(model_url, file_path):
87
  """Fetches the content of a file from a model repository (Hugging Face or GitHub)."""
88
  try:
@@ -178,13 +213,22 @@ with gr.Blocks(css=css) as demo:
178
  model_url_input = gr.Textbox(label="Model URL", placeholder="https://huggingface.co/... or https://github.com/...")
179
  file_path_input = gr.Textbox(label="File Path", placeholder="e.g., config.json or README.md")
180
 
 
 
 
 
181
  with gr.Row():
182
  model_content_output = gr.Textbox(label="File Content", interactive=True, elem_id="model-content-output")
183
 
184
  with gr.Row():
185
  gr.HTML("<button onclick='copyCode(\"model-content-output\")'>Copy Code</button>") # Add the "Copy Code" button
186
 
187
- submit_model_button = gr.Button("Fetch File Content")
 
 
 
 
 
188
  submit_model_button.click(
189
  fn=fetch_model_file_content,
190
  inputs=[model_url_input, file_path_input],
 
83
  return f"Error: {e}", "", None, [], [], [], [], [] # Return error message and empty data
84
 
85
  # Model to Text Converter
86
+ async def fetch_model_info(model_url):
87
+ """Fetches model description and installation instructions."""
88
+ try:
89
+ if "huggingface.co" in model_url:
90
+ # Fetch model card from Hugging Face
91
+ response = await asyncio.to_thread(requests.get, model_url, timeout=5)
92
+ response.raise_for_status()
93
+ soup = BeautifulSoup(response.text, "html.parser")
94
+
95
+ # Extract model description
96
+ description = soup.find("div", {"class": "prose"}).get_text(strip=True) if soup.find("div", {"class": "prose"}) else "No description available."
97
+
98
+ # Generate installation instructions
99
+ model_name = model_url.split("/")[-1]
100
+ install_instructions = f"To install this model, run:\n```bash\npip install transformers\n```\nThen load the model in Python:\n```python\nfrom transformers import AutoModel, AutoTokenizer\nmodel = AutoModel.from_pretrained('{model_name}')\ntokenizer = AutoTokenizer.from_pretrained('{model_name}')\n```"
101
+
102
+ return description, install_instructions
103
+ elif "github.com" in model_url:
104
+ # Fetch README from GitHub
105
+ readme_url = f"{model_url}/raw/main/README.md"
106
+ response = await asyncio.to_thread(requests.get, readme_url, timeout=5)
107
+ response.raise_for_status()
108
+
109
+ # Extract description from README
110
+ description = response.text if response.text else "No description available."
111
+
112
+ # Generate installation instructions
113
+ install_instructions = f"To install this model, clone the repository:\n```bash\ngit clone {model_url}.git\ncd {model_url.split('/')[-1]}\n```"
114
+
115
+ return description, install_instructions
116
+ else:
117
+ return "Unsupported repository.", ""
118
+ except Exception as e:
119
+ return f"Error: {e}", ""
120
+
121
  async def fetch_model_file_content(model_url, file_path):
122
  """Fetches the content of a file from a model repository (Hugging Face or GitHub)."""
123
  try:
 
213
  model_url_input = gr.Textbox(label="Model URL", placeholder="https://huggingface.co/... or https://github.com/...")
214
  file_path_input = gr.Textbox(label="File Path", placeholder="e.g., config.json or README.md")
215
 
216
+ with gr.Row():
217
+ model_description_output = gr.Textbox(label="Model Description", interactive=False)
218
+ install_instructions_output = gr.Textbox(label="Installation Instructions", interactive=False)
219
+
220
  with gr.Row():
221
  model_content_output = gr.Textbox(label="File Content", interactive=True, elem_id="model-content-output")
222
 
223
  with gr.Row():
224
  gr.HTML("<button onclick='copyCode(\"model-content-output\")'>Copy Code</button>") # Add the "Copy Code" button
225
 
226
+ submit_model_button = gr.Button("Fetch Model Info and File Content")
227
+ submit_model_button.click(
228
+ fn=fetch_model_info,
229
+ inputs=[model_url_input],
230
+ outputs=[model_description_output, install_instructions_output]
231
+ )
232
  submit_model_button.click(
233
  fn=fetch_model_file_content,
234
  inputs=[model_url_input, file_path_input],