requirements.txt

#1
by AnesKAM - opened
Files changed (1) hide show
  1. app.py +62 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py داخل Hugging Face Space لخدمة البحث
2
+ import gradio as gr
3
+ import requests
4
+ from bs4 import BeautifulSoup
5
+ import urllib.parse
6
+ import re
7
+
8
+ # دالة البحث والزحف
9
+ def perform_duckduckgo_scrape(query, max_chars=1500):
10
+ if not query:
11
+ return "الرجاء تقديم استعلام بحث."
12
+
13
+ encoded_query = urllib.parse.quote(query)
14
+ search_url = f"https://duckduckgo.com/html/?q={encoded_query}"
15
+
16
+ headers = {
17
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
18
+ }
19
+
20
+ print(f"--- تلقي طلب بحث عن: '{query}' ---")
21
+
22
+ scraped_content = ""
23
+ try:
24
+ response = requests.get(search_url, headers=headers, timeout=20)
25
+ response.raise_for_status()
26
+ soup = BeautifulSoup(response.text, 'html.parser')
27
+
28
+ result_snippets = soup.find_all('a', class_='result__snippet')
29
+ if not result_snippets:
30
+ result_snippets = soup.find_all('div', class_='result__body')
31
+
32
+ for snippet in result_snippets:
33
+ text = snippet.get_text().strip()
34
+ if text:
35
+ scraped_content += text + "\n"
36
+
37
+ if not scraped_content and soup.find('meta', {'name': 'description'}):
38
+ scraped_content = soup.find('meta', {'name': 'description'})['content'] + "\n"
39
+
40
+ if len(scraped_content) > max_chars:
41
+ scraped_content = scraped_content[:max_chars] + "..."
42
+
43
+ if scraped_content:
44
+ print("معلومات تم استخراجها بنجاح.")
45
+ return scraped_content
46
+ else:
47
+ print("لم يتم العثور على مقتطفات نصية ذات صلة.")
48
+ return "لم يتم العثور على معلومات ذات صلة عبر DuckDuckGo."
49
+
50
+ except Exception as e:
51
+ print(f"خطأ أثناء البحث/الزحف: {e}")
52
+ return f"حدث خطأ أثناء البحث في الويب: {str(e)}"
53
+
54
+ # إنشاء واجهة Gradio (هذا هو ما سيوفر نقطة نهاية الـ API)
55
+ iface = gr.Interface(
56
+ fn=perform_duckduckgo_scrape,
57
+ inputs=gr.Textbox(lines=2, label="استعلام البحث (باللغة العربية)"),
58
+ outputs=gr.Textbox(lines=10, label="النتائج المستخرجة"),
59
+ title="أداة البحث في DuckDuckGo (مقدمة من Pi-1)"
60
+ )
61
+
62
+ iface.launch(share=False)