hd-hg commited on
Commit
88feb1b
·
verified ·
1 Parent(s): 7f3fc08

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -12
app.py CHANGED
@@ -8,6 +8,7 @@ from tools.web_search import DuckDuckGoSearchTool
8
  import duckduckgo_search
9
  import requests
10
  from bs4 import BeautifulSoup
 
11
  from typing import List, Dict, Any, Optional
12
  from Gradio_UI import GradioUI
13
 
@@ -19,16 +20,11 @@ def get_healthy_cheat_meal(diet_type: str) -> List[Dict[str, Any]]:
19
  When a user provides a diet type—such as Keto, Paleo, Vegan, Vegetarian, or Non-Vegetarian—retrieve and summarize
20
  reliable, up-to-date information on healthy sweet dish alternatives.
21
 
22
- Ensure that your responses include:
23
- - Clear Recipe Details: List ingredients and nutritional benefits.
24
- - Diet-Specific Adaptations: Highlight modifications and substitutions relevant to the specified diet type.
25
- - Credible Sources: Rely on trusted culinary and nutritional resources to ensure accuracy and quality.
26
-
27
  Args:
28
  diet_type (str): User-provided diet type.
29
 
30
  Returns:
31
- List[Dict[str, Any]]: A list of dictionaries with the following structure:
32
  {
33
  "recipe_name": str, # The name of the recipe.
34
  "source": str # A credible source or URL where the recipe was found.
@@ -38,8 +34,15 @@ def get_healthy_cheat_meal(diet_type: str) -> List[Dict[str, Any]]:
38
  query = f"healthy {diet_type} sweet dish recipe with ingredients and instructions"
39
 
40
  try:
41
- # Perform web search using DuckDuckGo
42
- search_results = duckduckgo_search.ddg(query, max_results=5)
 
 
 
 
 
 
 
43
 
44
  if not search_results:
45
  return [{"error": "No recipes found. Try a different diet type or check your query."}]
@@ -47,16 +50,26 @@ def get_healthy_cheat_meal(diet_type: str) -> List[Dict[str, Any]]:
47
  recipes = []
48
 
49
  for result in search_results:
50
- recipes.append({
51
- "recipe_name": result.get("title", "Unknown Recipe"),
52
- "source": result.get("href", "No source available")
53
- })
 
 
 
 
 
54
 
 
 
 
 
55
  return recipes
56
 
57
  except Exception as e:
58
  return [{"error": f"An error occurred while searching: {str(e)}"}]
59
 
 
60
  # Initialize tools
61
  final_answer = FinalAnswerTool()
62
  web_search = DuckDuckGoSearchTool()
 
8
  import duckduckgo_search
9
  import requests
10
  from bs4 import BeautifulSoup
11
+ import re
12
  from typing import List, Dict, Any, Optional
13
  from Gradio_UI import GradioUI
14
 
 
20
  When a user provides a diet type—such as Keto, Paleo, Vegan, Vegetarian, or Non-Vegetarian—retrieve and summarize
21
  reliable, up-to-date information on healthy sweet dish alternatives.
22
 
 
 
 
 
 
23
  Args:
24
  diet_type (str): User-provided diet type.
25
 
26
  Returns:
27
+ A list of at least 5 sweet dishes dictionaries with the following structure:
28
  {
29
  "recipe_name": str, # The name of the recipe.
30
  "source": str # A credible source or URL where the recipe was found.
 
34
  query = f"healthy {diet_type} sweet dish recipe with ingredients and instructions"
35
 
36
  try:
37
+ # Perform web search using DuckDuckGo with specific parameters
38
+ search_results = ddg(
39
+ query,
40
+ region="us-en",
41
+ safesearch="moderate",
42
+ time=None,
43
+ max_results=5,
44
+ output='json'
45
+ )
46
 
47
  if not search_results:
48
  return [{"error": "No recipes found. Try a different diet type or check your query."}]
 
50
  recipes = []
51
 
52
  for result in search_results:
53
+ # Extract title and link from the results
54
+ title = result.get('title', '').split(' | ')[0] # Clean up title by removing site name
55
+ link = result.get('link', '')
56
+
57
+ if title and link:
58
+ recipes.append({
59
+ "recipe_name": title.strip(),
60
+ "source": link
61
+ })
62
 
63
+ # Ensure we have at least some results
64
+ if not recipes:
65
+ return [{"error": "No valid recipes found. Please try a different search."}]
66
+
67
  return recipes
68
 
69
  except Exception as e:
70
  return [{"error": f"An error occurred while searching: {str(e)}"}]
71
 
72
+
73
  # Initialize tools
74
  final_answer = FinalAnswerTool()
75
  web_search = DuckDuckGoSearchTool()