Update app.py
Browse files
app.py
CHANGED
|
@@ -1,162 +1,105 @@
|
|
| 1 |
# ------------------------------------------------------------
|
| 2 |
-
# AGI +
|
| 3 |
-
# يرد باللغة نفسها (عربي/إنجليزي) + Terminal Style
|
| 4 |
# ------------------------------------------------------------
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
import torch
|
| 8 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 9 |
import sympy as sp
|
| 10 |
from sympy import symbols, Eq, solve, diff, integrate
|
| 11 |
-
from z3 import Solver, Int, sat
|
| 12 |
import re
|
| 13 |
from collections import deque
|
| 14 |
|
| 15 |
-
# ----------
|
| 16 |
-
model_name = "
|
| 17 |
-
# بديل ممتاز
|
| 18 |
|
| 19 |
-
print("جاري تحميل النموذج ال
|
| 20 |
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 21 |
model = AutoModelForCausalLM.from_pretrained(
|
| 22 |
model_name,
|
| 23 |
-
torch_dtype=torch.float16,
|
| 24 |
device_map="auto",
|
| 25 |
trust_remote_code=True,
|
| 26 |
low_cpu_mem_usage=True
|
| 27 |
)
|
| 28 |
-
model.eval()
|
| 29 |
|
| 30 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
context = deque(maxlen=6)
|
| 32 |
|
| 33 |
# ---------- كشف المسائل الرياضية ----------
|
| 34 |
-
MATH_PATTERN = re.compile(r"[0-9+\-*/().^=]|sin|cos|tan|sqrt|log|exp|اشتق|تكامل|حل|d/dx|integral", re.I)
|
| 35 |
-
|
| 36 |
-
def is_math_question(text: str) -> bool:
|
| 37 |
-
return bool(MATH_PATTERN.search(text))
|
| 38 |
|
| 39 |
-
# ---------- حل
|
| 40 |
-
def
|
| 41 |
x = symbols('x')
|
| 42 |
try:
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
# حساب بسيط
|
| 69 |
-
result = sp.sympify(question)
|
| 70 |
-
return f"**النتيجة:** {result}", f"حساب: {question} = {result}"
|
| 71 |
-
|
| 72 |
-
except Exception as e:
|
| 73 |
-
return None, f"خطأ في الحل: {str(e)}"
|
| 74 |
-
|
| 75 |
-
# ---------- رد ذكي متعدد اللغات (يرد بنفس لغة السؤال) ----------
|
| 76 |
-
def smart_reply(user_text: str) -> str:
|
| 77 |
-
messages = [{"role": "system", "content": "أنت مساعد ذكي يرد بنفس لغة المستخدم (عربي أو إنجليزي) بأسلوب واضح ومفيد."}]
|
| 78 |
-
|
| 79 |
-
for user_msg, bot_msg in context:
|
| 80 |
-
messages.append({"role": "user", "content": user_msg})
|
| 81 |
-
messages.append({"role": "assistant", "content": bot_msg})
|
| 82 |
-
|
| 83 |
-
messages.append({"role": "user", "content": user_text})
|
| 84 |
-
|
| 85 |
-
inputs = tokenizer.apply_chat_template(
|
| 86 |
-
messages,
|
| 87 |
-
tokenize=True,
|
| 88 |
-
add_generation_prompt=True,
|
| 89 |
-
return_tensors="pt"
|
| 90 |
-
).to(model.device)
|
| 91 |
-
|
| 92 |
-
outputs = model.generate(
|
| 93 |
-
inputs,
|
| 94 |
-
max_new_tokens=300,
|
| 95 |
-
temperature=0.7,
|
| 96 |
-
top_p=0.9,
|
| 97 |
-
do_sample=True,
|
| 98 |
-
pad_token_id=tokenizer.eos_token_id
|
| 99 |
-
)
|
| 100 |
-
|
| 101 |
-
reply = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 102 |
-
reply = reply.split("assistant")[-1].strip()
|
| 103 |
return reply
|
| 104 |
|
| 105 |
-
# ---------- المعالجة
|
| 106 |
-
def
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
if not message.strip():
|
| 110 |
-
return html_output, ""
|
| 111 |
-
|
| 112 |
-
user_line = f"<span style='color:#00ff88'>أنت:</span> {message}<br>"
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
| 119 |
else:
|
| 120 |
-
reply =
|
| 121 |
-
context.append((message,
|
| 122 |
else:
|
| 123 |
reply = smart_reply(message)
|
| 124 |
context.append((message, reply))
|
| 125 |
|
| 126 |
-
|
| 127 |
-
return new_html, ""
|
| 128 |
-
|
| 129 |
-
# ---------- واجهة Terminal (بدون أي أخطاء) ----------
|
| 130 |
-
with gr.Blocks(title="AGI Terminal") as demo:
|
| 131 |
-
gr.HTML("""
|
| 132 |
-
<div style="text-align:center; padding:20px; background:#000; color:#00ff00; font-family:monospace; border-bottom:3px solid #00ff00;">
|
| 133 |
-
<h1>AGI + حل رياضيات دقيق 100% + ذكاء متعدد اللغات</h1>
|
| 134 |
-
<p>اكتب أي شيء: مسألة رياضية أو سؤال عادي (عربي أو إنجليزي) واضغط Enter</p>
|
| 135 |
-
</div>
|
| 136 |
-
""")
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
<
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
• What is the sun?
|
| 145 |
-
• Who invented electricity?
|
| 146 |
-
</pre>
|
| 147 |
-
""")
|
| 148 |
-
|
| 149 |
-
msg = gr.Textbox(
|
| 150 |
-
placeholder="اكتب هنا واضغط Enter...",
|
| 151 |
-
lines=1,
|
| 152 |
-
label="",
|
| 153 |
-
container=False
|
| 154 |
-
)
|
| 155 |
|
| 156 |
-
|
| 157 |
|
| 158 |
-
demo.launch(
|
| 159 |
-
share=True,
|
| 160 |
-
server_name="0.0.0.0",
|
| 161 |
-
server_port=7860
|
| 162 |
-
)
|
|
|
|
| 1 |
# ------------------------------------------------------------
|
| 2 |
+
# AGI + Math Solver + Fast & Smart Reply (يعمل في ثواني على CPU)
|
|
|
|
| 3 |
# ------------------------------------------------------------
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
import torch
|
| 7 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 8 |
import sympy as sp
|
| 9 |
from sympy import symbols, Eq, solve, diff, integrate
|
|
|
|
| 10 |
import re
|
| 11 |
from collections import deque
|
| 12 |
|
| 13 |
+
# ---------- النموذج السريع والذكي (يعمل على CPU في ثواني) ----------
|
| 14 |
+
model_name = "microsoft/Phi-3-mini-4k-instruct" # ← الأفضل الآن
|
| 15 |
+
# model_name = "google/gemma-2-2b-it" # ← بديل ممتاز
|
| 16 |
|
| 17 |
+
print("جاري تحميل النموذج السريع...")
|
| 18 |
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 19 |
model = AutoModelForCausalLM.from_pretrained(
|
| 20 |
model_name,
|
| 21 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 22 |
device_map="auto",
|
| 23 |
trust_remote_code=True,
|
| 24 |
low_cpu_mem_usage=True
|
| 25 |
)
|
|
|
|
| 26 |
|
| 27 |
+
# استخدم pipeline لت11السرعة
|
| 28 |
+
generator = pipeline(
|
| 29 |
+
"text-generation",
|
| 30 |
+
model=model,
|
| 31 |
+
tokenizer=tokenizer,
|
| 32 |
+
max_new_tokens=250,
|
| 33 |
+
temperature=0.7,
|
| 34 |
+
top_p=0.9,
|
| 35 |
+
do_sample=True,
|
| 36 |
+
pad_token_id=tokenizer.eos_token_id
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# ---------- السياق ----------
|
| 40 |
context = deque(maxlen=6)
|
| 41 |
|
| 42 |
# ---------- كشف المسائل الرياضية ----------
|
| 43 |
+
MATH_PATTERN = re.compile(r"[0-9+\-*/().^=]|sin|cos|tan|sqrt|log|exp|اشتق|تكامل|حل|d/dx|integral|solve", re.I)
|
| 44 |
+
def is_math(text): return bool(MATH_PATTERN.search(text))
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
# ---------- حل رياضيات بدقة 100% ----------
|
| 47 |
+
def math_solver(q):
|
| 48 |
x = symbols('x')
|
| 49 |
try:
|
| 50 |
+
if "=" in q and any(k in q.lower() for k in ["حل", "solve", "x"]):
|
| 51 |
+
left, right = q.split("=", 1)
|
| 52 |
+
eq = Eq(sp.sympify(left), sp.sympify(right))
|
| 53 |
+
sol = solve(eq, x)
|
| 54 |
+
return f"**الحل:** x = {sol}"
|
| 55 |
+
if any(k in q for k in ["اشتق", "derivative"]):
|
| 56 |
+
expr = sp.sympify(re.sub(r".*اشتق\s+", "", q, flags=re.I))
|
| 57 |
+
return f"**المشتقة:** {diff(expr, x)}"
|
| 58 |
+
if any(k in q for k in ["تكامل", "integral"]):
|
| 59 |
+
expr = sp.sympify(re.sub(r".*تكامل\s+", "", q, flags=re.I))
|
| 60 |
+
return f"**التكامل:** ∫{expr} dx = {integrate(expr, x)} + C"
|
| 61 |
+
return f"**النتيجة:** {sp.sympify(q)}"
|
| 62 |
+
except:
|
| 63 |
+
return None
|
| 64 |
+
|
| 65 |
+
# ---------- رد ذكي سريع ----------
|
| 66 |
+
def smart_reply(text):
|
| 67 |
+
messages = [{"role": "system", "content": "You are a helpful assistant. Reply in the same language as the user (Arabic or English)."}]
|
| 68 |
+
for u, a in context:
|
| 69 |
+
messages += [{"role": "user", "content": u}, {"role": "assistant", "content": a}]
|
| 70 |
+
messages.append({"role": "user", "content": text})
|
| 71 |
+
|
| 72 |
+
output = generator(messages, max_new_tokens=200)[0]['generated_text']
|
| 73 |
+
reply = output.split("assistant")[-1].strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
return reply
|
| 75 |
|
| 76 |
+
# ---------- المعالجة ----------
|
| 77 |
+
def chat(message, html):
|
| 78 |
+
if not message.strip(): return html, ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
+
user = f"<span style='color:#00ff88'>أنت:</span> {message}<br>"
|
| 81 |
+
|
| 82 |
+
if is_math(message):
|
| 83 |
+
result = math_solver(message)
|
| 84 |
+
if result:
|
| 85 |
+
reply = f"<span style='color:#ffff00'>{result}</span>"
|
| 86 |
+
context.append((message, result))
|
| 87 |
else:
|
| 88 |
+
reply = smart_reply(message)
|
| 89 |
+
context.append((message, reply))
|
| 90 |
else:
|
| 91 |
reply = smart_reply(message)
|
| 92 |
context.append((message, reply))
|
| 93 |
|
| 94 |
+
return html + user + f"<span style='color:#00ffff'>AGI:</span> {reply}<br><br>", ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
+
# ---------- الواجهة ----------
|
| 97 |
+
with gr.Blocks(title="AGI Fast Terminal") as demo:
|
| 98 |
+
gr.HTML("<div style='text-align:center;background:#000;color:#0f0;padding:20px;font-size:22px;border-bottom:3px solid #0f0;'>AGI + حل رياضيات + رد سريع (3-8 ثواني)</div>")
|
| 99 |
+
|
| 100 |
+
term = gr.HTML(value="<pre style='background:#000;color:#0f0;padding:15px;height:500px;overflow-y:auto;border:2px solid #0f0;border-radius:10px;font-family:monospace;'>مرحباً! جرب:<br>• حل x²-5x+6=0<br>• What is AI?<br>• من هو نيوتن؟</pre>")
|
| 101 |
+
inp = gr.Textbox(placeholder="اكتب هنا واضغط Enter...", lines=1, label="")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
+
inp.submit(chat, [inp, term], [term, inp])
|
| 104 |
|
| 105 |
+
demo.launch(share=True, server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|
|
|
|
|