Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,51 +1,70 @@
|
|
1 |
-
import os
|
2 |
-
import spaces
|
3 |
import urllib.parse
|
4 |
import requests
|
5 |
import gradio as gr
|
6 |
from transformers import pipeline
|
7 |
|
|
|
8 |
SEARCH_TEMPLATE = "https://ar.wikipedia.org/w/api.php?action=opensearch&search=%s&limit=1&namespace=0&format=json"
|
9 |
CONTENT_TEMPLATE = "https://ar.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro&explaintext&redirects=1&titles=%s"
|
10 |
|
11 |
-
|
|
|
12 |
|
13 |
-
@spaces.GPU
|
14 |
def search_wikipedia(query):
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
def chatbot_response(message, history):
|
|
|
28 |
summary, source = search_wikipedia(message)
|
29 |
-
response = f"
|
30 |
if source:
|
31 |
-
response += f"\n\
|
32 |
history.append((message, response))
|
33 |
return history
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
gr.Markdown("
|
39 |
-
gr.Markdown("This chatbot queries the Wikipedia API and summarizes the top result.")
|
40 |
|
41 |
chatbot = gr.Chatbot()
|
42 |
-
msg = gr.Textbox(label="
|
43 |
-
clear = gr.Button("
|
44 |
|
45 |
msg.submit(chatbot_response, [msg, chatbot], chatbot).then(
|
46 |
-
lambda:
|
47 |
)
|
48 |
-
clear.click(lambda:
|
49 |
|
50 |
if __name__ == "__main__":
|
51 |
-
demo.launch()
|
|
|
|
|
|
|
1 |
import urllib.parse
|
2 |
import requests
|
3 |
import gradio as gr
|
4 |
from transformers import pipeline
|
5 |
|
6 |
+
# 🟢 API Templates للحصول على بيانات ويكيبيديا
|
7 |
SEARCH_TEMPLATE = "https://ar.wikipedia.org/w/api.php?action=opensearch&search=%s&limit=1&namespace=0&format=json"
|
8 |
CONTENT_TEMPLATE = "https://ar.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro&explaintext&redirects=1&titles=%s"
|
9 |
|
10 |
+
# 🔥 استخدم نموذج يدعم اللغة العربية مثل "facebook/bart-large-cnn" أو أي نموذج متخصص
|
11 |
+
summarizer = pipeline("summarization", model="facebook/mbart-large-50", tokenizer="facebook/mbart-large-50")
|
12 |
|
|
|
13 |
def search_wikipedia(query):
|
14 |
+
""" البحث في ويكيبيديا العربية وإرجاع ملخص من المقال الأول. """
|
15 |
+
query_encoded = urllib.parse.quote_plus(query)
|
16 |
+
search_response = requests.get(SEARCH_TEMPLATE % query_encoded).json()
|
17 |
+
|
18 |
+
if not search_response or not search_response[1]:
|
19 |
+
return "❌ لم يتم العثور على نتائج.", ""
|
20 |
+
|
21 |
+
# 🟢 جلب أول نتيجة
|
22 |
+
page_title = search_response[1][0]
|
23 |
+
page_encoded = urllib.parse.quote_plus(page_title)
|
24 |
+
|
25 |
+
# 🟢 جلب محتوى المقالة
|
26 |
+
content_response = requests.get(CONTENT_TEMPLATE % page_encoded).json()
|
27 |
+
pages = content_response.get("query", {}).get("pages", {})
|
28 |
+
|
29 |
+
if not pages:
|
30 |
+
return "❌ لم يتم العثور على المحتوى.", ""
|
31 |
+
|
32 |
+
content = list(pages.values())[0].get("extract", "")
|
33 |
+
|
34 |
+
if not content:
|
35 |
+
return "❌ المقالة لا تحتوي على معلومات كافية.", ""
|
36 |
+
|
37 |
+
# 🟢 تحسين التلخيص بناءً على طول المقالة
|
38 |
+
max_length = 200 if len(content) > 1000 else 100
|
39 |
+
min_length = 50 if len(content) > 500 else 30
|
40 |
+
|
41 |
+
summary = summarizer(content, max_length=max_length, min_length=min_length, do_sample=False)[0]['summary_text']
|
42 |
+
|
43 |
+
source = search_response[3][0] # رابط المقال الأصلي
|
44 |
+
return summary, source
|
45 |
|
46 |
def chatbot_response(message, history):
|
47 |
+
""" دالة التفاعل مع المستخدم """
|
48 |
summary, source = search_wikipedia(message)
|
49 |
+
response = f"🔹 **ملخص ويكيبيديا:**\n\n{summary}"
|
50 |
if source:
|
51 |
+
response += f"\n\n🔗 **المصدر:** [اضغط هنا]({source})"
|
52 |
history.append((message, response))
|
53 |
return history
|
54 |
|
55 |
+
# 🔥 واجهة Gradio المحسنة
|
56 |
+
with gr.Blocks() as demo:
|
57 |
+
gr.Markdown("# 🤖 بوت ويكيبيديا العربية")
|
58 |
+
gr.Markdown("🔹 هذا البوت يستخدم ويكيبيديا العربية للبحث عن المعلومات وإعطاء ملخص عنها.")
|
|
|
59 |
|
60 |
chatbot = gr.Chatbot()
|
61 |
+
msg = gr.Textbox(label="🔍 اكتب سؤالك هنا:")
|
62 |
+
clear = gr.Button("🧹 مسح المحادثة")
|
63 |
|
64 |
msg.submit(chatbot_response, [msg, chatbot], chatbot).then(
|
65 |
+
lambda _: "", None, [msg], queue=False # 🟢 تصحيح التحديث التلقائي لحقل الإدخال
|
66 |
)
|
67 |
+
clear.click(lambda: [], None, chatbot, queue=False)
|
68 |
|
69 |
if __name__ == "__main__":
|
70 |
+
demo.launch()
|