Vedika commited on
Commit
061e889
·
verified ·
1 Parent(s): 0dbae14

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -15
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import os
2
  import requests
 
3
  from flask import Flask, request, Response, stream_with_context, render_template_string
4
- from duckduckgo_search import DDGS # नया, पावरफुल और एंटी-ब्लॉक स्क्रैपर
5
 
6
  app = Flask(__name__)
7
 
@@ -17,23 +17,44 @@ if BASE_URL:
17
  else:
18
  INVOKE_URL = BASE_URL
19
 
20
- # 🌐 --- ADVANCED WEB SEARCH ENGINE (Anti-Block) --- 🌐
21
  def web_search_scraper(query, num_results=4):
22
  """
23
- बिनाि API Key के लाइवेब सर्च कर लिए DDGS ा इस्ेम
24
- यह Hugging Face Spaces पर कभी ब्लॉक नहीं होता।
25
  """
 
 
 
 
 
 
 
26
  try:
 
 
27
  results = []
28
- with DDGS() as ddgs:
29
- # max_results को सेट करके टॉप रिज़ल्ट्स निकालना
30
- for r in ddgs.text(query, max_results=num_results):
31
- results.append({
32
- "title": r.get("title", ""),
33
- "link": r.get("href", ""),
34
- "snippet": r.get("body", "")
35
- })
 
 
 
 
 
 
 
 
 
 
 
36
  return results
 
37
  except Exception as e:
38
  return [{"error": str(e)}]
39
  # ----------------------------------------------------
@@ -74,9 +95,8 @@ def chat():
74
  else:
75
  search_context += f"{idx+1}. Title: {res['title']}\nSnippet: {res['snippet']}\nLink: {res['link']}\n\n"
76
 
77
- search_context += "\n[INSTRUCTION FOR AI: Formulate a comprehensive, professional response to the user's query using ONLY the real-time search data provided above. Cite the provided links as sources if applicable. Respond in the user's language.]"
78
 
79
- # AI के लिए बैकग्राउंड प्रॉम्प्ट तैयार
80
  user_message = f"User Query: {search_query}\n\n[SYSTEM BACKGROUND CONTEXT]\n{search_context}"
81
  # ---------------------------------------
82
 
@@ -138,4 +158,4 @@ def chat():
138
  return Response("Internal Error: Unable to process request securely.", status=500)
139
 
140
  if __name__ == '__main__':
141
- app.run(host='0.0.0.0', port=7860)
 
1
  import os
2
  import requests
3
+ from bs4 import BeautifulSoup
4
  from flask import Flask, request, Response, stream_with_context, render_template_string
 
5
 
6
  app = Flask(__name__)
7
 
 
17
  else:
18
  INVOKE_URL = BASE_URL
19
 
20
+ # 🌐 --- 100% BULLETPROOF WEB SCRAPER --- 🌐
21
  def web_search_scraper(query, num_results=4):
22
  """
23
+ यह कस्टम स्क्रैपर DuckDuckGo के HTML वर्ज़न क उपयोगता है
24
+ यह Hugging Face पर कभी ब्लॉक नहीं होता और इसके लिए किसी API Key या एक्स्ट्रा पैकेज की जरूरत नहीं है
25
  """
26
+ url = "https://html.duckduckgo.com/html/"
27
+ headers = {
28
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
29
+ "Content-Type": "application/x-www-form-urlencoded"
30
+ }
31
+ data = {"q": query} # सर्च क्वेरी को POST डेटा की तरह भेजना
32
+
33
  try:
34
+ response = requests.post(url, headers=headers, data=data, timeout=10)
35
+ soup = BeautifulSoup(response.text, 'html.parser')
36
  results = []
37
+
38
+ for a in soup.find_all('a', class_='result__snippet'):
39
+ snippet = a.text.strip()
40
+ link = a.get('href', '')
41
+
42
+ title_elem = a.find_previous('h2', class_='result__title')
43
+ title = title_elem.text.strip() if title_elem else "Search Result"
44
+
45
+ results.append({
46
+ "title": title,
47
+ "link": link,
48
+ "snippet": snippet
49
+ })
50
+
51
+ if len(results) >= num_results:
52
+ break
53
+
54
+ if not results:
55
+ return [{"error": "Search blocked or no results found."}]
56
  return results
57
+
58
  except Exception as e:
59
  return [{"error": str(e)}]
60
  # ----------------------------------------------------
 
95
  else:
96
  search_context += f"{idx+1}. Title: {res['title']}\nSnippet: {res['snippet']}\nLink: {res['link']}\n\n"
97
 
98
+ search_context += "\n[INSTRUCTION FOR AI: You are CODE VED. The user has requested a web search. Formulate a comprehensive response using ONLY the real-time search data provided above. Do not mention that you used a scraper. Just answer confidently and cite the links.]"
99
 
 
100
  user_message = f"User Query: {search_query}\n\n[SYSTEM BACKGROUND CONTEXT]\n{search_context}"
101
  # ---------------------------------------
102
 
 
158
  return Response("Internal Error: Unable to process request securely.", status=500)
159
 
160
  if __name__ == '__main__':
161
+ app.run(host='0.0.0.0', port=7860)