Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- app.py +62 -0
- postpartum_agent.py +64 -0
- requirement.txt +7 -0
- 🤗 Postpartum Research Agent.md +22 -0
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from postpartum_agent import PostpartumResearchAgent
|
| 5 |
+
import requests
|
| 6 |
+
|
| 7 |
+
# Instantiate your agent
|
| 8 |
+
agent = PostpartumResearchAgent()
|
| 9 |
+
|
| 10 |
+
# GAIA API base URL
|
| 11 |
+
GAIA_API_URL = "https://huggingface.co/api/gaia"
|
| 12 |
+
|
| 13 |
+
def answer_question(question):
|
| 14 |
+
return agent.run(question)
|
| 15 |
+
|
| 16 |
+
def submit_to_gaia(username, space_link):
|
| 17 |
+
# Get questions
|
| 18 |
+
response = requests.get(f"{GAIA_API_URL}/questions")
|
| 19 |
+
questions = response.json()
|
| 20 |
+
|
| 21 |
+
answers = []
|
| 22 |
+
for q in questions:
|
| 23 |
+
task_id = q['task_id']
|
| 24 |
+
question_text = q['question']
|
| 25 |
+
agent_answer = agent.run(question_text)
|
| 26 |
+
answers.append({
|
| 27 |
+
"task_id": task_id,
|
| 28 |
+
"submitted_answer": agent_answer
|
| 29 |
+
})
|
| 30 |
+
|
| 31 |
+
payload = {
|
| 32 |
+
"username": username,
|
| 33 |
+
"agent_code": space_link,
|
| 34 |
+
"answers": answers
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
submit_response = requests.post(f"{GAIA_API_URL}/submit", json=payload)
|
| 38 |
+
result = submit_response.json()
|
| 39 |
+
|
| 40 |
+
return f"Submitted! Result: {result}"
|
| 41 |
+
|
| 42 |
+
with gr.Blocks() as demo:
|
| 43 |
+
gr.Markdown("# 🤱 Postpartum Research Agent")
|
| 44 |
+
gr.Markdown("Ask any postpartum question and see the AI agent help you!")
|
| 45 |
+
|
| 46 |
+
with gr.Row():
|
| 47 |
+
with gr.Column():
|
| 48 |
+
question = gr.Textbox(label="Your Question", placeholder="How do I handle postpartum fatigue?")
|
| 49 |
+
output = gr.Textbox(label="Answer")
|
| 50 |
+
|
| 51 |
+
with gr.Column():
|
| 52 |
+
username = gr.Textbox(label="Your HF Username", placeholder="Your Hugging Face username")
|
| 53 |
+
space_link = gr.Textbox(label="Your Space Link", placeholder="https://huggingface.co/spaces/YOU/YOUR_SPACE/tree/main")
|
| 54 |
+
submit_btn = gr.Button("Submit to GAIA")
|
| 55 |
+
submit_output = gr.Textbox(label="Submission Result")
|
| 56 |
+
|
| 57 |
+
ask_btn = gr.Button("Get Answer")
|
| 58 |
+
|
| 59 |
+
ask_btn.click(fn=answer_question, inputs=question, outputs=output)
|
| 60 |
+
submit_btn.click(fn=submit_to_gaia, inputs=[username, space_link], outputs=submit_output)
|
| 61 |
+
|
| 62 |
+
demo.launch()
|
postpartum_agent.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# postpartum_agent.py
|
| 2 |
+
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
from datasets import Dataset
|
| 5 |
+
from sentence_transformers import SentenceTransformer, util
|
| 6 |
+
from smolagents.agent import Agent
|
| 7 |
+
|
| 8 |
+
import torch
|
| 9 |
+
|
| 10 |
+
# Prepare knowledge base
|
| 11 |
+
kb_data = [
|
| 12 |
+
{
|
| 13 |
+
"title": "Postpartum Fatigue",
|
| 14 |
+
"content": "It is normal to feel tired after childbirth. Sleep when your baby sleeps, accept help, and eat balanced meals to maintain your energy."
|
| 15 |
+
},
|
| 16 |
+
{
|
| 17 |
+
"title": "Breastfeeding",
|
| 18 |
+
"content": "Breastfeed on demand, check for a good latch, drink water, and see a lactation consultant if needed."
|
| 19 |
+
},
|
| 20 |
+
{
|
| 21 |
+
"title": "Postpartum Depression",
|
| 22 |
+
"content": "If sadness continues for more than two weeks, talk to your doctor. Support groups and therapy can help."
|
| 23 |
+
},
|
| 24 |
+
{
|
| 25 |
+
"title": "Self Care",
|
| 26 |
+
"content": "Take breaks, talk to loved ones, and ask for help. Caring for yourself helps you care for your baby."
|
| 27 |
+
},
|
| 28 |
+
{
|
| 29 |
+
"title": "Healing",
|
| 30 |
+
"content": "Rest, hydrate, and attend check-ups to heal well after childbirth."
|
| 31 |
+
}
|
| 32 |
+
]
|
| 33 |
+
|
| 34 |
+
kb = Dataset.from_list(kb_data)
|
| 35 |
+
embedder = SentenceTransformer('all-MiniLM-L6-v2')
|
| 36 |
+
kb_contents = [d['content'] for d in kb_data]
|
| 37 |
+
kb_embeddings = embedder.encode(kb_contents, convert_to_tensor=True)
|
| 38 |
+
|
| 39 |
+
qa_pipeline = pipeline("text2text-generation", model="google/flan-t5-base")
|
| 40 |
+
|
| 41 |
+
class PostpartumResearchAgent(Agent):
|
| 42 |
+
def __init__(self, name="PostpartumAgent"):
|
| 43 |
+
super().__init__(name=name)
|
| 44 |
+
|
| 45 |
+
def run(self, question: str) -> str:
|
| 46 |
+
query_embedding = embedder.encode(question, convert_to_tensor=True)
|
| 47 |
+
cos_scores = util.pytorch_cos_sim(query_embedding, kb_embeddings)[0]
|
| 48 |
+
top_idx = torch.argmax(cos_scores).item()
|
| 49 |
+
context = kb_data[top_idx]['content']
|
| 50 |
+
|
| 51 |
+
prompt = f"""
|
| 52 |
+
You are a kind postpartum research assistant.
|
| 53 |
+
Use the information below to answer the question clearly and kindly.
|
| 54 |
+
|
| 55 |
+
Information: {context}
|
| 56 |
+
|
| 57 |
+
Question: {question}
|
| 58 |
+
|
| 59 |
+
Answer (one short sentence):
|
| 60 |
+
"""
|
| 61 |
+
|
| 62 |
+
response = qa_pipeline(prompt, max_length=50, do_sample=False)
|
| 63 |
+
answer = response[0]['generated_text'].strip()
|
| 64 |
+
return answer
|
requirement.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
transformers
|
| 3 |
+
datasets
|
| 4 |
+
sentence-transformers
|
| 5 |
+
smolagents
|
| 6 |
+
requests
|
| 7 |
+
torch
|
🤗 Postpartum Research Agent.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 🤗 Postpartum Research Agent
|
| 2 |
+
|
| 3 |
+
This is my final project for the Hugging Face AI Agents Course.
|
| 4 |
+
|
| 5 |
+
- Built with `smolagents`, `transformers`, and `Gradio`
|
| 6 |
+
- Uses a simple postpartum knowledge base
|
| 7 |
+
- Answers GAIA benchmark questions for the final submission
|
| 8 |
+
- Fully reproducible in this public Space
|
| 9 |
+
|
| 10 |
+
## 🚀 How to Use
|
| 11 |
+
|
| 12 |
+
1️⃣ Ask any postpartum question in the left box.
|
| 13 |
+
2️⃣ See the answer from the agent.
|
| 14 |
+
3️⃣ Enter your username & Space link, click **Submit to GAIA**, and get your benchmark score!
|
| 15 |
+
|
| 16 |
+
## 📊 My Goal
|
| 17 |
+
|
| 18 |
+
Achieve at least **30%** on GAIA benchmark to earn the Certificate!
|
| 19 |
+
|
| 20 |
+
---
|
| 21 |
+
|
| 22 |
+
Made with 💙 for new moms.
|