Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -4,19 +4,70 @@ import requests
|
|
4 |
import pytz
|
5 |
import yaml
|
6 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
|
|
|
7 |
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
10 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
11 |
@tool
|
12 |
-
def
|
13 |
#Keep this format for the description / args / args description but feel free to modify the tool
|
14 |
-
"""A tool that
|
15 |
Args:
|
16 |
-
|
17 |
-
|
18 |
"""
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
@tool
|
22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
@@ -55,7 +106,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
55 |
|
56 |
agent = CodeAgent(
|
57 |
model=model,
|
58 |
-
tools=[final_answer], ## add your tools here (don't remove final answer)
|
59 |
max_steps=6,
|
60 |
verbosity_level=1,
|
61 |
grammar=None,
|
|
|
4 |
import pytz
|
5 |
import yaml
|
6 |
from tools.final_answer import FinalAnswerTool
|
7 |
+
from bs4 import BeautifulSoup
|
8 |
+
import pandas as pd
|
9 |
+
import time
|
10 |
|
11 |
from Gradio_UI import GradioUI
|
12 |
|
13 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
14 |
@tool
|
15 |
+
def scrape_drug_reviews(drug:str)-> dataframe: #it's import to specify the return type
|
16 |
#Keep this format for the description / args / args description but feel free to modify the tool
|
17 |
+
"""A tool that calls a scraping function on the drugs.com website to scrape for reviews on target dtug specified in input
|
18 |
Args:
|
19 |
+
drug: the name of the target drug we want to retrieve reviews for, in lower case (e.g. 'flecainide')
|
20 |
+
|
21 |
"""
|
22 |
+
try:
|
23 |
+
data = scrape_drugs_com_reviews(drug)
|
24 |
+
# Get current time in that timezone
|
25 |
+
return data
|
26 |
+
except Exception as e:
|
27 |
+
return f"Error fetching reviews for the target drug you provided: '{drug}'"
|
28 |
+
|
29 |
+
|
30 |
+
def scrape_drugs_com_reviews(drug_name, max_pages=5, sleep_time=2):
|
31 |
+
"""
|
32 |
+
Scrapes user reviews from Drugs.com for a given drug.
|
33 |
+
"""
|
34 |
+
base_url = f"https://www.drugs.com/comments/{drug_name}/"
|
35 |
+
reviews = []
|
36 |
+
|
37 |
+
for page in range(1, max_pages + 1):
|
38 |
+
url = base_url if page == 1 else f"{base_url}?page={page}"
|
39 |
+
response = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'})
|
40 |
+
|
41 |
+
if response.status_code != 200:
|
42 |
+
print(f"Failed to fetch page {page}")
|
43 |
+
break
|
44 |
+
|
45 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
46 |
+
review_blocks = soup.select('.user-comment')
|
47 |
+
|
48 |
+
if not review_blocks:
|
49 |
+
break
|
50 |
+
|
51 |
+
for block in review_blocks:
|
52 |
+
review_text = block.select_one('.user-comment-text')
|
53 |
+
condition = block.select_one('.drug-condition')
|
54 |
+
rating = block.select_one('.rating-score')
|
55 |
+
date = block.select_one('.comment-date')
|
56 |
+
|
57 |
+
reviews.append({
|
58 |
+
"condition": condition.get_text(strip=True) if condition else None,
|
59 |
+
"rating": rating.get_text(strip=True) if rating else None,
|
60 |
+
"review": review_text.get_text(strip=True) if review_text else None,
|
61 |
+
"date": date.get_text(strip=True) if date else None,
|
62 |
+
"source": url
|
63 |
+
})
|
64 |
+
|
65 |
+
print(f"[✓] Page {page} scraped.")
|
66 |
+
time.sleep(sleep_time)
|
67 |
+
|
68 |
+
return pd.DataFrame(reviews)
|
69 |
+
|
70 |
+
|
71 |
|
72 |
@tool
|
73 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
106 |
|
107 |
agent = CodeAgent(
|
108 |
model=model,
|
109 |
+
tools=[scrape_drug_reviews,final_answer], ## add your tools here (don't remove final answer)
|
110 |
max_steps=6,
|
111 |
verbosity_level=1,
|
112 |
grammar=None,
|