thomson99 commited on
Commit
847231a
·
verified ·
1 Parent(s): ba17bb4

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +45 -43
  2. 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 wikipedia
11
- wikipedia.set_lang("ar")
12
- from duckduckgo_search import ddg
13
 
14
  # تهيئة النموذج
15
  try:
@@ -34,52 +33,55 @@ class ArticleGenerator:
34
  """البحث عن معلومات حول الموضوع"""
35
  results = []
36
 
37
- # البحث في ويكيبيديا
38
  try:
39
- wiki_results = wikipedia.search(topic)
40
- for title in wiki_results[:2]:
41
- try:
42
- page = wikipedia.page(title)
43
- results.append({
44
- 'source': 'wikipedia',
45
- 'title': page.title,
46
- 'content': page.summary
47
- })
48
- except:
49
- continue
50
- except:
51
- pass
52
-
53
- # البحث في محرك DuckDuckGo
54
- try:
55
- ddg_results = ddg(topic, region='wt-wt', safesearch='Moderate', time='y', max_results=num_results)
56
- for result in ddg_results:
57
- results.append({
58
- 'source': 'web',
59
- 'title': result['title'],
60
- 'content': result['body']
61
- })
62
  except:
63
- pass
64
-
 
 
 
 
 
65
  return results
66
 
67
  def extract_keywords(self, topic):
68
  """استخراج الكلمات المفتاحية من الموضوع"""
69
- prompt = f"""
70
- استخرج الكلمات المفتاحية المهمة المتعلقة بموضوع: {topic}
71
- يجب أن تكون الكلمات مرتبطة بشكل مباشر بالموضوع ومفيدة للبحث.
72
- """
 
 
 
 
 
 
 
 
 
73
 
74
- inputs = self.tokenizer(prompt, return_tensors="pt").to(self.device)
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
- wikipedia-api>=0.5.8
4
- duckduckgo-search>=3.0.0
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