Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
# ------------------------------------------------------------
|
| 2 |
# AGI + Safe Math + Awareness + 100% Accurate Math Solver (Best-Practice)
|
| 3 |
-
# Hugging Face Spaces Version – Fixed
|
| 4 |
# ------------------------------------------------------------
|
| 5 |
|
| 6 |
import gradio as gr
|
|
@@ -85,6 +85,7 @@ def advanced_math_solver(question: str):
|
|
| 85 |
# --- 1. جبر: حل المعادلات ---
|
| 86 |
if any(op in question for op in ["=", "حل", "solve"]):
|
| 87 |
if "x" in question or "y" in question:
|
|
|
|
| 88 |
eq_part = re.sub(r"[^=0-9xXyY\+\-\*/\^\(\)]", "", question)
|
| 89 |
if "=" in eq_part:
|
| 90 |
left, right = eq_part.split("=", 1)
|
|
@@ -188,17 +189,15 @@ def gpt2_reply(user_text: str) -> str:
|
|
| 188 |
reply = reply.split("AGI:")[-1].strip()
|
| 189 |
return reply
|
| 190 |
|
| 191 |
-
# ----------
|
| 192 |
-
def process_input(user_input,
|
| 193 |
global context
|
| 194 |
|
| 195 |
-
if
|
| 196 |
-
|
|
|
|
|
|
|
| 197 |
|
| 198 |
-
# Add user input to history
|
| 199 |
-
history.append([f"<span style='color:#00ff00'>أنت:</span> {user_input}", None])
|
| 200 |
-
|
| 201 |
-
# Your original logic (no changes)
|
| 202 |
logging.info(f"USER: {user_input}")
|
| 203 |
|
| 204 |
if contains_bad_word(user_input):
|
|
@@ -223,12 +222,10 @@ def process_input(user_input, history):
|
|
| 223 |
logging.info(f"AGI REPLY: {reply}")
|
| 224 |
context.append((user_input, reply))
|
| 225 |
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
return history, ""
|
| 230 |
|
| 231 |
-
# ---------- Gradio UI (Fixed: No
|
| 232 |
print("AGI + 100% Accurate Math Solver (Best-Practice) جاهز!")
|
| 233 |
print("أمثلة:")
|
| 234 |
print(" حل x^3 - 6x^2 + 11x - 6 = 0")
|
|
@@ -239,7 +236,7 @@ print(" 5 + 3 * 2")
|
|
| 239 |
print("اكتب 'exit' للخروج.\n")
|
| 240 |
|
| 241 |
with gr.Blocks(title="AGI Math Solver Terminal") as demo:
|
| 242 |
-
# Header
|
| 243 |
gr.HTML("""
|
| 244 |
<div style="text-align:center; padding:20px; background-color:#000; color:#00ff00; border-radius:15px; margin-bottom:20px; font-family:monospace;">
|
| 245 |
<h1>AGI + 100% Accurate Math Solver – Terminal Mode</h1>
|
|
@@ -247,13 +244,19 @@ with gr.Blocks(title="AGI Math Solver Terminal") as demo:
|
|
| 247 |
</div>
|
| 248 |
""")
|
| 249 |
|
| 250 |
-
#
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
)
|
| 258 |
|
| 259 |
msg = gr.Textbox(
|
|
@@ -262,35 +265,37 @@ with gr.Blocks(title="AGI Math Solver Terminal") as demo:
|
|
| 262 |
lines=1
|
| 263 |
)
|
| 264 |
|
| 265 |
-
# Process input and update output
|
| 266 |
-
def
|
| 267 |
if message.lower() in ["exit", "quit", "خروج"]:
|
| 268 |
reply = "تم الإغلاق. شكرًا!"
|
| 269 |
-
|
| 270 |
-
return output_text, ""
|
| 271 |
else:
|
| 272 |
# Your original logic
|
| 273 |
if contains_bad_word(message):
|
| 274 |
-
reply = "AGI: آسف، لا أقبل اللغة السيئة.
|
| 275 |
elif is_math_expression(message):
|
| 276 |
math_result = advanced_math_solver(message)
|
| 277 |
if math_result["success"]:
|
| 278 |
-
reply = f"AGI: الإجابة: {math_result['answer']}
|
| 279 |
if math_result["steps"]:
|
| 280 |
-
reply += "الخطوات:
|
| 281 |
for step in math_result["steps"]:
|
| 282 |
-
reply += f" • {step}
|
| 283 |
context.append((message, str(math_result["answer"])))
|
| 284 |
else:
|
| 285 |
-
reply = "AGI: لم أتمكن من حلها رمزيًا، سأستخدم الذكاء العام...
|
| 286 |
else:
|
| 287 |
reply = gpt2_reply(message)
|
| 288 |
context.append((message, reply))
|
| 289 |
|
| 290 |
-
|
| 291 |
-
|
|
|
|
|
|
|
| 292 |
|
| 293 |
-
msg.submit(
|
|
|
|
| 294 |
|
| 295 |
demo.launch(
|
| 296 |
share=True,
|
|
|
|
| 1 |
# ------------------------------------------------------------
|
| 2 |
# AGI + Safe Math + Awareness + 100% Accurate Math Solver (Best-Practice)
|
| 3 |
+
# Hugging Face Spaces Version – Fixed readonly Error
|
| 4 |
# ------------------------------------------------------------
|
| 5 |
|
| 6 |
import gradio as gr
|
|
|
|
| 85 |
# --- 1. جبر: حل المعادلات ---
|
| 86 |
if any(op in question for op in ["=", "حل", "solve"]):
|
| 87 |
if "x" in question or "y" in question:
|
| 88 |
+
# استخراج المعادلة
|
| 89 |
eq_part = re.sub(r"[^=0-9xXyY\+\-\*/\^\(\)]", "", question)
|
| 90 |
if "=" in eq_part:
|
| 91 |
left, right = eq_part.split("=", 1)
|
|
|
|
| 189 |
reply = reply.split("AGI:")[-1].strip()
|
| 190 |
return reply
|
| 191 |
|
| 192 |
+
# ---------- Terminal-style Interface ----------
|
| 193 |
+
def process_input(user_input, output_text):
|
| 194 |
global context
|
| 195 |
|
| 196 |
+
if user_input.lower() in ["exit", "quit", "خروج"]:
|
| 197 |
+
reply = "تم الإغلاق. شكرًا!"
|
| 198 |
+
output_text += f"\nأنت: {user_input}\nAGI: {reply}\n"
|
| 199 |
+
return output_text, ""
|
| 200 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
logging.info(f"USER: {user_input}")
|
| 202 |
|
| 203 |
if contains_bad_word(user_input):
|
|
|
|
| 222 |
logging.info(f"AGI REPLY: {reply}")
|
| 223 |
context.append((user_input, reply))
|
| 224 |
|
| 225 |
+
output_text += f"\nأنت: {user_input}\nAGI: {reply}"
|
| 226 |
+
return output_text, ""
|
|
|
|
|
|
|
| 227 |
|
| 228 |
+
# ---------- Gradio UI (Fixed: No readonly, Use HTML for Output) ----------
|
| 229 |
print("AGI + 100% Accurate Math Solver (Best-Practice) جاهز!")
|
| 230 |
print("أمثلة:")
|
| 231 |
print(" حل x^3 - 6x^2 + 11x - 6 = 0")
|
|
|
|
| 236 |
print("اكتب 'exit' للخروج.\n")
|
| 237 |
|
| 238 |
with gr.Blocks(title="AGI Math Solver Terminal") as demo:
|
| 239 |
+
# Header
|
| 240 |
gr.HTML("""
|
| 241 |
<div style="text-align:center; padding:20px; background-color:#000; color:#00ff00; border-radius:15px; margin-bottom:20px; font-family:monospace;">
|
| 242 |
<h1>AGI + 100% Accurate Math Solver – Terminal Mode</h1>
|
|
|
|
| 244 |
</div>
|
| 245 |
""")
|
| 246 |
|
| 247 |
+
# Output as HTML (no readonly error)
|
| 248 |
+
output_html = gr.HTML(
|
| 249 |
+
value="""
|
| 250 |
+
<pre style="background:#000; color:#00ff00; padding:10px; height:400px; overflow-y:scroll; font-family:monospace;">
|
| 251 |
+
AGI: AGI + 100% Accurate Math Solver جاهز!<br>
|
| 252 |
+
أمثلة:<br>
|
| 253 |
+
• حل x^3 - 6x^2 + 11x - 6 = 0<br>
|
| 254 |
+
• اشتق x^2 + sin(x)<br>
|
| 255 |
+
• تكامل x^2 + 2x<br>
|
| 256 |
+
• 5 + 3 * 2<br>
|
| 257 |
+
اكتب 'exit' للخروج.
|
| 258 |
+
</pre>
|
| 259 |
+
"""
|
| 260 |
)
|
| 261 |
|
| 262 |
msg = gr.Textbox(
|
|
|
|
| 265 |
lines=1
|
| 266 |
)
|
| 267 |
|
| 268 |
+
# Process input and update HTML output
|
| 269 |
+
def update_output(message, current_html):
|
| 270 |
if message.lower() in ["exit", "quit", "خروج"]:
|
| 271 |
reply = "تم الإغلاق. شكرًا!"
|
| 272 |
+
new_html = current_html + f"<br><span style='color:#00ff00'>أنت:</span> {message}<br><span style='color:#ffff00'>AGI:</span> {reply}"
|
|
|
|
| 273 |
else:
|
| 274 |
# Your original logic
|
| 275 |
if contains_bad_word(message):
|
| 276 |
+
reply = "AGI: آسف، لا أقبل اللغة السيئة."
|
| 277 |
elif is_math_expression(message):
|
| 278 |
math_result = advanced_math_solver(message)
|
| 279 |
if math_result["success"]:
|
| 280 |
+
reply = f"AGI: الإجابة: {math_result['answer']}<br>"
|
| 281 |
if math_result["steps"]:
|
| 282 |
+
reply += "الخطوات:<br>"
|
| 283 |
for step in math_result["steps"]:
|
| 284 |
+
reply += f" • {step}<br>"
|
| 285 |
context.append((message, str(math_result["answer"])))
|
| 286 |
else:
|
| 287 |
+
reply = "AGI: لم أتمكن من حلها رمزيًا، سأستخدم الذكاء العام..."
|
| 288 |
else:
|
| 289 |
reply = gpt2_reply(message)
|
| 290 |
context.append((message, reply))
|
| 291 |
|
| 292 |
+
new_html = current_html + f"<br><span style='color:#00ff00'>أنت:</span> {message}<br><span style='color:#ffff00'>AGI:</span> {reply}"
|
| 293 |
+
|
| 294 |
+
# Return updated HTML
|
| 295 |
+
return gr.update(value=new_html)
|
| 296 |
|
| 297 |
+
msg.submit(update_output, [msg, output_html], output_html)
|
| 298 |
+
msg.submit(lambda x: "", outputs=[msg]) # Clear input after submit
|
| 299 |
|
| 300 |
demo.launch(
|
| 301 |
share=True,
|