Spaces:
Sleeping
Sleeping
Sandhya
commited on
Commit
·
707ec96
1
Parent(s):
6f06d33
first commit
Browse files- __pycache__/app.cpython-311.pyc +0 -0
- app.py +45 -31
- mcp_server.py +12 -10
__pycache__/app.cpython-311.pyc
ADDED
|
Binary file (6.03 kB). View file
|
|
|
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
from fastapi import FastAPI
|
| 3 |
from dotenv import load_dotenv
|
|
@@ -9,26 +10,27 @@ 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","google/gemma-2-2b")
|
| 14 |
-
app=FastAPI(title="MODEL-CARD-CHATBOT")
|
| 15 |
-
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=['*'], allow_headers=['*'])
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
|
|
|
|
|
|
|
| 20 |
|
| 21 |
async def get_agent():
|
| 22 |
global agent_instance
|
| 23 |
if agent_instance is None and HF_TOKEN:
|
| 24 |
print("🔧 Creating new Agent instance ...")
|
| 25 |
-
print(f"✅ HF_TOKEN present
|
| 26 |
print(f"🤖 Model: {HF_MODEL}")
|
| 27 |
-
print(f"Provider: {DEFAULT_PROVIDER}")
|
| 28 |
try:
|
| 29 |
agent = Agent(
|
| 30 |
model=HF_MODEL,
|
| 31 |
-
provider=
|
| 32 |
api_key=HF_TOKEN,
|
| 33 |
servers=[{
|
| 34 |
"type": "stdio",
|
|
@@ -52,42 +54,54 @@ async def startup_event():
|
|
| 52 |
global agent_instance
|
| 53 |
agent_instance = await get_agent()
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
def chat_function(user_message, history, model_id):
|
| 58 |
global agent_instance
|
| 59 |
-
prompt=f"""
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
| 63 |
history = history + [(user_message, None)]
|
| 64 |
try:
|
| 65 |
response = ""
|
| 66 |
-
|
|
|
|
| 67 |
if hasattr(output, "content") and output.content:
|
| 68 |
response = output.content
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
history[-1] = (user_message,
|
| 72 |
except Exception as e:
|
| 73 |
history[-1] = (user_message, f"⚠️ Error: {str(e)}")
|
| 74 |
return history, ""
|
| 75 |
|
| 76 |
-
|
| 77 |
def create_gradio_app():
|
| 78 |
-
with gr.Blocks(title="Model Card Chatbot") as demo:
|
| 79 |
-
gr.Markdown("
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
return demo
|
| 86 |
-
|
| 87 |
-
|
|
|
|
| 88 |
|
| 89 |
@app.get("/")
|
| 90 |
async def root():
|
| 91 |
return RedirectResponse("/")
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
import os
|
| 3 |
from fastapi import FastAPI
|
| 4 |
from dotenv import load_dotenv
|
|
|
|
| 10 |
from typing import Optional, Literal
|
| 11 |
|
| 12 |
load_dotenv()
|
| 13 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 14 |
+
HF_MODEL = os.getenv("HF_MODEL", "google/gemma-2-2b")
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
app = FastAPI(title="Model Card Chatbot")
|
| 17 |
+
app.add_middleware(
|
| 18 |
+
CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"]
|
| 19 |
+
)
|
| 20 |
|
| 21 |
+
agent_instance: Optional[Agent] = None
|
| 22 |
+
DEFAULT_PROVIDER: Literal['hf-inference'] = "hf-inference"
|
| 23 |
|
| 24 |
async def get_agent():
|
| 25 |
global agent_instance
|
| 26 |
if agent_instance is None and HF_TOKEN:
|
| 27 |
print("🔧 Creating new Agent instance ...")
|
| 28 |
+
print(f"✅ HF_TOKEN present: {bool(HF_TOKEN)}")
|
| 29 |
print(f"🤖 Model: {HF_MODEL}")
|
|
|
|
| 30 |
try:
|
| 31 |
agent = Agent(
|
| 32 |
model=HF_MODEL,
|
| 33 |
+
provider=DEFAULT_PROVIDER,
|
| 34 |
api_key=HF_TOKEN,
|
| 35 |
servers=[{
|
| 36 |
"type": "stdio",
|
|
|
|
| 54 |
global agent_instance
|
| 55 |
agent_instance = await get_agent()
|
| 56 |
|
|
|
|
|
|
|
| 57 |
def chat_function(user_message, history, model_id):
|
| 58 |
global agent_instance
|
| 59 |
+
prompt = f"""
|
| 60 |
+
You're an assistant helping with Hugging Face model cards.
|
| 61 |
+
First, run the tool `read_model_card` on repo_id `{model_id}` to get the model card.
|
| 62 |
+
Then answer this user question based on the model card:
|
| 63 |
+
User question: {user_message}
|
| 64 |
+
"""
|
| 65 |
history = history + [(user_message, None)]
|
| 66 |
try:
|
| 67 |
response = ""
|
| 68 |
+
outputs = agent_instance.run(prompt)
|
| 69 |
+
for output in outputs:
|
| 70 |
if hasattr(output, "content") and output.content:
|
| 71 |
response = output.content
|
| 72 |
+
if not response:
|
| 73 |
+
response = "⚠️ Sorry, I couldn't generate a response."
|
| 74 |
+
history[-1] = (user_message, response)
|
| 75 |
except Exception as e:
|
| 76 |
history[-1] = (user_message, f"⚠️ Error: {str(e)}")
|
| 77 |
return history, ""
|
| 78 |
|
|
|
|
| 79 |
def create_gradio_app():
|
| 80 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="🤖 Model Card Chatbot") as demo:
|
| 81 |
+
gr.Markdown("""
|
| 82 |
+
# 🤖 **Model Card Chatbot**
|
| 83 |
+
Ask anything about a model's card on Hugging Face.
|
| 84 |
+
""")
|
| 85 |
+
with gr.Row():
|
| 86 |
+
model_id = gr.Textbox(label="Model ID", value="google/gemma-2-2b", scale=2)
|
| 87 |
+
user_input = gr.Textbox(label="Your Question", placeholder="e.g., What is this model trained on?", scale=3)
|
| 88 |
+
send = gr.Button("🔍 Ask", scale=1)
|
| 89 |
+
chatbot = gr.Chatbot(label="Chat")
|
| 90 |
+
|
| 91 |
+
send.click(
|
| 92 |
+
fn=chat_function,
|
| 93 |
+
inputs=[user_input, chatbot, model_id],
|
| 94 |
+
outputs=[chatbot, user_input]
|
| 95 |
+
)
|
| 96 |
return demo
|
| 97 |
+
|
| 98 |
+
gradio_app = create_gradio_app()
|
| 99 |
+
app = gr.mount_gradio_app(app, gradio_app, path="/")
|
| 100 |
|
| 101 |
@app.get("/")
|
| 102 |
async def root():
|
| 103 |
return RedirectResponse("/")
|
| 104 |
+
|
| 105 |
+
if __name__ == "__main__":
|
| 106 |
+
uvicorn.run("app:app", host="0.0.0.0", port=7860, reload=True)
|
| 107 |
+
|
mcp_server.py
CHANGED
|
@@ -1,23 +1,25 @@
|
|
|
|
|
| 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 |
-
"""
|
|
|
|
|
|
|
| 13 |
try:
|
| 14 |
-
card=ModelCard.load(repo_id)
|
| 15 |
-
return json.dumps({"status":"success","readme": card.text})
|
| 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 |
|
|
|
|
| 1 |
+
# mcp_server.py
|
| 2 |
from fastmcp import FastMCP
|
| 3 |
from huggingface_hub import ModelCard
|
| 4 |
+
import json
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
load_dotenv()
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
mcp = FastMCP("model_card-chatbot")
|
| 9 |
|
| 10 |
@mcp.tool()
|
| 11 |
+
def read_model_card(repo_id: str) -> str:
|
| 12 |
+
"""
|
| 13 |
+
Tool to read and return the full model card from Hugging Face.
|
| 14 |
+
"""
|
| 15 |
try:
|
| 16 |
+
card = ModelCard.load(repo_id)
|
| 17 |
+
return json.dumps({"status": "success", "readme": card.text})
|
|
|
|
| 18 |
except FileNotFoundError:
|
| 19 |
+
return json.dumps({"status": "error", "message": "This model does not have a model card"})
|
| 20 |
except Exception as e:
|
| 21 |
+
return json.dumps({"status": "error", "message": str(e)})
|
| 22 |
+
|
| 23 |
|
| 24 |
|
| 25 |
|