Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import requests
|
| 6 |
+
import pandas as pd
|
| 7 |
+
from transformers.tools import HfAgent
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class SmartAgent:
|
| 14 |
+
def __init__(self):
|
| 15 |
+
self.agent = HfAgent("https://api-inference.huggingface.co/chat/agent")
|
| 16 |
+
print("SmartAgent initialized with Hugging Face tools.")
|
| 17 |
+
|
| 18 |
+
def __call__(self, question: str) -> str:
|
| 19 |
+
print(f"[SmartAgent] Received question: {question[:100]}")
|
| 20 |
+
try:
|
| 21 |
+
result = self.agent.run(question)
|
| 22 |
+
print(f"[SmartAgent] Agent result: {result}")
|
| 23 |
+
return str(result)
|
| 24 |
+
except Exception as e:
|
| 25 |
+
print(f"[SmartAgent] Error: {e}")
|
| 26 |
+
return f"Agent error: {e}"
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 30 |
+
space_id = os.getenv("SPACE_ID")
|
| 31 |
+
if profile:
|
| 32 |
+
username = f"{profile.username}"
|
| 33 |
+
else:
|
| 34 |
+
return "Please Login to Hugging Face with the button.", None
|
| 35 |
+
|
| 36 |
+
api_url = DEFAULT_API_URL
|
| 37 |
+
questions_url = f"{api_url}/questions"
|
| 38 |
+
submit_url = f"{api_url}/submit"
|
| 39 |
+
|
| 40 |
+
try:
|
| 41 |
+
agent = SmartAgent()
|
| 42 |
+
except Exception as e:
|
| 43 |
+
return f"Error initializing agent: {e}", None
|
| 44 |
+
|
| 45 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 46 |
+
|
| 47 |
+
try:
|
| 48 |
+
response = requests.get(questions_url, timeout=15)
|
| 49 |
+
response.raise_for_status()
|
| 50 |
+
questions_data = response.json()
|
| 51 |
+
except Exception as e:
|
| 52 |
+
return f"Error fetching questions: {e}", None
|
| 53 |
+
|
| 54 |
+
results_log = []
|
| 55 |
+
answers_payload = []
|
| 56 |
+
for item in questions_data:
|
| 57 |
+
task_id = item.get("task_id")
|
| 58 |
+
question_text = item.get("question")
|
| 59 |
+
if not task_id or question_text is None:
|
| 60 |
+
continue
|
| 61 |
+
try:
|
| 62 |
+
submitted_answer = agent(question_text)
|
| 63 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 64 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 65 |
+
except Exception as e:
|
| 66 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
| 67 |
+
|
| 68 |
+
submission_data = {
|
| 69 |
+
"username": username.strip(),
|
| 70 |
+
"agent_code": agent_code,
|
| 71 |
+
"answers": answers_payload
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
try:
|
| 75 |
+
response = requests.post(submit_url, json=submission_data, timeout=60)
|
| 76 |
+
response.raise_for_status()
|
| 77 |
+
result_data = response.json()
|
| 78 |
+
final_status = (
|
| 79 |
+
f"Submission Successful!\n"
|
| 80 |
+
f"User: {result_data.get('username')}\n"
|
| 81 |
+
f"Score: {result_data.get('score', 'N/A')}%\n"
|
| 82 |
+
f"Correct: {result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')}\n"
|
| 83 |
+
f"Message: {result_data.get('message', 'No message received.')}"
|
| 84 |
+
)
|
| 85 |
+
return final_status, pd.DataFrame(results_log)
|
| 86 |
+
except Exception as e:
|
| 87 |
+
return f"Submission failed: {e}", pd.DataFrame(results_log)
|
| 88 |
+
|
| 89 |
+
with gr.Blocks() as demo:
|
| 90 |
+
gr.Markdown("# Smart AI Agent (Web, Image, Video, and QA Support)")
|
| 91 |
+
gr.Markdown("""
|
| 92 |
+
This agent can:
|
| 93 |
+
- Answer complex questions
|
| 94 |
+
- Perform web searches
|
| 95 |
+
- Explain images or videos from URLs
|
| 96 |
+
|
| 97 |
+
Please login and run the evaluation to test the agent.
|
| 98 |
+
""")
|
| 99 |
+
gr.LoginButton()
|
| 100 |
+
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 101 |
+
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 102 |
+
results_table = gr.DataFrame(label="Questions and Agent Answers")
|
| 103 |
+
|
| 104 |
+
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
| 105 |
+
|
| 106 |
+
if __name__ == "__main__":
|
| 107 |
+
demo.launch(debug=True, share=False)
|