Zeba15 commited on
Commit
7d9c229
Β·
verified Β·
1 Parent(s): 2b1c25c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -80
app.py CHANGED
@@ -1,103 +1,93 @@
1
  # app.py
2
 
 
3
  import gradio as gr
4
- from postpartum_agent import PostpartumResearchAgent
5
  import requests
6
- import transformers
 
 
7
 
8
  # Instantiate your agent
9
  agent = PostpartumResearchAgent()
10
 
11
- # GAIA API base URL
12
- GAIA_API_URL = "https://huggingface.co/api/gaia"
13
 
 
14
  def answer_question(question):
15
  return agent.kb_search(question)
16
- # def run(self, question):
17
- # return self.kb_search(question)
18
-
19
-
20
- def submit_to_gaia(username, space_link):
21
- GAIA_API_URL = "https://huggingface.co/api/gaia"
22
-
23
- try:
24
- # Step 1: Fetch questions
25
- response = requests.get(f"{GAIA_API_URL}/questions")
26
- if response.status_code != 200:
27
- return f"❌ Failed to fetch tasks. HTTP {response.status_code}: {response.text}"
28
-
29
- questions = response.json()
30
- if not isinstance(questions, list):
31
- return f"❌ Unexpected response format: {questions}"
32
-
33
- answers = []
34
- for idx, q in enumerate(questions):
35
- try:
36
- task_id = q["task_id"]
37
- question_text = q["question"]
38
- print(f"➑️ [{idx+1}/{len(questions)}] Answering: {question_text}")
39
-
40
- agent_answer = agent.search_advice(question_text)
41
- cleaned_answer = (agent_answer or "").strip()
42
-
43
- if not cleaned_answer:
44
- print(f"⚠️ Warning: Empty answer for task {task_id}")
45
-
46
- answers.append({
47
- "task_id": task_id,
48
- "submitted_answer": cleaned_answer
49
- })
50
-
51
- except Exception as inner_e:
52
- print(f"❌ Error on question {idx+1}: {inner_e}")
53
- continue
54
-
55
- if not answers:
56
- return "❌ No answers were collected. All tasks failed or returned empty."
57
-
58
- payload = {
59
- "username": username.strip(),
60
- "agent_code": space_link.strip(),
61
- "answers": answers
62
- }
63
-
64
- print("πŸš€ Submitting answers to GAIA...")
65
- submit_response = requests.post(f"{GAIA_API_URL}/submit", json=payload)
66
-
67
- try:
68
- result = submit_response.json()
69
- except Exception:
70
- return f"❌ Failed to parse submission response:\n{submit_response.text}"
71
-
72
- return f"""βœ… Submission complete!
73
- Score: {result.get('score', 'N/A')}
74
- Status: {result.get('status', 'N/A')}
75
- Message: {result.get('message', 'No message')}
76
- """
77
-
78
- except Exception as e:
79
- return f"❌ Submission failed: {str(e)}"
80
-
81
-
82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
  with gr.Blocks() as demo:
84
  gr.Markdown("# 🀱 Postpartum Research Agent")
85
- gr.Markdown("Ask any postpartum question and see the AI agent help you!")
 
 
 
86
 
87
  with gr.Row():
88
  with gr.Column():
89
- question = gr.Textbox(label="Your Question", placeholder="How do I handle postpartum fatigue?")
90
- output = gr.Textbox(label="Answer")
 
91
 
92
  with gr.Column():
93
- username = gr.Textbox(label="Your HF Username", placeholder="Your Hugging Face username")
94
- space_link = gr.Textbox(label="Your Space Link", placeholder="https://huggingface.co/spaces/YOU/YOUR_SPACE/tree/main")
95
- submit_btn = gr.Button("Submit to GAIA")
96
- submit_output = gr.Textbox(label="Submission Result")
97
-
98
- ask_btn = gr.Button("Get Answer")
99
 
100
  ask_btn.click(fn=answer_question, inputs=question, outputs=output)
101
- submit_btn.click(fn=submit_to_gaia, inputs=[username, space_link], outputs=submit_output)
102
 
103
  demo.launch()
 
1
  # app.py
2
 
3
+ import os
4
  import gradio as gr
 
5
  import requests
6
+ import pandas as pd
7
+
8
+ from postpartum_agent import PostpartumResearchAgent
9
 
10
  # Instantiate your agent
11
  agent = PostpartumResearchAgent()
12
 
13
+ # Correct GAIA Unit 4 scoring API
14
+ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
15
 
16
+ # βœ… Single question: interactive
17
  def answer_question(question):
18
  return agent.kb_search(question)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
+ # βœ… Full submission flow: official Unit 4 style
21
+ def run_and_submit_all(profile: gr.OAuthProfile | None):
22
+ if not profile:
23
+ return "❌ Please log in with your Hugging Face account.", None
24
+
25
+ username = profile.username
26
+ space_id = os.getenv("SPACE_ID")
27
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
28
+
29
+ questions_url = f"{DEFAULT_API_URL}/questions"
30
+ submit_url = f"{DEFAULT_API_URL}/submit"
31
+
32
+ # Fetch GAIA questions
33
+ response = requests.get(questions_url)
34
+ questions_data = response.json()
35
+
36
+ answers_payload = []
37
+ results_log = []
38
+
39
+ for item in questions_data:
40
+ task_id = item["task_id"]
41
+ question_text = item["question"]
42
+ submitted_answer = agent.kb_search(question_text)
43
+ answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
44
+ results_log.append({
45
+ "Task ID": task_id,
46
+ "Question": question_text,
47
+ "Submitted Answer": submitted_answer
48
+ })
49
+
50
+ payload = {
51
+ "username": username,
52
+ "agent_code": agent_code,
53
+ "answers": answers_payload
54
+ }
55
+
56
+ submit_response = requests.post(submit_url, json=payload)
57
+ result = submit_response.json()
58
+
59
+ final_status = (
60
+ f"βœ… Submission Complete!\n"
61
+ f"User: {result.get('username')}\n"
62
+ f"Score: {result.get('score', 'N/A')}%\n"
63
+ f"Correct: {result.get('correct_count', '?')}/{result.get('total_attempted', '?')}\n"
64
+ f"Message: {result.get('message', 'No message')}"
65
+ )
66
+
67
+ return final_status, pd.DataFrame(results_log)
68
+
69
+ # βœ… βœ… βœ… Gradio app
70
  with gr.Blocks() as demo:
71
  gr.Markdown("# 🀱 Postpartum Research Agent")
72
+ gr.Markdown(
73
+ "Ask any postpartum question and see the AI agent help you. "
74
+ "When ready, submit all answers to the GAIA leaderboard!"
75
+ )
76
 
77
  with gr.Row():
78
  with gr.Column():
79
+ question = gr.Textbox(label="Your Question")
80
+ output = gr.Textbox(label="Agent Answer")
81
+ ask_btn = gr.Button("Get Answer")
82
 
83
  with gr.Column():
84
+ gr.Markdown("## Submit to GAIA Benchmark")
85
+ gr.LoginButton()
86
+ run_button = gr.Button("Run & Submit All")
87
+ status_output = gr.Textbox(label="Submission Status", lines=5)
88
+ results_table = gr.DataFrame(label="Questions & Answers")
 
89
 
90
  ask_btn.click(fn=answer_question, inputs=question, outputs=output)
91
+ run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
92
 
93
  demo.launch()