Spaces:
Sleeping
Sleeping
Sandhya
commited on
Commit
·
289e125
1
Parent(s):
014b0ea
First Commit
Browse files- .gitignore +1 -0
- app.py +93 -0
- mcp_server.py +24 -0
- requirements.txt +80 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
.env
|
app.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from fastapi import FastAPI
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
from huggingface_hub.inference._mcp.agent import Agent
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import uvicorn
|
| 7 |
+
from fastapi.responses import RedirectResponse
|
| 8 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 9 |
+
from typing import Optional, Literal
|
| 10 |
+
|
| 11 |
+
load_dotenv()
|
| 12 |
+
HF_TOKEN=os.getenv("HF_TOKEN")
|
| 13 |
+
HF_MODEL=os.getenv("HF_MODEL","Qwen/Qwen1.5-0.5B-Chat")
|
| 14 |
+
app=FastAPI(title="MODEL-CARD-CHATBOT")
|
| 15 |
+
app.add_middleware(CORSMiddleware,allow_origins=["*"],allow_methods=["*"],allow_header=["*"])
|
| 16 |
+
|
| 17 |
+
agent_instance: Optional[Agent]=None
|
| 18 |
+
DEFAULT_PROVIDER:Literal['hf-inference']="hf-inference"
|
| 19 |
+
async def get_agent():
|
| 20 |
+
"""Get or create the agent instance."""
|
| 21 |
+
global agent_instance
|
| 22 |
+
if agent_instance is None and HF_TOKEN:
|
| 23 |
+
print("🔧 Creating new Agent instance ...")
|
| 24 |
+
print(f"✅ HF_TOKEN present : {bool(HF_TOKEN)}")
|
| 25 |
+
print(f"🤖 Model: {HF_MODEL}")
|
| 26 |
+
print(f"Provider: {DEFAULT_PROVIDER}")
|
| 27 |
+
try:
|
| 28 |
+
agent=Agent(model=HF_MODEL,provider="hf-inference",api_key=HF_TOKEN,
|
| 29 |
+
servers=[{"type":"stdio","config":{
|
| 30 |
+
"command":"python",
|
| 31 |
+
"args":["mcp_server.py"],"cwd":".","env":{"HF_TOKEN": HF_TOKEN} if HF_TOKEN else {}}}])
|
| 32 |
+
print("🚀 Agent instance created successfully")
|
| 33 |
+
print("🔁 loading tools ...")
|
| 34 |
+
await agent_instance.load_tools()
|
| 35 |
+
print("✅ Tools loaded successfully")
|
| 36 |
+
|
| 37 |
+
except Exception as e:
|
| 38 |
+
print(f"❌ Error creating/loading agent: {str(e)}")
|
| 39 |
+
return agent_instance
|
| 40 |
+
|
| 41 |
+
agent=get_agent()
|
| 42 |
+
|
| 43 |
+
@app.on_event("startup")
|
| 44 |
+
async def startup_event():
|
| 45 |
+
await agent.load_tools()
|
| 46 |
+
|
| 47 |
+
def chat_function(user_message,history,model_id):
|
| 48 |
+
|
| 49 |
+
"""Handles a user question by prompting the agent to read a model card and answer.
|
| 50 |
+
Args:
|
| 51 |
+
user_message (str): The user's question.
|
| 52 |
+
history (list): Chat history for display.
|
| 53 |
+
model_id (str): Hugging Face repo ID
|
| 54 |
+
|
| 55 |
+
Returns:
|
| 56 |
+
Tuple[list,str]: Updated Chat history and empty string for clearing input."""
|
| 57 |
+
|
| 58 |
+
prompt=f"""You're an assistant helping with hugging face model cards.
|
| 59 |
+
First, run the tool `read_model_card` on repo_id `{model_id}` to get the model card.
|
| 60 |
+
Then answer this user question based on the model card:
|
| 61 |
+
User question: {user_message}"""
|
| 62 |
+
history=history+[(user_message)]
|
| 63 |
+
try:
|
| 64 |
+
response=""
|
| 65 |
+
for output in agent.run(prompt):
|
| 66 |
+
if hasattr(output,"content") and output.content:
|
| 67 |
+
response=output.content
|
| 68 |
+
final_response=response or "⚠️ Sorry, I couldn't generate a response."
|
| 69 |
+
history[-1]=(user_message,final_response)
|
| 70 |
+
except Exception as e:
|
| 71 |
+
history[-1]=(user_message,f"⚠️ Error: {str(e)}")
|
| 72 |
+
return history, ""
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def create_gradio_app():
|
| 77 |
+
with gr.Blocks(title="Model Card Chatbot") as demo:
|
| 78 |
+
gr.Markdown("## 🤖 Model Card Chatbot\nAsk questions about Hugging Face model card")
|
| 79 |
+
with gr.Row():
|
| 80 |
+
model_id=gr.Textbox(label="MODEL ID", value="google/gemma-2-2b")
|
| 81 |
+
user_input=gr.Textbox(label="Your Question",value="Ask something about the model card .....")
|
| 82 |
+
chat=gr.Chatbot(label="chat")
|
| 83 |
+
send=gr.Button("Ask")
|
| 84 |
+
send.click(fn=chat_function,inputs=[user_input,chat,model_id],outputs=[chat,user_input])
|
| 85 |
+
return demo
|
| 86 |
+
gradio_app=create_gradio_app()
|
| 87 |
+
app=gr.mount_gradio_app(app,gradio_app,path="/")
|
| 88 |
+
|
| 89 |
+
@app.get("/")
|
| 90 |
+
async def root():
|
| 91 |
+
return RedirectResponse("/")
|
| 92 |
+
if __name__=="__main__":
|
| 93 |
+
uvicorn.run("app:app",host="0.0.0.0",port=7860,reload=True)
|
mcp_server.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastmcp import FastMCP
|
| 2 |
+
from huggingface_hub import ModelCard
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
load_dotenv()
|
| 5 |
+
import os,json
|
| 6 |
+
HF_TOKEN=os.getenv("HF_TOKEN")
|
| 7 |
+
mcp=FastMCP("model_card-chatbot")
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
@mcp.tool()
|
| 11 |
+
def read_model_card(repo_id:str)->str:
|
| 12 |
+
"""Tool to read and return the full model card from hugging face."""
|
| 13 |
+
try:
|
| 14 |
+
card=ModelCard.load(repo_id)
|
| 15 |
+
return json.dumps({"status":"success","readme":str(card)})
|
| 16 |
+
|
| 17 |
+
except FileNotFoundError:
|
| 18 |
+
return json.dumps({"status":"error","message":"This model does not have a model card"})
|
| 19 |
+
except Exception as e:
|
| 20 |
+
return json.dumps({"status":"error","message":str(e)})
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
aiofiles==24.1.0
|
| 2 |
+
annotated-types==0.7.0
|
| 3 |
+
anyio==4.9.0
|
| 4 |
+
attrs==25.3.0
|
| 5 |
+
Authlib==1.6.1
|
| 6 |
+
Brotli==1.1.0
|
| 7 |
+
certifi==2025.7.14
|
| 8 |
+
cffi==1.17.1
|
| 9 |
+
charset-normalizer==3.4.2
|
| 10 |
+
click==8.2.1
|
| 11 |
+
colorama==0.4.6
|
| 12 |
+
cryptography==45.0.5
|
| 13 |
+
cyclopts==3.22.2
|
| 14 |
+
dnspython==2.7.0
|
| 15 |
+
docstring_parser==0.17.0
|
| 16 |
+
docutils==0.21.2
|
| 17 |
+
email_validator==2.2.0
|
| 18 |
+
exceptiongroup==1.3.0
|
| 19 |
+
fastapi==0.116.1
|
| 20 |
+
fastmcp==2.10.6
|
| 21 |
+
ffmpy==0.6.1
|
| 22 |
+
filelock==3.18.0
|
| 23 |
+
fsspec==2025.7.0
|
| 24 |
+
gradio==5.38.0
|
| 25 |
+
gradio_client==1.11.0
|
| 26 |
+
groovy==0.1.2
|
| 27 |
+
h11==0.16.0
|
| 28 |
+
httpcore==1.0.9
|
| 29 |
+
httpx==0.28.1
|
| 30 |
+
httpx-sse==0.4.1
|
| 31 |
+
huggingface-hub==0.33.4
|
| 32 |
+
idna==3.10
|
| 33 |
+
Jinja2==3.1.6
|
| 34 |
+
jsonschema==4.25.0
|
| 35 |
+
jsonschema-specifications==2025.4.1
|
| 36 |
+
markdown-it-py==3.0.0
|
| 37 |
+
MarkupSafe==3.0.2
|
| 38 |
+
mcp==1.12.1
|
| 39 |
+
mdurl==0.1.2
|
| 40 |
+
numpy==2.3.1
|
| 41 |
+
openapi-pydantic==0.5.1
|
| 42 |
+
orjson==3.11.0
|
| 43 |
+
packaging==25.0
|
| 44 |
+
pandas==2.3.1
|
| 45 |
+
pillow==11.3.0
|
| 46 |
+
pycparser==2.22
|
| 47 |
+
pydantic==2.11.7
|
| 48 |
+
pydantic-settings==2.10.1
|
| 49 |
+
pydantic_core==2.33.2
|
| 50 |
+
pydub==0.25.1
|
| 51 |
+
Pygments==2.19.2
|
| 52 |
+
pyperclip==1.9.0
|
| 53 |
+
python-dateutil==2.9.0.post0
|
| 54 |
+
python-dotenv==1.1.1
|
| 55 |
+
python-multipart==0.0.20
|
| 56 |
+
pytz==2025.2
|
| 57 |
+
pywin32==311
|
| 58 |
+
PyYAML==6.0.2
|
| 59 |
+
referencing==0.36.2
|
| 60 |
+
requests==2.32.4
|
| 61 |
+
rich==14.0.0
|
| 62 |
+
rich-rst==1.3.1
|
| 63 |
+
rpds-py==0.26.0
|
| 64 |
+
ruff==0.12.4
|
| 65 |
+
safehttpx==0.1.6
|
| 66 |
+
semantic-version==2.10.0
|
| 67 |
+
shellingham==1.5.4
|
| 68 |
+
six==1.17.0
|
| 69 |
+
sniffio==1.3.1
|
| 70 |
+
sse-starlette==2.4.1
|
| 71 |
+
starlette==0.47.2
|
| 72 |
+
tomlkit==0.13.3
|
| 73 |
+
tqdm==4.67.1
|
| 74 |
+
typer==0.16.0
|
| 75 |
+
typing-inspection==0.4.1
|
| 76 |
+
typing_extensions==4.14.1
|
| 77 |
+
tzdata==2025.2
|
| 78 |
+
urllib3==2.5.0
|
| 79 |
+
uvicorn==0.35.0
|
| 80 |
+
websockets==15.0.1
|