Sandhya commited on
Commit
f95f6a4
·
1 Parent(s): 060bc2f

first commit

Browse files
Files changed (1) hide show
  1. app.py +17 -15
app.py CHANGED
@@ -24,9 +24,7 @@ DEFAULT_PROVIDER: Literal['hf-inference'] = "hf-inference"
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,
@@ -38,21 +36,20 @@ async def get_agent():
38
  "command": "python",
39
  "args": ["mcp_server.py"],
40
  "cwd": ".",
41
- "env": {"HF_TOKEN": HF_TOKEN} if HF_TOKEN else {}
42
  }
43
  }]
44
  )
45
  await agent.load_tools()
46
  agent_instance = agent
47
- print("✅ Agent is ready")
48
  except Exception as e:
49
- print(f"❌ Error creating/loading agent: {str(e)}")
50
  return agent_instance
51
 
52
  @app.on_event("startup")
53
  async def startup_event():
54
- global agent_instance
55
- agent_instance = await get_agent()
56
 
57
  def chat_function(user_message, history, model_id):
58
  global agent_instance
@@ -69,7 +66,8 @@ User question: {user_message}
69
  if hasattr(output, "content") and output.content:
70
  response = output.content
71
  if not response:
72
- response = "⚠️ Sorry, I couldn't generate a response."
 
73
  history = history + [{"role": "user", "content": user_message}]
74
  history.append({"role": "assistant", "content": response})
75
  except Exception as e:
@@ -78,18 +76,22 @@ User question: {user_message}
78
  return history, ""
79
 
80
  def create_gradio_app():
81
- with gr.Blocks(theme=gr.themes.Soft(primary_hue="violet"), title="🤖 Model Card Chatbot") as demo:
82
  gr.Markdown("""
83
  # 🤖 **Model Card Chatbot**
84
- Ask anything about a model's card on Hugging Face.
85
  """)
 
86
  with gr.Row():
87
  model_id = gr.Textbox(label="Model ID", value="google/gemma-2-2b", scale=2)
88
- user_input = gr.Textbox(label="Your Question", placeholder="e.g., What is this model trained on?", scale=3)
89
- send = gr.Button("🔍 Ask", scale=1)
90
- chatbot = gr.Chatbot(label="Chat", type="messages")
 
 
 
91
 
92
- send.click(
93
  fn=chat_function,
94
  inputs=[user_input, chatbot, model_id],
95
  outputs=[chatbot, user_input]
 
24
  async def get_agent():
25
  global agent_instance
26
  if agent_instance is None and HF_TOKEN:
27
+ print("🔧 Creating Agent...")
 
 
28
  try:
29
  agent = Agent(
30
  model=HF_MODEL,
 
36
  "command": "python",
37
  "args": ["mcp_server.py"],
38
  "cwd": ".",
39
+ "env": {"HF_TOKEN": HF_TOKEN}
40
  }
41
  }]
42
  )
43
  await agent.load_tools()
44
  agent_instance = agent
45
+ print("✅ Agent is ready!")
46
  except Exception as e:
47
+ print(f"❌ Failed to create agent: {str(e)}")
48
  return agent_instance
49
 
50
  @app.on_event("startup")
51
  async def startup_event():
52
+ await get_agent()
 
53
 
54
  def chat_function(user_message, history, model_id):
55
  global agent_instance
 
66
  if hasattr(output, "content") and output.content:
67
  response = output.content
68
  if not response:
69
+ response = "⚠️ Sorry, no answer generated."
70
+ # Append as new OpenAI-style message
71
  history = history + [{"role": "user", "content": user_message}]
72
  history.append({"role": "assistant", "content": response})
73
  except Exception as e:
 
76
  return history, ""
77
 
78
  def create_gradio_app():
79
+ with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo"), title="🤖 Model Card Chatbot") as demo:
80
  gr.Markdown("""
81
  # 🤖 **Model Card Chatbot**
82
+ _Ask anything about a Hugging Face model's card_
83
  """)
84
+ chatbot = gr.Chatbot(label="Chat", type="messages", show_copy_button=True, height=400)
85
  with gr.Row():
86
  model_id = gr.Textbox(label="Model ID", value="google/gemma-2-2b", scale=2)
87
+ user_input = gr.Textbox(
88
+ label="Your Question",
89
+ placeholder="e.g., What dataset was it trained on?",
90
+ scale=4
91
+ )
92
+ ask_button = gr.Button("🔍 Ask", scale=1)
93
 
94
+ ask_button.click(
95
  fn=chat_function,
96
  inputs=[user_input, chatbot, model_id],
97
  outputs=[chatbot, user_input]