Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,128 +1,52 @@
|
|
| 1 |
-
|
| 2 |
import requests
|
| 3 |
from bs4 import BeautifulSoup
|
| 4 |
from googlesearch import search
|
| 5 |
from huggingface_hub import InferenceClient
|
| 6 |
-
from langdetect import detect
|
| 7 |
import os
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
client = InferenceClient("google/gemma-3-4b-it", token=hf_token)
|
| 12 |
|
| 13 |
-
def
|
|
|
|
|
|
|
| 14 |
try:
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
except:
|
| 17 |
-
return
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
# الإصدار الجديد يستخدم num_results بدلاً من num و stop
|
| 24 |
-
links = []
|
| 25 |
-
results = search(full_query, num_results=3, lang="ar")
|
| 26 |
-
for link in results:
|
| 27 |
-
links.append(link)
|
| 28 |
-
|
| 29 |
-
print(f"الروابط المستخرجة: {links}")
|
| 30 |
-
return links
|
| 31 |
-
except Exception as e:
|
| 32 |
-
print(f"فشل البحث في جوجل: {e}")
|
| 33 |
-
return []
|
| 34 |
-
|
| 35 |
-
def scrape_content(url):
|
| 36 |
-
try:
|
| 37 |
-
print(f"جاري استخراج محتوى: {url}")
|
| 38 |
-
headers = {'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'}
|
| 39 |
-
response = requests.get(url, headers=headers, timeout=10)
|
| 40 |
-
response.encoding = 'utf-8'
|
| 41 |
-
soup = BeautifulSoup(response.text, 'html.parser')
|
| 42 |
-
|
| 43 |
-
# بنية إسلام ويب قد تختلف، نحاول أكثر من مكان
|
| 44 |
-
main_content = soup.find('div', {'class': 'item'}) or soup.find('div', {'id': 'fullitem'}) or soup.find('article')
|
| 45 |
-
|
| 46 |
-
if main_content:
|
| 47 |
-
text = main_content.get_text(strip=True)[:4000]
|
| 48 |
-
print("تم استخراج النص بنجاح.")
|
| 49 |
-
return text
|
| 50 |
-
return ""
|
| 51 |
-
except Exception as e:
|
| 52 |
-
print(f"فشل الاستخراج: {e}")
|
| 53 |
-
return ""
|
| 54 |
-
|
| 55 |
-
def islamic_ai_search(question):
|
| 56 |
-
if not question.strip():
|
| 57 |
-
yield "الرجاء كتابة سؤال أولاً.", ""
|
| 58 |
-
return
|
| 59 |
-
|
| 60 |
-
lang = get_language(question)
|
| 61 |
-
|
| 62 |
-
# 1. البحث عن الروابط
|
| 63 |
-
links = search_islamweb(question)
|
| 64 |
-
if not links:
|
| 65 |
-
yield "عذراً، جوجل حظر طلب البحث حالياً. جرب مرة أخرى بعد قليل أو تأكد من إعدادات البحث.", ""
|
| 66 |
-
return
|
| 67 |
-
|
| 68 |
-
# 2. استخراج السياق
|
| 69 |
-
context = ""
|
| 70 |
-
source_link = ""
|
| 71 |
-
for link in links:
|
| 72 |
-
content = scrape_content(link)
|
| 73 |
-
if len(content) > 150:
|
| 74 |
-
context = content
|
| 75 |
-
source_link = link
|
| 76 |
-
break
|
| 77 |
|
| 78 |
if not context:
|
| 79 |
-
|
| 80 |
-
return
|
| 81 |
-
|
| 82 |
-
# 3. نظام الرسائل للنموذج
|
| 83 |
-
system_message = (
|
| 84 |
-
"You are an expert Islamic researcher. Answer the user's question ONLY using the provided context from IslamWeb. "
|
| 85 |
-
"Strict rules: \n"
|
| 86 |
-
"1. If the answer is not in the context, say you don't know.\n"
|
| 87 |
-
"2. Response language MUST be the same as input question language.\n"
|
| 88 |
-
f"Context: {context}"
|
| 89 |
-
)
|
| 90 |
-
|
| 91 |
-
# 4. طلب الإجابة (Streaming)
|
| 92 |
-
response = ""
|
| 93 |
-
try:
|
| 94 |
-
for message in client.chat_completion(
|
| 95 |
-
messages=[
|
| 96 |
-
{"role": "system", "content": system_message},
|
| 97 |
-
{"role": "user", "content": question}
|
| 98 |
-
],
|
| 99 |
-
max_tokens=1000,
|
| 100 |
-
stream=True,
|
| 101 |
-
):
|
| 102 |
-
token = message.choices[0].delta.content
|
| 103 |
-
if token:
|
| 104 |
-
response += token
|
| 105 |
-
yield response, source_link
|
| 106 |
-
except Exception as e:
|
| 107 |
-
print(f"خطأ الـ AI: {e}")
|
| 108 |
-
yield f"حدث خطأ أثناء توليد الإجابة. تأكد من إضافة HF_TOKEN في الـ Secrets. الخطأ: {e}", source_link
|
| 109 |
|
| 110 |
-
|
| 111 |
-
with gr.Blocks() as demo:
|
| 112 |
-
gr.Markdown("# 🕋 الباحث الإسلامي الذكي")
|
| 113 |
-
|
| 114 |
-
with gr.Row():
|
| 115 |
-
input_text = gr.Textbox(label="سؤالك / Sorunuz")
|
| 116 |
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
inputs=input_text,
|
| 125 |
-
outputs=[output_answer, output_source]
|
| 126 |
)
|
| 127 |
-
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
import requests
|
| 3 |
from bs4 import BeautifulSoup
|
| 4 |
from googlesearch import search
|
| 5 |
from huggingface_hub import InferenceClient
|
|
|
|
| 6 |
import os
|
| 7 |
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
client = InferenceClient("google/gemma-3-4b-it", token=os.getenv("HF_TOKEN"))
|
|
|
|
| 10 |
|
| 11 |
+
def search_and_scrape(question):
|
| 12 |
+
# نفس منطق البحث والاستخراج السابق
|
| 13 |
+
full_query = f"site:islamweb.net {question}"
|
| 14 |
try:
|
| 15 |
+
results = search(full_query, num_results=1, lang="ar")
|
| 16 |
+
link = next(results)
|
| 17 |
+
headers = {'User-Agent': 'Mozilla/5.0'}
|
| 18 |
+
resp = requests.get(link, headers=headers, timeout=5)
|
| 19 |
+
soup = BeautifulSoup(resp.text, 'html.parser')
|
| 20 |
+
content = soup.find('div', {'class': 'item'}).get_text(strip=True)[:3000]
|
| 21 |
+
return content, link
|
| 22 |
except:
|
| 23 |
+
return None, None
|
| 24 |
|
| 25 |
+
@app.post("/ask")
|
| 26 |
+
async def ask_ai(data: dict):
|
| 27 |
+
question = data.get("question")
|
| 28 |
+
context, link = search_and_scrape(question)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
if not context:
|
| 31 |
+
return {"answer": "لم أجد نتائج.", "source": ""}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
system_msg = f"Answer only from context: {context}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
# طلب الإجابة من الموديل
|
| 36 |
+
response = client.chat_completion(
|
| 37 |
+
messages=[
|
| 38 |
+
{"role": "system", "content": system_msg},
|
| 39 |
+
{"role": "user", "content": question}
|
| 40 |
+
],
|
| 41 |
+
max_tokens=500
|
|
|
|
|
|
|
| 42 |
)
|
| 43 |
+
|
| 44 |
+
return {
|
| 45 |
+
"answer": response.choices[0].message.content,
|
| 46 |
+
"source": link
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
# تشغيل FastAPI بدلاً من Gradio
|
| 50 |
+
if __name__ == "__main__":
|
| 51 |
+
import uvicorn
|
| 52 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|