apple muncy commited on
Commit
3842a4e
·
1 Parent(s): bd999cb

restore from original

Browse files

hf -
Signed-off-by: apple muncy <[email protected]>

Files changed (6) hide show
  1. Dockerfile +1 -10
  2. README.md +1 -1
  3. app.py +15 -94
  4. mcp_server.py +2 -10
  5. pyproject.toml +9 -9
  6. requirements.txt +76 -74
Dockerfile CHANGED
@@ -6,8 +6,6 @@ WORKDIR /app
6
  # Install system dependencies
7
  RUN apt-get update && apt-get install -y \
8
  git \
9
- mc \
10
- fonts-dejavu-web \
11
  && rm -rf /var/lib/apt/lists/*
12
 
13
  # Copy project files
@@ -16,7 +14,6 @@ COPY server.py .
16
  COPY mcp_server.py .
17
  COPY env.example .
18
  COPY README.md .
19
- COPY tag.log .
20
 
21
  # Install Python dependencies
22
  RUN pip install --no-cache-dir -e .
@@ -27,16 +24,10 @@ USER appuser
27
 
28
  # Expose port
29
  EXPOSE 8000
30
- EXPOSE 443
31
- EXPOSE 7860
32
- EXPOSE 7861
33
- EXPOSE 7862
34
- EXPOSE 7863
35
 
36
  # Health check
37
  HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
38
  CMD curl -f http://localhost:8000/ || exit 1
39
 
40
- ENTRYPOINT ["python", "app.py"]
41
  # Run the application
42
- CMD ["-c"]
 
6
  # Install system dependencies
7
  RUN apt-get update && apt-get install -y \
8
  git \
 
 
9
  && rm -rf /var/lib/apt/lists/*
10
 
11
  # Copy project files
 
14
  COPY mcp_server.py .
15
  COPY env.example .
16
  COPY README.md .
 
17
 
18
  # Install Python dependencies
19
  RUN pip install --no-cache-dir -e .
 
24
 
25
  # Expose port
26
  EXPOSE 8000
 
 
 
 
 
27
 
28
  # Health check
29
  HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
30
  CMD curl -f http://localhost:8000/ || exit 1
31
 
 
32
  # Run the application
33
+ CMD ["python", "server.py"]
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 👀
4
  colorFrom: purple
5
  colorTo: yellow
6
  sdk: gradio
7
- sdk_version: 5.34.2
8
  app_file: app.py
9
  pinned: false
10
  base_path: /gradio
 
4
  colorFrom: purple
5
  colorTo: yellow
6
  sdk: gradio
7
+ sdk_version: 5.31.0
8
  app_file: app.py
9
  pinned: false
10
  base_path: /gradio
app.py CHANGED
@@ -3,7 +3,7 @@ import re
3
  import json
4
  from datetime import datetime
5
  from typing import List, Dict, Any, Optional, Literal
6
- import copy
7
  from fastapi import FastAPI, Request, BackgroundTasks
8
  from fastapi.middleware.cors import CORSMiddleware
9
  import gradio as gr
@@ -11,14 +11,6 @@ import uvicorn
11
  from pydantic import BaseModel
12
  from huggingface_hub.inference._mcp.agent import Agent
13
  from dotenv import load_dotenv
14
- #add logging
15
- import logging
16
- logger = logging.getLogger(__name__)
17
- logging.basicConfig(filename='/home/user/tag.log', encoding='utf-8', level=logging.DEBUG)
18
- logger.debug('This message should go to the log file')
19
- logger.info('So should this')
20
- logger.warning('And this, too')
21
- logger.error('And non-ASCII stuff, too, like Øresund and Malmö')
22
 
23
  load_dotenv()
24
 
@@ -30,14 +22,6 @@ HF_MODEL = os.getenv("HF_MODEL", "Qwen/Qwen2.5-72B-Instruct")
30
  DEFAULT_PROVIDER: Literal["hf-inference"] = "hf-inference"
31
  HF_PROVIDER = os.getenv("HF_PROVIDER", DEFAULT_PROVIDER)
32
 
33
- #global variables for gradio display
34
- g_payload=[]
35
-
36
- g_payload.append("Space holder")
37
-
38
- def fn_payload(g_payload):
39
- return g_payload
40
-
41
  # Simple storage for processed tag operations
42
  tag_operations_store: List[Dict[str, Any]] = []
43
 
@@ -81,7 +65,7 @@ RECOGNIZED_TAGS = {
81
  "computer-vision",
82
  "nlp",
83
  "cv",
84
- "multimodal"
85
  }
86
 
87
 
@@ -92,26 +76,20 @@ class WebhookEvent(BaseModel):
92
  repo: Dict[str, str]
93
 
94
 
95
- my_app = FastAPI(title="HF Tagging Bot")
96
-
97
- @my_app.get("/")
98
- def read_main():
99
- return {"message": "This is your main app"}
100
-
101
- my_app.add_middleware(CORSMiddleware, allow_origins=["*"])
102
 
103
 
104
  async def get_agent():
105
  """Get or create Agent instance"""
106
  print("🤖 get_agent() called...")
107
- logger.info('get_agent() called...')
108
  global agent_instance
109
  if agent_instance is None and HF_TOKEN:
110
  print("🔧 Creating new Agent instance...")
111
  print(f"🔑 HF_TOKEN present: {bool(HF_TOKEN)}")
112
  print(f"🤖 Model: {HF_MODEL}")
113
  print(f"🔗 Provider: {DEFAULT_PROVIDER}")
114
- logger.info('Creating new Agent instance...')
115
  try:
116
  agent_instance = Agent(
117
  model=HF_MODEL,
@@ -130,9 +108,7 @@ async def get_agent():
130
  ],
131
  )
132
  print("✅ Agent instance created successfully")
133
- logger.info('Agent instance created successfully')
134
  print("🔧 Loading tools...")
135
- logger.info('Loading tools...')
136
  await agent_instance.load_tools()
137
  print("✅ Tools loaded successfully")
138
  except Exception as e:
@@ -147,9 +123,6 @@ async def get_agent():
147
 
148
 
149
  def extract_tags_from_text(text: str) -> List[str]:
150
-
151
- g_payload.append(text)
152
- print(f"Tag string: {text}" )
153
  """Extract potential tags from discussion text"""
154
  text_lower = text.lower()
155
 
@@ -182,10 +155,8 @@ def extract_tags_from_text(text: str) -> List[str]:
182
  valid_tags = []
183
  for tag in all_tags:
184
  if tag in RECOGNIZED_TAGS or tag in explicit_tags:
185
- valid_tags.append(tag.replace("'", '"'))
186
 
187
-
188
- print(f"valid tags: {valid_tags}")
189
  return valid_tags
190
 
191
 
@@ -234,7 +205,7 @@ async def process_webhook_comment(webhook_data: Dict[str, Any]):
234
  try:
235
  # Create a comprehensive prompt for the agent
236
  user_prompt = f"""
237
- I need to add the following tags to the repository "{repo_name}": {", ".join(all_tags)}
238
 
239
  For each tag, please:
240
  1. Check if the tag already exists on the repository using get_current_tags
@@ -398,7 +369,7 @@ Please process all {len(all_tags)} tags: {", ".join(all_tags)}
398
  "original_comment": comment_content,
399
  "comment_author": comment_author,
400
  "detected_tags": all_tags,
401
- "results": result_messages
402
  }
403
 
404
  tag_operations_store.append(interaction)
@@ -411,7 +382,8 @@ Please process all {len(all_tags)} tags: {", ".join(all_tags)}
411
  print(error_msg)
412
  return error_msg
413
 
414
- @my_app.post("/webhook")
 
415
  async def webhook_handler(request: Request, background_tasks: BackgroundTasks):
416
  """Handle HF Hub webhooks"""
417
  webhook_secret = request.headers.get("X-Webhook-Secret")
@@ -421,7 +393,7 @@ async def webhook_handler(request: Request, background_tasks: BackgroundTasks):
421
 
422
  payload = await request.json()
423
  print(f"📥 Received webhook payload: {json.dumps(payload, indent=2)}")
424
- g_payload.append(payload)
425
  event = payload.get("event", {})
426
  scope = event.get("scope")
427
  action = event.get("action")
@@ -503,7 +475,7 @@ def create_gradio_app():
503
  - Supports common ML tags like: pytorch, tensorflow,
504
  text-generation, etc.
505
  """)
506
-
507
  with gr.Column():
508
  sim_repo = gr.Textbox(
509
  label="Repository",
@@ -539,65 +511,14 @@ def create_gradio_app():
539
 
540
  return demo
541
 
542
- def create_dashboard():
543
- """Create Gradio interface"""
544
- with gr.Blocks(title="HF Tagging Bot", theme=gr.themes.Soft()) as demo:
545
- gr.Markdown("# 🏷️ HF Tagging Bot Additional Dashboard")
546
- gr.Markdown("*Display MCP data*")
547
-
548
- gr.Markdown("""
549
- ## How it works:
550
- - Monitors HuggingFace Hub discussions
551
- - Detects tag mentions in comments (e.g., "tag: pytorch",
552
- "#transformers")
553
- - Automatically adds recognized tags to the model repository
554
- - Supports common ML tags like: pytorch, tensorflow,
555
- text-generation, etc.
556
- """)
557
- gr.Textbox(
558
- label="Text to extract tag from",
559
- value=g_payload,
560
- placeholder="payload"
561
- )
562
-
563
- with gr.Column():
564
- sim_repo = gr.Textbox(
565
- label="Payload",
566
- value="burtenshaw/play-mcp-repo-bot",
567
- placeholder="username/model-name",
568
- )
569
- sim_title = gr.Textbox(
570
- label="Discussion Title",
571
- value="Add pytorch tag",
572
- placeholder="Discussion title"
573
- )
574
- sim_comment = gr.Textbox(
575
- label="Comment",
576
- lines=3,
577
- value="This model should have tags: pytorch, text-generation",
578
- placeholder="Comment mentioning tags..."
579
- )
580
- sim_btn = gr.Button("🏷️ Test Tag Detection")
581
- gr.Markdown(f"""
582
- ## Recognized Tags:
583
- {g_payload}
584
- """)
585
-
586
-
587
- return demo
588
-
589
-
590
-
591
 
592
  # Mount Gradio app
593
  gradio_app = create_gradio_app()
594
- dashboard_ui = create_dashboard()
595
- app = gr.mount_gradio_app(my_app, gradio_app, path="/gradio")
596
- app2 = gr.mount_gradio_app(my_app, dashboard_ui, path="/dashboard")
597
 
598
  if __name__ == "__main__":
599
- logger.info('🚀 Starting HF Tagging Bot...')
600
  print("🚀 Starting HF Tagging Bot...")
601
  print("📊 Dashboard: http://localhost:7860/gradio")
602
  print("🔗 Webhook: http://localhost:7860/webhook")
603
- uvicorn.run("app:app", host="0.0.0.0", port=7860, reload=False)
 
3
  import json
4
  from datetime import datetime
5
  from typing import List, Dict, Any, Optional, Literal
6
+
7
  from fastapi import FastAPI, Request, BackgroundTasks
8
  from fastapi.middleware.cors import CORSMiddleware
9
  import gradio as gr
 
11
  from pydantic import BaseModel
12
  from huggingface_hub.inference._mcp.agent import Agent
13
  from dotenv import load_dotenv
 
 
 
 
 
 
 
 
14
 
15
  load_dotenv()
16
 
 
22
  DEFAULT_PROVIDER: Literal["hf-inference"] = "hf-inference"
23
  HF_PROVIDER = os.getenv("HF_PROVIDER", DEFAULT_PROVIDER)
24
 
 
 
 
 
 
 
 
 
25
  # Simple storage for processed tag operations
26
  tag_operations_store: List[Dict[str, Any]] = []
27
 
 
65
  "computer-vision",
66
  "nlp",
67
  "cv",
68
+ "multimodal",
69
  }
70
 
71
 
 
76
  repo: Dict[str, str]
77
 
78
 
79
+ app = FastAPI(title="HF Tagging Bot")
80
+ app.add_middleware(CORSMiddleware, allow_origins=["*"])
 
 
 
 
 
81
 
82
 
83
  async def get_agent():
84
  """Get or create Agent instance"""
85
  print("🤖 get_agent() called...")
 
86
  global agent_instance
87
  if agent_instance is None and HF_TOKEN:
88
  print("🔧 Creating new Agent instance...")
89
  print(f"🔑 HF_TOKEN present: {bool(HF_TOKEN)}")
90
  print(f"🤖 Model: {HF_MODEL}")
91
  print(f"🔗 Provider: {DEFAULT_PROVIDER}")
92
+
93
  try:
94
  agent_instance = Agent(
95
  model=HF_MODEL,
 
108
  ],
109
  )
110
  print("✅ Agent instance created successfully")
 
111
  print("🔧 Loading tools...")
 
112
  await agent_instance.load_tools()
113
  print("✅ Tools loaded successfully")
114
  except Exception as e:
 
123
 
124
 
125
  def extract_tags_from_text(text: str) -> List[str]:
 
 
 
126
  """Extract potential tags from discussion text"""
127
  text_lower = text.lower()
128
 
 
155
  valid_tags = []
156
  for tag in all_tags:
157
  if tag in RECOGNIZED_TAGS or tag in explicit_tags:
158
+ valid_tags.append(tag)
159
 
 
 
160
  return valid_tags
161
 
162
 
 
205
  try:
206
  # Create a comprehensive prompt for the agent
207
  user_prompt = f"""
208
+ I need to add the following tags to the repository '{repo_name}': {", ".join(all_tags)}
209
 
210
  For each tag, please:
211
  1. Check if the tag already exists on the repository using get_current_tags
 
369
  "original_comment": comment_content,
370
  "comment_author": comment_author,
371
  "detected_tags": all_tags,
372
+ "results": result_messages,
373
  }
374
 
375
  tag_operations_store.append(interaction)
 
382
  print(error_msg)
383
  return error_msg
384
 
385
+
386
+ @app.post("/webhook")
387
  async def webhook_handler(request: Request, background_tasks: BackgroundTasks):
388
  """Handle HF Hub webhooks"""
389
  webhook_secret = request.headers.get("X-Webhook-Secret")
 
393
 
394
  payload = await request.json()
395
  print(f"📥 Received webhook payload: {json.dumps(payload, indent=2)}")
396
+
397
  event = payload.get("event", {})
398
  scope = event.get("scope")
399
  action = event.get("action")
 
475
  - Supports common ML tags like: pytorch, tensorflow,
476
  text-generation, etc.
477
  """)
478
+
479
  with gr.Column():
480
  sim_repo = gr.Textbox(
481
  label="Repository",
 
511
 
512
  return demo
513
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
514
 
515
  # Mount Gradio app
516
  gradio_app = create_gradio_app()
517
+ app = gr.mount_gradio_app(app, gradio_app, path="/gradio")
518
+
 
519
 
520
  if __name__ == "__main__":
 
521
  print("🚀 Starting HF Tagging Bot...")
522
  print("📊 Dashboard: http://localhost:7860/gradio")
523
  print("🔗 Webhook: http://localhost:7860/webhook")
524
+ uvicorn.run("app:app", host="0.0.0.0", port=7860, reload=True)
mcp_server.py CHANGED
@@ -10,13 +10,6 @@ from huggingface_hub import HfApi, model_info, ModelCard, ModelCardData
10
  from huggingface_hub.utils import HfHubHTTPError
11
  from dotenv import load_dotenv
12
 
13
- #adding logger"🚀 Starting HF Tagging Bot...
14
- import logging
15
- loggerS = logging.getLogger(__name__)
16
- logging.basicConfig(filename='/home/user/Stag.log', encoding='utf-8', level=logging.DEBUG)
17
- #for testing logger
18
- loggerS.debug('MCP-server, This message should go to the log file')
19
-
20
  load_dotenv()
21
 
22
  # Configuration
@@ -33,7 +26,7 @@ mcp = FastMCP("hf-tagging-bot")
33
  def get_current_tags(repo_id: str) -> str:
34
  """Get current tags from a HuggingFace model repository"""
35
  print(f"🔧 get_current_tags called with repo_id: {repo_id}")
36
- loggerS.info('🔧 MCP Server-get_current_tags called with repo_id %s', {repo_id})
37
  if not hf_api:
38
  error_result = {"error": "HF token not configured"}
39
  json_str = json.dumps(error_result)
@@ -68,7 +61,7 @@ def get_current_tags(repo_id: str) -> str:
68
  def add_new_tag(repo_id: str, new_tag: str) -> str:
69
  """Add a new tag to a HuggingFace model repository via PR"""
70
  print(f"🔧 add_new_tag called with repo_id: {repo_id}, new_tag: {new_tag}")
71
- loggerS.info('Server-Add a new tag')
72
  if not hf_api:
73
  error_result = {"error": "HF token not configured"}
74
  json_str = json.dumps(error_result)
@@ -188,5 +181,4 @@ This PR adds the `{new_tag}` tag to the model repository.
188
 
189
 
190
  if __name__ == "__main__":
191
- loggerS.info('main mcp-server')
192
  mcp.run()
 
10
  from huggingface_hub.utils import HfHubHTTPError
11
  from dotenv import load_dotenv
12
 
 
 
 
 
 
 
 
13
  load_dotenv()
14
 
15
  # Configuration
 
26
  def get_current_tags(repo_id: str) -> str:
27
  """Get current tags from a HuggingFace model repository"""
28
  print(f"🔧 get_current_tags called with repo_id: {repo_id}")
29
+
30
  if not hf_api:
31
  error_result = {"error": "HF token not configured"}
32
  json_str = json.dumps(error_result)
 
61
  def add_new_tag(repo_id: str, new_tag: str) -> str:
62
  """Add a new tag to a HuggingFace model repository via PR"""
63
  print(f"🔧 add_new_tag called with repo_id: {repo_id}, new_tag: {new_tag}")
64
+
65
  if not hf_api:
66
  error_result = {"error": "HF token not configured"}
67
  json_str = json.dumps(error_result)
 
181
 
182
 
183
  if __name__ == "__main__":
 
184
  mcp.run()
pyproject.toml CHANGED
@@ -5,15 +5,15 @@ description = "FastAPI and Gradio app for Hugging Face Hub discussion webhooks"
5
  readme = "README.md"
6
  requires-python = ">=3.11"
7
  dependencies = [
8
- "fastapi",
9
- "uvicorn[standard]>",
10
- "gradio",
11
- "huggingface-hub[mcp]",
12
- "pydantic",
13
- "python-multipart",
14
- "requests",
15
- "python-dotenv",
16
- "fastmcp",
17
  ]
18
 
19
  [build-system]
 
5
  readme = "README.md"
6
  requires-python = ">=3.11"
7
  dependencies = [
8
+ "fastapi>=0.104.0",
9
+ "uvicorn[standard]>=0.24.0",
10
+ "gradio>=4.0.0",
11
+ "huggingface-hub[mcp]>=0.32.0",
12
+ "pydantic>=2.0.0",
13
+ "python-multipart>=0.0.6",
14
+ "requests>=2.31.0",
15
+ "python-dotenv>=1.0.0",
16
+ "fastmcp>=2.0.0",
17
  ]
18
 
19
  [build-system]
requirements.txt CHANGED
@@ -1,75 +1,77 @@
1
- aiofiles
2
- aiohappyeyeballs
3
- aiohttp
4
- aiosignal
5
- annotated-types
6
- anyio
7
- attrs
 
 
8
  audioop-lts==0.2.1 ; python_full_version >= '3.13'
9
- certifi
10
- charset-normalizer
11
- click
12
- colorama ; sys_platform == 'win32' or platform_system == 'Windows'
13
- exceptiongroup
14
- fastapi
15
- fastmcp
16
- ffmpy
17
- filelock
18
- frozenlist
19
- fsspec
20
- gradio
21
- gradio-client
22
- groovy
23
- h11
24
- hf-xet ; platform_machine == 'aarch64' or platform_machine == 'amd64' or platform_machine == 'arm64' or platform_machine == 'x86_64'
25
- httpcore
26
- httptools
27
- httpx
28
- httpx-sse
29
- huggingface-hub
30
- idna
31
- jinja2
32
- markdown-it-py
33
- markupsafe
34
- mcp
35
- mdurl
36
- multidict
37
- numpy
38
- openapi-pydantic
39
- orjson
40
- packaging
41
- pandas
42
- pillow
43
- propcache
44
- pydantic
45
- pydantic-core
46
- pydantic-settings
47
- pydub
48
- pygments
49
- python-dateutil
50
- python-dotenv
51
- python-multipart
52
- pytz
53
- pyyaml
54
- requests
55
- rich
56
- ruff ; sys_platform != 'emscripten'
57
- safehttpx
58
- semantic-version
59
- shellingham
60
- six
61
- sniffio
62
- sse-starlette
63
- starlette
64
- tomlkit
65
- tqdm
66
- typer
67
- typing-extensions
68
- typing-inspection
69
- tzdata
70
- urllib3
71
- uvicorn
72
- uvloop ; platform_python_implementation != 'PyPy' and sys_platform != 'cygwin' and sys_platform != 'win32'
73
- watchfiles
74
- websockets
75
- yarl
 
1
+ # This file was autogenerated by uv via the following command:
2
+ # uv export --format requirements-txt --no-hashes
3
+ aiofiles==24.1.0
4
+ aiohappyeyeballs==2.6.1
5
+ aiohttp==3.12.2
6
+ aiosignal==1.3.2
7
+ annotated-types==0.7.0
8
+ anyio==4.9.0
9
+ attrs==25.3.0
10
  audioop-lts==0.2.1 ; python_full_version >= '3.13'
11
+ certifi==2025.4.26
12
+ charset-normalizer==3.4.2
13
+ click==8.2.1
14
+ colorama==0.4.6 ; sys_platform == 'win32' or platform_system == 'Windows'
15
+ exceptiongroup==1.3.0
16
+ fastapi==0.115.12
17
+ fastmcp==2.5.1
18
+ ffmpy==0.5.0
19
+ filelock==3.18.0
20
+ frozenlist==1.6.0
21
+ fsspec==2025.5.1
22
+ gradio==5.31.0
23
+ gradio-client==1.10.1
24
+ groovy==0.1.2
25
+ h11==0.16.0
26
+ hf-xet==1.1.2 ; platform_machine == 'aarch64' or platform_machine == 'amd64' or platform_machine == 'arm64' or platform_machine == 'x86_64'
27
+ httpcore==1.0.9
28
+ httptools==0.6.4
29
+ httpx==0.28.1
30
+ httpx-sse==0.4.0
31
+ huggingface-hub==0.32.2
32
+ idna==3.10
33
+ jinja2==3.1.6
34
+ markdown-it-py==3.0.0
35
+ markupsafe==3.0.2
36
+ mcp==1.9.1
37
+ mdurl==0.1.2
38
+ multidict==6.4.4
39
+ numpy==2.2.6
40
+ openapi-pydantic==0.5.1
41
+ orjson==3.10.18
42
+ packaging==25.0
43
+ pandas==2.2.3
44
+ pillow==11.2.1
45
+ propcache==0.3.1
46
+ pydantic==2.11.5
47
+ pydantic-core==2.33.2
48
+ pydantic-settings==2.9.1
49
+ pydub==0.25.1
50
+ pygments==2.19.1
51
+ python-dateutil==2.9.0.post0
52
+ python-dotenv==1.1.0
53
+ python-multipart==0.0.20
54
+ pytz==2025.2
55
+ pyyaml==6.0.2
56
+ requests==2.32.3
57
+ rich==14.0.0
58
+ ruff==0.11.11 ; sys_platform != 'emscripten'
59
+ safehttpx==0.1.6
60
+ semantic-version==2.10.0
61
+ shellingham==1.5.4
62
+ six==1.17.0
63
+ sniffio==1.3.1
64
+ sse-starlette==2.3.5
65
+ starlette==0.46.2
66
+ tomlkit==0.13.2
67
+ tqdm==4.67.1
68
+ typer==0.16.0
69
+ typing-extensions==4.13.2
70
+ typing-inspection==0.4.1
71
+ tzdata==2025.2
72
+ urllib3==2.4.0
73
+ uvicorn==0.34.2
74
+ uvloop==0.21.0 ; platform_python_implementation != 'PyPy' and sys_platform != 'cygwin' and sys_platform != 'win32'
75
+ watchfiles==1.0.5
76
+ websockets==15.0.1
77
+ yarl==1.20.0