Spaces:
Runtime error
Runtime error
Commit
Β·
e07bdae
1
Parent(s):
ce0a17d
adding app.py
Browse files- README.md +8 -5
- app.py +149 -0
- requirements.txt +2 -0
README.md
CHANGED
@@ -1,12 +1,15 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
colorFrom: indigo
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 5.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
|
|
|
|
|
|
10 |
---
|
11 |
|
12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: Template Final Assignment
|
3 |
+
emoji: π΅π»ββοΈ
|
4 |
colorFrom: indigo
|
5 |
+
colorTo: indigo
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 5.25.2
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
+
hf_oauth: true
|
11 |
+
# optional, default duration is 8 hours/480 minutes. Max duration is 30 days/43200 minutes.
|
12 |
+
hf_oauth_expiration_minutes: 480
|
13 |
---
|
14 |
|
15 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,149 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
import requests
|
4 |
+
import pandas as pd
|
5 |
+
|
6 |
+
# --- Constants ---
|
7 |
+
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
8 |
+
|
9 |
+
# --- Basic Agent ---
|
10 |
+
class BasicAgent:
|
11 |
+
def __init__(self):
|
12 |
+
print("BasicAgent initialized.")
|
13 |
+
|
14 |
+
def __call__(self, question: str) -> str:
|
15 |
+
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
16 |
+
fixed_answer = "This is a default answer."
|
17 |
+
print(f"Agent returning fixed answer: {fixed_answer}")
|
18 |
+
return fixed_answer
|
19 |
+
|
20 |
+
# --- Run Agent only (generate answers, don't submit) ---
|
21 |
+
def run_agent_only(profile: gr.OAuthProfile | None):
|
22 |
+
if not profile:
|
23 |
+
return "Please login first.", None, None
|
24 |
+
|
25 |
+
try:
|
26 |
+
agent = BasicAgent()
|
27 |
+
except Exception as e:
|
28 |
+
return f"Error initializing agent: {e}", None, None
|
29 |
+
|
30 |
+
questions_url = f"{DEFAULT_API_URL}/questions"
|
31 |
+
try:
|
32 |
+
response = requests.get(questions_url, timeout=10)
|
33 |
+
response.raise_for_status()
|
34 |
+
questions_data = response.json()
|
35 |
+
except Exception as e:
|
36 |
+
return f"Failed to fetch questions: {e}", None, None
|
37 |
+
|
38 |
+
results_log = []
|
39 |
+
answers_payload = []
|
40 |
+
|
41 |
+
for item in questions_data:
|
42 |
+
task_id = item.get("task_id")
|
43 |
+
question_text = item.get("question")
|
44 |
+
if not task_id or not question_text:
|
45 |
+
continue
|
46 |
+
try:
|
47 |
+
answer = agent(question_text)
|
48 |
+
results_log.append({
|
49 |
+
"Task ID": task_id,
|
50 |
+
"Question": question_text,
|
51 |
+
"Submitted Answer": answer
|
52 |
+
})
|
53 |
+
answers_payload.append({
|
54 |
+
"task_id": task_id,
|
55 |
+
"submitted_answer": answer
|
56 |
+
})
|
57 |
+
except Exception as e:
|
58 |
+
results_log.append({
|
59 |
+
"Task ID": task_id,
|
60 |
+
"Question": question_text,
|
61 |
+
"Submitted Answer": f"ERROR: {e}"
|
62 |
+
})
|
63 |
+
|
64 |
+
return "β
Answers generated. You can now submit.", pd.DataFrame(results_log), answers_payload
|
65 |
+
|
66 |
+
# --- Submit answers only ---
|
67 |
+
def submit_only(profile: gr.OAuthProfile | None, answers_payload: list | None):
|
68 |
+
if not profile:
|
69 |
+
return "Please login first.", None
|
70 |
+
if not answers_payload:
|
71 |
+
return "No answers to submit. Run the agent first.", None
|
72 |
+
|
73 |
+
username = profile.username
|
74 |
+
space_id = os.getenv("SPACE_ID")
|
75 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
76 |
+
submit_url = f"{DEFAULT_API_URL}/submit"
|
77 |
+
|
78 |
+
try:
|
79 |
+
submission_data = {
|
80 |
+
"username": username.strip(),
|
81 |
+
"agent_code": agent_code,
|
82 |
+
"answers": answers_payload
|
83 |
+
}
|
84 |
+
response = requests.post(submit_url, json=submission_data, timeout=30)
|
85 |
+
response.raise_for_status()
|
86 |
+
result_data = response.json()
|
87 |
+
final_status = (
|
88 |
+
f"π Submission Successful!\n"
|
89 |
+
f"π€ User: {result_data.get('username')}\n"
|
90 |
+
f"β
Score: {result_data.get('score')}%\n"
|
91 |
+
f"π― Correct: {result_data.get('correct_count')}/{result_data.get('total_attempted')}"
|
92 |
+
)
|
93 |
+
return final_status, None
|
94 |
+
except Exception as e:
|
95 |
+
return f"β Submission failed: {e}", None
|
96 |
+
|
97 |
+
# --- Gradio Interface ---
|
98 |
+
with gr.Blocks() as demo:
|
99 |
+
gr.Markdown("# π€ Basic Agent Evaluation Runner")
|
100 |
+
gr.Markdown("""
|
101 |
+
**Steps to Complete Your Hugging Face Certification:**
|
102 |
+
1. Clone this Space and build your own logic in the `BasicAgent`.
|
103 |
+
2. Log in using the Hugging Face button.
|
104 |
+
3. Click `Run Agent` to generate answers.
|
105 |
+
4. Once ready, click `Submit` to send your answers and view your score.
|
106 |
+
""")
|
107 |
+
|
108 |
+
gr.LoginButton()
|
109 |
+
|
110 |
+
run_button = gr.Button("π§ Run Agent (Generate Answers)")
|
111 |
+
submit_button = gr.Button("π€ Submit All Answers")
|
112 |
+
|
113 |
+
status_output = gr.Textbox(label="Status", lines=5, interactive=False)
|
114 |
+
results_table = gr.DataFrame(label="Agent Answers", wrap=True)
|
115 |
+
|
116 |
+
answers_state = gr.State(value=None)
|
117 |
+
|
118 |
+
run_button.click(
|
119 |
+
fn=run_agent_only,
|
120 |
+
inputs=[gr.OAuthProfile()],
|
121 |
+
outputs=[status_output, results_table, answers_state]
|
122 |
+
)
|
123 |
+
|
124 |
+
submit_button.click(
|
125 |
+
fn=submit_only,
|
126 |
+
inputs=[gr.OAuthProfile(), answers_state],
|
127 |
+
outputs=[status_output, results_table]
|
128 |
+
)
|
129 |
+
|
130 |
+
# --- App Startup Info ---
|
131 |
+
if __name__ == "__main__":
|
132 |
+
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
133 |
+
space_host_startup = os.getenv("SPACE_HOST")
|
134 |
+
space_id_startup = os.getenv("SPACE_ID")
|
135 |
+
|
136 |
+
if space_host_startup:
|
137 |
+
print(f"β
SPACE_HOST: https://{space_host_startup}.hf.space")
|
138 |
+
else:
|
139 |
+
print("βΉοΈ SPACE_HOST not found.")
|
140 |
+
|
141 |
+
if space_id_startup:
|
142 |
+
print(f"β
SPACE_ID: {space_id_startup}")
|
143 |
+
print(f" Repo: https://huggingface.co/spaces/{space_id_startup}")
|
144 |
+
else:
|
145 |
+
print("βΉοΈ SPACE_ID not found.")
|
146 |
+
|
147 |
+
print("-"*(60 + len(" App Starting ")) + "\n")
|
148 |
+
|
149 |
+
demo.launch(debug=True, share=False)
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
requests
|