Ani14 commited on
Commit
0557f2f
·
verified ·
1 Parent(s): c0c8004

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -4
app.py CHANGED
@@ -12,7 +12,7 @@ from PIL import Image
12
  from io import BytesIO
13
  from fpdf import FPDF
14
  import base64
15
- from duckduckgo_search import ddg_images
16
 
17
  # Load environment variables
18
  load_dotenv()
@@ -51,13 +51,14 @@ def call_llm(messages, model="deepseek/deepseek-chat-v3-0324:free", max_tokens=3
51
  except json.JSONDecodeError:
52
  pass
53
 
 
 
54
  def get_sources(topic, domains=None):
55
  query = topic
56
  if domains:
57
  domain_filters = [d.strip() for d in domains.split(",") if d.strip()]
58
  query += " site:" + " OR site:".join(domain_filters)
59
 
60
- # Fetch search results from Tavily
61
  response = tavily.search(query=query, search_depth="advanced", max_results=10)
62
  results = []
63
 
@@ -67,8 +68,11 @@ def get_sources(topic, domains=None):
67
  # Fallback to DuckDuckGo image search if Tavily doesn't return an image
68
  if not image_url:
69
  try:
70
- ddg_results = ddg_images(r["title"], max_results=1)
71
- image_url = ddg_results[0]["image"] if ddg_results else None
 
 
 
72
  except Exception as e:
73
  print(f"DuckDuckGo image search failed for '{r['title']}': {e}")
74
  image_url = None
 
12
  from io import BytesIO
13
  from fpdf import FPDF
14
  import base64
15
+ from duckduckgo_search import DDGS
16
 
17
  # Load environment variables
18
  load_dotenv()
 
51
  except json.JSONDecodeError:
52
  pass
53
 
54
+
55
+
56
  def get_sources(topic, domains=None):
57
  query = topic
58
  if domains:
59
  domain_filters = [d.strip() for d in domains.split(",") if d.strip()]
60
  query += " site:" + " OR site:".join(domain_filters)
61
 
 
62
  response = tavily.search(query=query, search_depth="advanced", max_results=10)
63
  results = []
64
 
 
68
  # Fallback to DuckDuckGo image search if Tavily doesn't return an image
69
  if not image_url:
70
  try:
71
+ with DDGS() as ddgs:
72
+ ddgs_images_gen = ddgs.images(r["title"])
73
+ image_result = next(ddgs_images_gen, None)
74
+ if image_result:
75
+ image_url = image_result.get("image")
76
  except Exception as e:
77
  print(f"DuckDuckGo image search failed for '{r['title']}': {e}")
78
  image_url = None