XMMR12 commited on
Commit
5c75995
·
verified ·
1 Parent(s): dab9acc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -21
app.py CHANGED
@@ -49,27 +49,28 @@ def search_full_plant_information(plant_name:str):
49
  """ """
50
  query = f"{plant_name} plant medicinal uses scientific information site:.edu OR site:.gov OR site:.org"
51
  search_results=""
52
- time.sleep(1)
53
- try:
54
- search_results = DDGS().text(keywords=query, max_results=5)
55
- except Exception as e :
56
- pass
57
- if search_results:
58
- content=""
59
- for result in search_results:
60
- #content+=requests.get(f"{specialtoken}/Analyze this:{result['body']}").text
61
- content+=result['body']+" "
62
- content+=fetch_page_content(result['href'])+" "
63
- time.sleep(2)
64
- prompt = PROMPT_TEMPLATE.format(plant_name=plant_name, content=content)
65
- response = requests.get(f"{specialtoken}/{prompt}")
66
- #print (response.text)
67
- return response.text
68
- else:
69
- content = DDGS().text(keywords=query, max_results=2)[0]['body']
70
- prompt = PROMPT_TEMPLATE.format(plant_name=plant_name, content=content)
71
- response = requests.get(f"{specialtoken}/{prompt}")
72
- return response.text
 
73
  return f"No data Found for {plant_name}!"
74
 
75
  DB_NAME="plants.db"
 
49
  """ """
50
  query = f"{plant_name} plant medicinal uses scientific information site:.edu OR site:.gov OR site:.org"
51
  search_results=""
52
+ for attempt in range(3): # Retry up to 3 times
53
+ try:
54
+ search_results = DDGS().text(keywords=query, max_results=5)
55
+ except DuckDuckGoSearchException as e:
56
+ if "Ratelimit" in str(e):
57
+ wait_time = 2 ** attempt # Exponential backoff
58
+ print(f"Rate limit hit, retrying in {wait_time} seconds...")
59
+ time.sleep(wait_time)
60
+ else:
61
+ raise
62
+ raise Exception("Failed to fetch data after retries due to rate limiting.")
63
+ if search_results:
64
+ content=""
65
+ for result in search_results:
66
+ #content+=requests.get(f"{specialtoken}/Analyze this:{result['body']}").text
67
+ content+=result['body']+" "
68
+ content+=fetch_page_content(result['href'])+" "
69
+ time.sleep(2)
70
+ prompt = PROMPT_TEMPLATE.format(plant_name=plant_name, content=content)
71
+ response = requests.get(f"{specialtoken}/{prompt}")
72
+ #print (response.text)
73
+ return response.text
74
  return f"No data Found for {plant_name}!"
75
 
76
  DB_NAME="plants.db"