Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +45 -43
- requirements.txt +2 -2
app.py
CHANGED
@@ -7,9 +7,8 @@ from bidi.algorithm import get_display
|
|
7 |
import re
|
8 |
from collections import Counter
|
9 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
10 |
-
import
|
11 |
-
|
12 |
-
from duckduckgo_search import ddg
|
13 |
|
14 |
# تهيئة النموذج
|
15 |
try:
|
@@ -34,52 +33,55 @@ class ArticleGenerator:
|
|
34 |
"""البحث عن معلومات حول الموضوع"""
|
35 |
results = []
|
36 |
|
37 |
-
# البحث في ويكيبيديا
|
38 |
try:
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
'content': result['body']
|
61 |
-
})
|
62 |
except:
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
65 |
return results
|
66 |
|
67 |
def extract_keywords(self, topic):
|
68 |
"""استخراج الكلمات المفتاحية من الموضوع"""
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
-
|
75 |
-
outputs = self.model.generate(
|
76 |
-
inputs["input_ids"],
|
77 |
-
max_length=200,
|
78 |
-
temperature=0.7,
|
79 |
-
num_return_sequences=1
|
80 |
-
)
|
81 |
-
keywords = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
82 |
-
return [kw.strip() for kw in keywords.split(',')]
|
83 |
|
84 |
def generate_content_with_research(self, topic, style):
|
85 |
"""توليد محتوى مبني على البحث"""
|
@@ -88,7 +90,7 @@ class ArticleGenerator:
|
|
88 |
|
89 |
# البحث عن كل كلمة مفتاحية
|
90 |
all_research = []
|
91 |
-
for keyword in keywords:
|
92 |
search_results = self.search_topic(f"{topic} {keyword}")
|
93 |
all_research.extend(search_results)
|
94 |
|
|
|
7 |
import re
|
8 |
from collections import Counter
|
9 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
10 |
+
import requests
|
11 |
+
from bs4 import BeautifulSoup
|
|
|
12 |
|
13 |
# تهيئة النموذج
|
14 |
try:
|
|
|
33 |
"""البحث عن معلومات حول الموضوع"""
|
34 |
results = []
|
35 |
|
|
|
36 |
try:
|
37 |
+
# استخدام محرك بحث عربي
|
38 |
+
search_url = f"https://www.google.com/search?q={topic}&hl=ar"
|
39 |
+
headers = {
|
40 |
+
'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'
|
41 |
+
}
|
42 |
+
response = requests.get(search_url, headers=headers)
|
43 |
+
|
44 |
+
if response.status_code == 200:
|
45 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
46 |
+
search_results = soup.find_all('div', class_='g')
|
47 |
+
|
48 |
+
for result in search_results[:num_results]:
|
49 |
+
title_elem = result.find('h3')
|
50 |
+
snippet_elem = result.find('div', class_='VwiC3b')
|
51 |
+
|
52 |
+
if title_elem and snippet_elem:
|
53 |
+
results.append({
|
54 |
+
'source': 'web',
|
55 |
+
'title': title_elem.text,
|
56 |
+
'content': snippet_elem.text
|
57 |
+
})
|
|
|
|
|
58 |
except:
|
59 |
+
# في حالة فشل البحث، استخدم محتوى افتراضي
|
60 |
+
results.append({
|
61 |
+
'source': 'default',
|
62 |
+
'title': f'معلومات عن {topic}',
|
63 |
+
'content': f'يعتبر موضوع {topic} من المواضيع المهمة في وقتنا الحاضر.'
|
64 |
+
})
|
65 |
+
|
66 |
return results
|
67 |
|
68 |
def extract_keywords(self, topic):
|
69 |
"""استخراج الكلمات المفتاحية من الموضوع"""
|
70 |
+
# قائمة من الكلمات المفتاحية المحتملة
|
71 |
+
potential_keywords = [
|
72 |
+
f"تعريف {topic}",
|
73 |
+
f"أهمية {topic}",
|
74 |
+
f"فوائد {topic}",
|
75 |
+
f"أنواع {topic}",
|
76 |
+
f"مميزات {topic}",
|
77 |
+
f"تطبيقات {topic}",
|
78 |
+
f"تاريخ {topic}",
|
79 |
+
f"مستقبل {topic}",
|
80 |
+
f"تحديات {topic}",
|
81 |
+
f"حلول {topic}"
|
82 |
+
]
|
83 |
|
84 |
+
return potential_keywords
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
|
86 |
def generate_content_with_research(self, topic, style):
|
87 |
"""توليد محتوى مبني على البحث"""
|
|
|
90 |
|
91 |
# البحث عن كل كلمة مفتاحية
|
92 |
all_research = []
|
93 |
+
for keyword in keywords[:3]: # نأخذ أول 3 كلمات مفتاحية فقط
|
94 |
search_results = self.search_topic(f"{topic} {keyword}")
|
95 |
all_research.extend(search_results)
|
96 |
|
requirements.txt
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
transformers>=4.30.0
|
2 |
torch>=2.0.0
|
3 |
-
|
4 |
-
|
5 |
gradio>=4.0.0
|
6 |
python-dotenv==1.0.0
|
7 |
sentencepiece==0.1.99
|
|
|
1 |
transformers>=4.30.0
|
2 |
torch>=2.0.0
|
3 |
+
requests>=2.25.1
|
4 |
+
beautifulsoup4>=4.9.3
|
5 |
gradio>=4.0.0
|
6 |
python-dotenv==1.0.0
|
7 |
sentencepiece==0.1.99
|