Update app.py
#1
by
reach-vb
HF Staff
- opened
app.py
CHANGED
|
@@ -1,64 +1,188 @@
|
|
| 1 |
-
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
-
|
| 4 |
"""
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
"""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
response = ""
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
|
|
|
|
|
|
| 2 |
"""
|
| 3 |
+
gradio_app.py
|
| 4 |
+
--------------
|
| 5 |
+
Gradio application (with MCP support) exposing the functionality of
|
| 6 |
+
`extract_readme.py` as an interactive tool. After launching, the app can be
|
| 7 |
+
used via a regular web UI *or* programmatically by any MCP-compatible LLM
|
| 8 |
+
client (Cursor, Claude Desktop, etc.).
|
| 9 |
+
|
| 10 |
+
Run locally:
|
| 11 |
+
python gradio_app.py
|
| 12 |
+
|
| 13 |
+
This will start both the Gradio web server *and* the MCP endpoint. The latter
|
| 14 |
+
is announced in the terminal when the app starts.
|
| 15 |
"""
|
|
|
|
| 16 |
|
| 17 |
+
from __future__ import annotations
|
| 18 |
|
| 19 |
+
import os
|
| 20 |
+
import re
|
| 21 |
+
import time
|
| 22 |
+
from types import TracebackType
|
| 23 |
+
from typing import Any, List, Sequence, Tuple, Type
|
| 24 |
+
from urllib.parse import urlparse
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
import gradio as gr
|
| 27 |
+
import requests
|
| 28 |
+
from huggingface_hub import HfApi, InferenceClient, ModelCard # type: ignore
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
# -----------------------------------------------------------------------------
|
| 31 |
+
# Core logic (adapted from extract_readme.py)
|
| 32 |
+
# -----------------------------------------------------------------------------
|
| 33 |
|
|
|
|
| 34 |
|
| 35 |
+
def _extract_urls(text: str) -> List[str]:
|
| 36 |
+
"""Return a list of unique URLs found inside *text* preserving order."""
|
| 37 |
+
url_pattern = re.compile(r"https?://[^\s\)\]\>'\"`]+")
|
| 38 |
+
urls = url_pattern.findall(text)
|
| 39 |
+
# Preserve insertion order while removing duplicates.
|
| 40 |
+
seen: set[str] = set()
|
| 41 |
+
unique_urls: List[str] = []
|
| 42 |
+
for u in urls:
|
| 43 |
+
if u not in seen:
|
| 44 |
+
unique_urls.append(u)
|
| 45 |
+
seen.add(u)
|
| 46 |
+
return unique_urls
|
| 47 |
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
def _summarise_external_urls(urls: Sequence[str]) -> List[Tuple[str, str]]:
|
| 50 |
+
"""Return a list of (url, summary) tuples using the r.jina.ai proxy."""
|
| 51 |
+
if not urls:
|
| 52 |
+
return []
|
| 53 |
|
| 54 |
+
summaries: List[Tuple[str, str]] = []
|
| 55 |
+
url_pattern = re.compile(r"https?://[^\s\)\]\>'\"`]+")
|
| 56 |
+
|
| 57 |
+
for idx, original_url in enumerate(urls):
|
| 58 |
+
proxy_url = f"https://r.jina.ai/{original_url}"
|
| 59 |
+
try:
|
| 60 |
+
resp = requests.get(proxy_url, timeout=15)
|
| 61 |
+
resp.raise_for_status()
|
| 62 |
+
cleaned_text = url_pattern.sub("", resp.text)
|
| 63 |
+
summaries.append((original_url, cleaned_text))
|
| 64 |
+
except Exception as err: # pylint: disable=broad-except
|
| 65 |
+
summaries.append((original_url, f"β Failed to fetch summary: {err}"))
|
| 66 |
+
# Respect ~15 req/min rate-limit of r.jina.ai
|
| 67 |
+
if idx < len(urls) - 1:
|
| 68 |
+
time.sleep(4.1)
|
| 69 |
+
return summaries
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
# -----------------------------------------------------------------------------
|
| 73 |
+
# Public MCP-exposed function
|
| 74 |
+
# -----------------------------------------------------------------------------
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def extract_model_info(
|
| 78 |
+
model_id: str,
|
| 79 |
+
llm_model_id: str = "CohereLabs/c4ai-command-a-03-2025",
|
| 80 |
+
) -> str:
|
| 81 |
+
"""Fetch a Hugging Face model card, analyse it and optionally summarise it.
|
| 82 |
+
|
| 83 |
+
Args:
|
| 84 |
+
model_id: The *repository ID* of the model on Hugging Face (e.g.
|
| 85 |
+
"bert-base-uncased").
|
| 86 |
+
llm_model_id: ID of the LLM used for summarisation via the Inference
|
| 87 |
+
Endpoint. Defaults to Cohere Command R+.
|
| 88 |
+
open_pr: If *True*, the generated summary will be posted as a **new
|
| 89 |
+
discussion** in the specified model repo. Requires a valid
|
| 90 |
+
`HF_TOKEN` environment variable with write permissions.
|
| 91 |
+
|
| 92 |
+
Returns:
|
| 93 |
+
A single markdown-formatted string containing:
|
| 94 |
+
1. The raw README.
|
| 95 |
+
2. Extracted external URLs.
|
| 96 |
+
3. Brief summaries of the external URLs (via r.jina.ai).
|
| 97 |
+
4. A concise LLM-generated summary of the model card.
|
| 98 |
+
"""
|
| 99 |
+
|
| 100 |
+
# ------------------------------------------------------------------
|
| 101 |
+
# 1. Load model card
|
| 102 |
+
# ------------------------------------------------------------------
|
| 103 |
+
try:
|
| 104 |
+
card = ModelCard.load(model_id)
|
| 105 |
+
except Exception as err: # pylint: disable=broad-except
|
| 106 |
+
return f"β Failed to load model card for '{model_id}': {err}"
|
| 107 |
+
|
| 108 |
+
combined_sections: List[str] = ["=== README markdown ===", card.text]
|
| 109 |
+
|
| 110 |
+
# ------------------------------------------------------------------
|
| 111 |
+
# 2. Extract URLs
|
| 112 |
+
# ------------------------------------------------------------------
|
| 113 |
+
unique_urls = _extract_urls(card.text)
|
| 114 |
+
if unique_urls:
|
| 115 |
+
combined_sections.append("\n=== URLs found ===")
|
| 116 |
+
combined_sections.extend(unique_urls)
|
| 117 |
+
|
| 118 |
+
EXCLUDED_KEYWORDS = ("colab.research.google.com", "github.com")
|
| 119 |
+
filtered_urls = [
|
| 120 |
+
u for u in unique_urls if not any(k in urlparse(u).netloc for k in EXCLUDED_KEYWORDS)
|
| 121 |
+
]
|
| 122 |
+
|
| 123 |
+
if filtered_urls:
|
| 124 |
+
combined_sections.append("\n=== Summaries via r.jina.ai ===")
|
| 125 |
+
for url, summary in _summarise_external_urls(filtered_urls):
|
| 126 |
+
combined_sections.append(f"\n--- {url} ---\n{summary}")
|
| 127 |
+
else:
|
| 128 |
+
combined_sections.append("\nNo external URLs (after filtering) detected in the model card.")
|
| 129 |
+
else:
|
| 130 |
+
combined_sections.append("\nNo URLs detected in the model card.")
|
| 131 |
+
|
| 132 |
+
# ------------------------------------------------------------------
|
| 133 |
+
# 3. Summarise with LLM (if token available)
|
| 134 |
+
# ------------------------------------------------------------------
|
| 135 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 136 |
+
summary_text: str | None = None
|
| 137 |
+
if hf_token:
|
| 138 |
+
client = InferenceClient(provider="auto", api_key=hf_token)
|
| 139 |
+
prompt = (
|
| 140 |
+
"You are given a lot of information about a machine learning model "
|
| 141 |
+
"available on Hugging Face. Create a concise, technical and to the point "
|
| 142 |
+
"summary highlighting the technical details, comparisons and instructions "
|
| 143 |
+
"to run the model (if available). Think of the summary as a gist with all "
|
| 144 |
+
"the information someone should need to know about the model without "
|
| 145 |
+
"overwhelming them. Do not add any text formatting to your output text, "
|
| 146 |
+
"keep it simple and plain text. If you have to then sparingly just use "
|
| 147 |
+
"markdown for Heading and lists. Specifically do not use ** to bold text, "
|
| 148 |
+
"just use # for headings and - for lists. No need to put any contact "
|
| 149 |
+
"information in the summary. The summary is supposed to be insightful and "
|
| 150 |
+
"information dense and should not be more than 200-300 words. Don't "
|
| 151 |
+
"hallucinate and refer only to the content provided to you. Remember to "
|
| 152 |
+
"be concise. Here is the information:\n\n" + "\n".join(combined_sections)
|
| 153 |
+
)
|
| 154 |
+
try:
|
| 155 |
+
completion = client.chat.completions.create(
|
| 156 |
+
model=llm_model_id,
|
| 157 |
+
messages=[{"role": "user", "content": prompt}],
|
| 158 |
+
)
|
| 159 |
+
summary_text = completion.choices[0].message.content
|
| 160 |
+
except Exception as err: # pylint: disable=broad-except
|
| 161 |
+
return f"β Failed to generate summary: {err}"
|
| 162 |
+
else:
|
| 163 |
+
return "β οΈ HF_TOKEN environment variable not set. Please set it to enable summarisation."
|
| 164 |
+
# Return only the summary text if available
|
| 165 |
+
return summary_text or "β Summary generation failed for unknown reasons."
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
# -----------------------------------------------------------------------------
|
| 169 |
+
# Gradio UI & MCP launch
|
| 170 |
+
# -----------------------------------------------------------------------------
|
| 171 |
+
|
| 172 |
+
demo = gr.Interface(
|
| 173 |
+
fn=extract_model_info,
|
| 174 |
+
inputs=[
|
| 175 |
+
gr.Textbox(value="bert-base-uncased", label="Model ID"),
|
| 176 |
+
gr.Textbox(value="CohereLabs/c4ai-command-a-03-2025", label="LLM Model ID"),
|
| 177 |
],
|
| 178 |
+
outputs=gr.Textbox(label="Result", lines=25),
|
| 179 |
+
title="Model Card Inspector & Summariser",
|
| 180 |
+
description=(
|
| 181 |
+
"Fetch a model card from Hugging Face, extract useful links, optionally "
|
| 182 |
+
"summarise it with an LLM and (optionally) open a discussion on the Hub. "
|
| 183 |
+
"This tool is also available via MCP so LLM clients can call it directly."
|
| 184 |
+
),
|
| 185 |
)
|
| 186 |
|
|
|
|
| 187 |
if __name__ == "__main__":
|
| 188 |
+
demo.launch(mcp_server=True)
|