Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,466 +1,411 @@
|
|
1 |
-
|
2 |
-
import
|
3 |
-
import
|
4 |
-
|
5 |
-
from
|
6 |
-
|
7 |
-
|
8 |
-
from fastapi
|
9 |
-
|
10 |
-
import
|
11 |
-
from pydantic import BaseModel
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
#
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
"
|
26 |
-
"
|
27 |
-
"
|
28 |
-
"
|
29 |
-
"
|
30 |
-
"
|
31 |
-
"
|
32 |
-
"
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
"
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
"
|
47 |
-
"
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
"
|
54 |
-
"
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
"
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
"
|
374 |
-
|
375 |
-
|
376 |
-
|
377 |
-
|
378 |
-
|
379 |
-
|
380 |
-
|
381 |
-
|
382 |
-
|
383 |
-
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
-
|
389 |
-
|
390 |
-
|
391 |
-
|
392 |
-
|
393 |
-
|
394 |
-
def
|
395 |
-
"""
|
396 |
-
|
397 |
-
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
|
402 |
-
|
403 |
-
|
404 |
-
|
405 |
-
|
406 |
-
|
407 |
-
|
408 |
-
|
409 |
-
|
410 |
-
|
411 |
-
|
412 |
-
value="burtenshaw/play-mcp-repo-bot",
|
413 |
-
placeholder="username/repo-name (can be model, dataset, or space)",
|
414 |
-
)
|
415 |
-
sim_title = gr.Textbox(
|
416 |
-
label="Discussion Title",
|
417 |
-
value="Add pytorch tag",
|
418 |
-
placeholder="Discussion title",
|
419 |
-
)
|
420 |
-
sim_comment = gr.Textbox(
|
421 |
-
label="Comment",
|
422 |
-
lines=3,
|
423 |
-
value="This repository should have tags: pytorch, text-generation",
|
424 |
-
placeholder="Comment mentioning tags...",
|
425 |
-
)
|
426 |
-
sim_btn = gr.Button("π·οΈ Test Tag Detection")
|
427 |
-
|
428 |
-
with gr.Column():
|
429 |
-
sim_result = gr.Textbox(label="Result", lines=8)
|
430 |
-
|
431 |
-
sim_btn.click(
|
432 |
-
fn=simulate_webhook,
|
433 |
-
inputs=[sim_repo, sim_title, sim_comment],
|
434 |
-
outputs=sim_result,
|
435 |
-
)
|
436 |
-
|
437 |
-
gr.Markdown(f"""
|
438 |
-
## Recognized Tags:
|
439 |
-
{", ".join(sorted(RECOGNIZED_TAGS))}
|
440 |
-
""")
|
441 |
-
|
442 |
-
# Add recent operations section
|
443 |
-
if tag_operations_store:
|
444 |
-
gr.Markdown("## Recent Operations")
|
445 |
-
for op in tag_operations_store[-5:]: # Show last 5 operations
|
446 |
-
gr.Markdown(f"""
|
447 |
-
**{op['repo']}** - {op['timestamp'][:19]}
|
448 |
-
- Tags: {', '.join(op['detected_tags'])}
|
449 |
-
- Results: {' | '.join(op['results'][:2])}...
|
450 |
-
""")
|
451 |
-
|
452 |
-
return demo
|
453 |
-
|
454 |
-
|
455 |
-
# Mount Gradio app
|
456 |
-
gradio_app = create_gradio_app()
|
457 |
-
app = gr.mount_gradio_app(app, gradio_app, path="/gradio")
|
458 |
-
|
459 |
-
|
460 |
-
if __name__ == "__main__":
|
461 |
-
print("π Starting HF Tagging Bot...")
|
462 |
-
print(f"π Dashboard: http://localhost:7860/gradio")
|
463 |
-
print(f"π Webhook: http://localhost:7860/webhook")
|
464 |
-
print(f"π HF_TOKEN configured: {bool(HF_TOKEN)}")
|
465 |
-
print("π§ Using direct HuggingFace Hub API (Windows compatible)")
|
466 |
-
uvicorn.run("app:app", host="0.0.0.0", port=7860, reload=True)
|
|
|
1 |
+
# app.py - Main application file for HF Spaces
|
2 |
+
import os
|
3 |
+
import re
|
4 |
+
import json
|
5 |
+
from datetime import datetime
|
6 |
+
from typing import List, Dict, Any, Optional
|
7 |
+
|
8 |
+
from fastapi import FastAPI, Request, BackgroundTasks
|
9 |
+
from fastapi.middleware.cors import CORSMiddleware
|
10 |
+
import gradio as gr
|
11 |
+
from pydantic import BaseModel
|
12 |
+
|
13 |
+
# Configuration - Use HF Spaces secrets
|
14 |
+
WEBHOOK_SECRET = os.getenv("WEBHOOK_SECRET")
|
15 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
16 |
+
|
17 |
+
# Simple storage for processed tag operations
|
18 |
+
tag_operations_store: List[Dict[str, Any]] = []
|
19 |
+
|
20 |
+
# Common ML tags that we recognize for auto-tagging
|
21 |
+
RECOGNIZED_TAGS = {
|
22 |
+
"pytorch", "tensorflow", "jax", "transformers", "diffusers",
|
23 |
+
"text-generation", "text-classification", "question-answering",
|
24 |
+
"text-to-image", "image-classification", "object-detection",
|
25 |
+
"fill-mask", "token-classification", "translation", "summarization",
|
26 |
+
"feature-extraction", "sentence-similarity", "zero-shot-classification",
|
27 |
+
"image-to-text", "automatic-speech-recognition", "audio-classification",
|
28 |
+
"voice-activity-detection", "depth-estimation", "image-segmentation",
|
29 |
+
"video-classification", "reinforcement-learning", "tabular-classification",
|
30 |
+
"tabular-regression", "time-series-forecasting", "graph-ml", "robotics",
|
31 |
+
"computer-vision", "nlp", "cv", "multimodal", "gguf", "safetensors",
|
32 |
+
"llamacpp", "onnx", "mlx"
|
33 |
+
}
|
34 |
+
|
35 |
+
class WebhookEvent(BaseModel):
|
36 |
+
event: Dict[str, str]
|
37 |
+
comment: Dict[str, Any]
|
38 |
+
discussion: Dict[str, Any]
|
39 |
+
repo: Dict[str, str]
|
40 |
+
|
41 |
+
def extract_tags_from_text(text: str) -> List[str]:
|
42 |
+
"""Extract potential tags from discussion text"""
|
43 |
+
text_lower = text.lower()
|
44 |
+
explicit_tags = []
|
45 |
+
|
46 |
+
# Pattern 1: "tag: something" or "tags: something"
|
47 |
+
tag_pattern = r"tags?:\s*([a-zA-Z0-9-_,\s]+)"
|
48 |
+
matches = re.findall(tag_pattern, text_lower)
|
49 |
+
for match in matches:
|
50 |
+
tags = [tag.strip() for tag in match.split(",")]
|
51 |
+
explicit_tags.extend(tags)
|
52 |
+
|
53 |
+
# Pattern 2: "#hashtag" style
|
54 |
+
hashtag_pattern = r"#([a-zA-Z0-9-_]+)"
|
55 |
+
hashtag_matches = re.findall(hashtag_pattern, text_lower)
|
56 |
+
explicit_tags.extend(hashtag_matches)
|
57 |
+
|
58 |
+
# Pattern 3: Look for recognized tags mentioned in natural text
|
59 |
+
mentioned_tags = []
|
60 |
+
for tag in RECOGNIZED_TAGS:
|
61 |
+
if tag in text_lower:
|
62 |
+
mentioned_tags.append(tag)
|
63 |
+
|
64 |
+
# Combine and deduplicate
|
65 |
+
all_tags = list(set(explicit_tags + mentioned_tags))
|
66 |
+
|
67 |
+
# Filter to only include recognized tags or explicitly mentioned ones
|
68 |
+
valid_tags = []
|
69 |
+
for tag in all_tags:
|
70 |
+
if tag in RECOGNIZED_TAGS or tag in explicit_tags:
|
71 |
+
valid_tags.append(tag)
|
72 |
+
|
73 |
+
return valid_tags
|
74 |
+
|
75 |
+
async def process_tags_directly(all_tags: List[str], repo_name: str) -> List[str]:
|
76 |
+
"""Process tags using direct HuggingFace Hub API calls"""
|
77 |
+
print("π§ Using direct HuggingFace Hub API approach...")
|
78 |
+
result_messages = []
|
79 |
+
|
80 |
+
if not HF_TOKEN:
|
81 |
+
error_msg = "No HF_TOKEN configured"
|
82 |
+
print(f"β {error_msg}")
|
83 |
+
return [error_msg]
|
84 |
+
|
85 |
+
try:
|
86 |
+
from huggingface_hub import HfApi, model_info, dataset_info, space_info, ModelCard, ModelCardData
|
87 |
+
from huggingface_hub.utils import HfHubHTTPError
|
88 |
+
from huggingface_hub import CommitOperationAdd
|
89 |
+
|
90 |
+
hf_api = HfApi(token=HF_TOKEN)
|
91 |
+
|
92 |
+
# First, determine what type of repository this is
|
93 |
+
repo_type = None
|
94 |
+
repo_info = None
|
95 |
+
|
96 |
+
# Try different repository types
|
97 |
+
for repo_type_to_try in ["model", "dataset", "space"]:
|
98 |
+
try:
|
99 |
+
print(f"π Trying to access {repo_name} as {repo_type_to_try}...")
|
100 |
+
if repo_type_to_try == "model":
|
101 |
+
repo_info = model_info(repo_id=repo_name, token=HF_TOKEN)
|
102 |
+
elif repo_type_to_try == "dataset":
|
103 |
+
repo_info = dataset_info(repo_id=repo_name, token=HF_TOKEN)
|
104 |
+
elif repo_type_to_try == "space":
|
105 |
+
repo_info = space_info(repo_id=repo_name, token=HF_TOKEN)
|
106 |
+
|
107 |
+
repo_type = repo_type_to_try
|
108 |
+
print(f"β
Found repository as {repo_type}")
|
109 |
+
break
|
110 |
+
|
111 |
+
except HfHubHTTPError as e:
|
112 |
+
if "404" in str(e):
|
113 |
+
continue
|
114 |
+
else:
|
115 |
+
print(f"β Error accessing as {repo_type_to_try}: {e}")
|
116 |
+
continue
|
117 |
+
except Exception as e:
|
118 |
+
print(f"β Unexpected error for {repo_type_to_try}: {e}")
|
119 |
+
continue
|
120 |
+
|
121 |
+
if not repo_type or not repo_info:
|
122 |
+
error_msg = f"Repository '{repo_name}' not found"
|
123 |
+
return [f"Error: {error_msg}"]
|
124 |
+
|
125 |
+
current_tags = repo_info.tags if repo_info.tags else []
|
126 |
+
print(f"π·οΈ Current tags: {current_tags}")
|
127 |
+
|
128 |
+
# Process each tag
|
129 |
+
for tag in all_tags:
|
130 |
+
try:
|
131 |
+
if tag in current_tags:
|
132 |
+
msg = f"Tag '{tag}': Already exists"
|
133 |
+
result_messages.append(msg)
|
134 |
+
continue
|
135 |
+
|
136 |
+
# Add the new tag
|
137 |
+
updated_tags = current_tags + [tag]
|
138 |
+
|
139 |
+
# Create/update model card
|
140 |
+
try:
|
141 |
+
card = ModelCard.load(repo_name, token=HF_TOKEN, repo_type=repo_type)
|
142 |
+
if not hasattr(card, "data") or card.data is None:
|
143 |
+
card.data = ModelCardData()
|
144 |
+
except HfHubHTTPError:
|
145 |
+
card = ModelCard("")
|
146 |
+
card.data = ModelCardData()
|
147 |
+
|
148 |
+
# Update tags
|
149 |
+
card_dict = card.data.to_dict()
|
150 |
+
card_dict["tags"] = updated_tags
|
151 |
+
card.data = ModelCardData(**card_dict)
|
152 |
+
|
153 |
+
pr_title = f"Add '{tag}' tag"
|
154 |
+
pr_description = f"""
|
155 |
+
## Add tag: {tag}
|
156 |
+
|
157 |
+
This PR adds the `{tag}` tag to the {repo_type} repository.
|
158 |
+
|
159 |
+
**Changes:**
|
160 |
+
- Added `{tag}` to {repo_type} tags
|
161 |
+
- Updated from {len(current_tags)} to {len(updated_tags)} tags
|
162 |
+
|
163 |
+
**Current tags:** {", ".join(current_tags) if current_tags else "None"}
|
164 |
+
**New tags:** {", ".join(updated_tags)}
|
165 |
+
|
166 |
+
*This PR was created automatically by the HF Tagging Bot.*
|
167 |
+
"""
|
168 |
+
|
169 |
+
commit_info = hf_api.create_commit(
|
170 |
+
repo_id=repo_name,
|
171 |
+
repo_type=repo_type,
|
172 |
+
operations=[
|
173 |
+
CommitOperationAdd(
|
174 |
+
path_in_repo="README.md",
|
175 |
+
path_or_fileobj=str(card).encode("utf-8")
|
176 |
+
)
|
177 |
+
],
|
178 |
+
commit_message=pr_title,
|
179 |
+
commit_description=pr_description,
|
180 |
+
token=HF_TOKEN,
|
181 |
+
create_pr=True,
|
182 |
+
)
|
183 |
+
|
184 |
+
pr_url = getattr(commit_info, 'pr_url', str(commit_info))
|
185 |
+
msg = f"Tag '{tag}': [PR created]({pr_url})"
|
186 |
+
result_messages.append(msg)
|
187 |
+
|
188 |
+
except Exception as tag_error:
|
189 |
+
error_msg = f"Tag '{tag}': Error - {str(tag_error)}"
|
190 |
+
result_messages.append(error_msg)
|
191 |
+
|
192 |
+
return result_messages
|
193 |
+
|
194 |
+
except Exception as e:
|
195 |
+
error_msg = f"Processing failed: {str(e)}"
|
196 |
+
return [error_msg]
|
197 |
+
|
198 |
+
async def process_webhook_comment(webhook_data: Dict[str, Any]):
|
199 |
+
"""Process webhook to detect and add tags"""
|
200 |
+
try:
|
201 |
+
comment_content = webhook_data["comment"]["content"]
|
202 |
+
discussion_title = webhook_data["discussion"]["title"]
|
203 |
+
repo_name = webhook_data["repo"]["name"]
|
204 |
+
discussion_num = webhook_data["discussion"]["num"]
|
205 |
+
comment_author = webhook_data["comment"]["author"].get("id", "unknown")
|
206 |
+
|
207 |
+
print(f"π Processing comment from {comment_author} on {repo_name}")
|
208 |
+
print(f"π Comment: {comment_content}")
|
209 |
+
|
210 |
+
# Extract tags
|
211 |
+
comment_tags = extract_tags_from_text(comment_content)
|
212 |
+
title_tags = extract_tags_from_text(discussion_title)
|
213 |
+
all_tags = list(set(comment_tags + title_tags))
|
214 |
+
|
215 |
+
if not all_tags:
|
216 |
+
return "No recognizable tags found"
|
217 |
+
|
218 |
+
print(f"π·οΈ Found tags: {all_tags}")
|
219 |
+
result_messages = await process_tags_directly(all_tags, repo_name)
|
220 |
+
|
221 |
+
# Store interaction
|
222 |
+
interaction = {
|
223 |
+
"timestamp": datetime.now().isoformat(),
|
224 |
+
"repo": repo_name,
|
225 |
+
"discussion_title": discussion_title,
|
226 |
+
"discussion_num": discussion_num,
|
227 |
+
"comment_author": comment_author,
|
228 |
+
"detected_tags": all_tags,
|
229 |
+
"results": result_messages,
|
230 |
+
}
|
231 |
+
tag_operations_store.append(interaction)
|
232 |
+
|
233 |
+
# Keep only last 100 operations to prevent memory issues
|
234 |
+
if len(tag_operations_store) > 100:
|
235 |
+
tag_operations_store.pop(0)
|
236 |
+
|
237 |
+
return " | ".join(result_messages)
|
238 |
+
|
239 |
+
except Exception as e:
|
240 |
+
error_msg = f"Error processing webhook: {str(e)}"
|
241 |
+
print(f"β {error_msg}")
|
242 |
+
return error_msg
|
243 |
+
|
244 |
+
def create_gradio_interface():
|
245 |
+
"""Create Gradio interface for monitoring"""
|
246 |
+
with gr.Blocks(title="HF Tagging Bot", theme=gr.themes.Soft()) as demo:
|
247 |
+
gr.Markdown("# π·οΈ HuggingFace Tagging Bot")
|
248 |
+
gr.Markdown("*Automatically adds tags to repositories when mentioned in discussions*")
|
249 |
+
|
250 |
+
with gr.Tab("π Home"):
|
251 |
+
gr.Markdown(f"""
|
252 |
+
## Bot Status
|
253 |
+
- π **HF Token**: {'β
Configured' if HF_TOKEN else 'β Missing'}
|
254 |
+
- π **Webhook Secret**: {'β
Configured' if WEBHOOK_SECRET else 'β Missing'}
|
255 |
+
- π **Operations Processed**: {len(tag_operations_store)}
|
256 |
+
|
257 |
+
## How to Use
|
258 |
+
1. **Configure webhook** in your repository settings:
|
259 |
+
- Webhook URL: `{os.getenv('SPACE_ID', 'your-space')}.hf.space/webhook`
|
260 |
+
- Events: Discussion comments
|
261 |
+
- Secret: Your webhook secret (optional)
|
262 |
+
|
263 |
+
2. **In discussions, mention tags like**:
|
264 |
+
- "Please add tags: pytorch, transformers"
|
265 |
+
- "This needs #pytorch and #text-generation tags"
|
266 |
+
- "tag: computer-vision"
|
267 |
+
|
268 |
+
3. **Bot will automatically**:
|
269 |
+
- Detect mentioned tags
|
270 |
+
- Create PRs to add missing tags
|
271 |
+
- Post results in logs
|
272 |
+
|
273 |
+
## Webhook Endpoint
|
274 |
+
`POST /{os.getenv('SPACE_ID', 'your-space')}.hf.space/webhook`
|
275 |
+
""")
|
276 |
+
|
277 |
+
with gr.Tab("π Recent Operations"):
|
278 |
+
def get_recent_operations():
|
279 |
+
if not tag_operations_store:
|
280 |
+
return "No operations yet. Waiting for webhook events..."
|
281 |
+
|
282 |
+
recent = tag_operations_store[-10:] # Last 10 operations
|
283 |
+
output = []
|
284 |
+
for op in reversed(recent): # Most recent first
|
285 |
+
output.append(f"""
|
286 |
+
**{op['repo']}** - {op['timestamp'][:19]}
|
287 |
+
- π€ Author: {op['comment_author']}
|
288 |
+
- π·οΈ Tags: {', '.join(op['detected_tags']) if op['detected_tags'] else 'None'}
|
289 |
+
- π Results: {' | '.join(op['results'][:2])}{'...' if len(op['results']) > 2 else ''}
|
290 |
+
---""")
|
291 |
+
return "\n".join(output)
|
292 |
+
|
293 |
+
operations_display = gr.Textbox(
|
294 |
+
label="Recent Operations",
|
295 |
+
value=get_recent_operations(),
|
296 |
+
lines=15,
|
297 |
+
interactive=False
|
298 |
+
)
|
299 |
+
|
300 |
+
refresh_btn = gr.Button("π Refresh")
|
301 |
+
refresh_btn.click(fn=get_recent_operations, outputs=operations_display)
|
302 |
+
|
303 |
+
with gr.Tab("π·οΈ Supported Tags"):
|
304 |
+
gr.Markdown(f"""
|
305 |
+
## Recognized Tags ({len(RECOGNIZED_TAGS)} total):
|
306 |
+
|
307 |
+
{', '.join(sorted(RECOGNIZED_TAGS))}
|
308 |
+
|
309 |
+
## Tag Detection Patterns:
|
310 |
+
- **Explicit**: `tag: pytorch` or `tags: pytorch, transformers`
|
311 |
+
- **Hashtag**: `#pytorch #transformers`
|
312 |
+
- **Natural text**: Any mention of recognized tags in text
|
313 |
+
|
314 |
+
## Examples:
|
315 |
+
- β
"This model uses pytorch and transformers"
|
316 |
+
- β
"Please add tags: computer-vision, object-detection"
|
317 |
+
- β
"Missing #pytorch #text-generation tags"
|
318 |
+
- β
"tag: nlp"
|
319 |
+
""")
|
320 |
+
|
321 |
+
with gr.Tab("π§ Test"):
|
322 |
+
gr.Markdown("### Test Tag Detection")
|
323 |
+
test_input = gr.Textbox(
|
324 |
+
label="Test Comment",
|
325 |
+
placeholder="Enter a comment to test tag detection...",
|
326 |
+
lines=3
|
327 |
+
)
|
328 |
+
test_output = gr.Textbox(
|
329 |
+
label="Detected Tags",
|
330 |
+
lines=2,
|
331 |
+
interactive=False
|
332 |
+
)
|
333 |
+
test_btn = gr.Button("π Test Detection")
|
334 |
+
|
335 |
+
def test_tag_detection(text):
|
336 |
+
if not text:
|
337 |
+
return "Enter some text to test"
|
338 |
+
tags = extract_tags_from_text(text)
|
339 |
+
if tags:
|
340 |
+
return f"Found tags: {', '.join(tags)}"
|
341 |
+
else:
|
342 |
+
return "No tags detected"
|
343 |
+
|
344 |
+
test_btn.click(fn=test_tag_detection, inputs=test_input, outputs=test_output)
|
345 |
+
|
346 |
+
return demo
|
347 |
+
|
348 |
+
# Create FastAPI app for webhook handling
|
349 |
+
app = FastAPI(title="HF Tagging Bot API")
|
350 |
+
app.add_middleware(CORSMiddleware, allow_origins=["*"])
|
351 |
+
|
352 |
+
@app.post("/webhook")
|
353 |
+
async def webhook_handler(request: Request, background_tasks: BackgroundTasks):
|
354 |
+
"""Handle HF Hub webhooks"""
|
355 |
+
# Verify webhook secret if configured
|
356 |
+
if WEBHOOK_SECRET:
|
357 |
+
webhook_secret = request.headers.get("X-Webhook-Secret")
|
358 |
+
if webhook_secret != WEBHOOK_SECRET:
|
359 |
+
print("β Invalid webhook secret")
|
360 |
+
return {"error": "Invalid webhook secret"}
|
361 |
+
|
362 |
+
try:
|
363 |
+
payload = await request.json()
|
364 |
+
print(f"π₯ Received webhook: {payload.get('event', {})}")
|
365 |
+
|
366 |
+
event = payload.get("event", {})
|
367 |
+
scope = event.get("scope")
|
368 |
+
action = event.get("action")
|
369 |
+
|
370 |
+
# Only process discussion comment creation (not PRs)
|
371 |
+
if (scope == "discussion" and
|
372 |
+
action == "create" and
|
373 |
+
not payload.get("discussion", {}).get("isPullRequest", False)):
|
374 |
+
|
375 |
+
background_tasks.add_task(process_webhook_comment, payload)
|
376 |
+
return {"status": "processing"}
|
377 |
+
|
378 |
+
return {"status": "ignored"}
|
379 |
+
except Exception as e:
|
380 |
+
print(f"β Webhook error: {e}")
|
381 |
+
return {"error": str(e)}
|
382 |
+
|
383 |
+
# Health check endpoint
|
384 |
+
@app.get("/health")
|
385 |
+
async def health_check():
|
386 |
+
return {
|
387 |
+
"status": "healthy",
|
388 |
+
"hf_token_configured": bool(HF_TOKEN),
|
389 |
+
"webhook_secret_configured": bool(WEBHOOK_SECRET),
|
390 |
+
"operations_processed": len(tag_operations_store)
|
391 |
+
}
|
392 |
+
|
393 |
+
@app.get("/")
|
394 |
+
async def root():
|
395 |
+
return {"message": "HF Tagging Bot is running! Visit /gradio for the interface."}
|
396 |
+
|
397 |
+
# Create and mount Gradio interface
|
398 |
+
gradio_app = create_gradio_interface()
|
399 |
+
app = gr.mount_gradio_app(app, gradio_app, path="/gradio")
|
400 |
+
|
401 |
+
# For HF Spaces, we also need to make the Gradio interface the default
|
402 |
+
demo = gradio_app
|
403 |
+
|
404 |
+
if __name__ == "__main__":
|
405 |
+
# This runs when testing locally
|
406 |
+
import uvicorn
|
407 |
+
port = int(os.getenv("PORT", 7860))
|
408 |
+
print(f"π Starting HF Tagging Bot on port {port}")
|
409 |
+
print(f"π HF_TOKEN: {'β
Configured' if HF_TOKEN else 'β Missing'}")
|
410 |
+
print(f"π Webhook Secret: {'β
Configured' if WEBHOOK_SECRET else 'β Missing'}")
|
411 |
+
uvicorn.run("app:app", host="0.0.0.0", port=port)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|