Upload 2 files
Browse files- app.py +28 -16
- requirements.txt +1 -1
app.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
# ββ Imports ββ (ORDER MATTERS: re must come after sympy wildcard)
|
| 2 |
import streamlit as st
|
| 3 |
import sympy as sp
|
| 4 |
-
import
|
|
|
|
| 5 |
from sympy import (
|
| 6 |
symbols, diff, integrate, limit, solve, simplify, expand, factor,
|
| 7 |
latex, sympify, oo, sin, cos, tan, exp, log, sqrt, pi, E,
|
|
@@ -307,16 +308,27 @@ def run_sympy(problem: str) -> dict:
|
|
| 307 |
return default
|
| 308 |
|
| 309 |
|
| 310 |
-
# βββ
|
| 311 |
def ask_claude(problem: str, sympy_info: dict, history: list) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
sympy_context = ""
|
| 313 |
if (sympy_info.get("result")
|
| 314 |
and sympy_info["result"] not in (None, "matrix_detected", "mod_detected")):
|
| 315 |
sympy_context = f"""
|
| 316 |
SYMPY VERIFIED RESULT (mathematically exact β your final answer MUST match this):
|
| 317 |
-
- Operation : {sympy_info.get(
|
| 318 |
-
- Result : {sympy_info.get(
|
| 319 |
-
- LaTeX : {sympy_info.get(
|
| 320 |
"""
|
| 321 |
|
| 322 |
system_prompt = f"""You are SAAD AI β an elite BSc Mathematics tutor.
|
|
@@ -334,19 +346,19 @@ TOPICS: Calculus | Linear Algebra | Number Theory | ODEs | Numerical Methods |
|
|
| 334 |
Differential Geometry | Hydro Mechanics | Theory of Numbers | Real Analysis II | General Math
|
| 335 |
"""
|
| 336 |
|
| 337 |
-
|
|
|
|
| 338 |
for msg in history[-8:]:
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
max_tokens=3000,
|
| 346 |
-
system=system_prompt,
|
| 347 |
-
messages=messages
|
| 348 |
)
|
| 349 |
-
|
|
|
|
|
|
|
| 350 |
|
| 351 |
|
| 352 |
# βββ Sidebar ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 1 |
# ββ Imports ββ (ORDER MATTERS: re must come after sympy wildcard)
|
| 2 |
import streamlit as st
|
| 3 |
import sympy as sp
|
| 4 |
+
import google.generativeai as genai
|
| 5 |
+
import os
|
| 6 |
from sympy import (
|
| 7 |
symbols, diff, integrate, limit, solve, simplify, expand, factor,
|
| 8 |
latex, sympify, oo, sin, cos, tan, exp, log, sqrt, pi, E,
|
|
|
|
| 308 |
return default
|
| 309 |
|
| 310 |
|
| 311 |
+
# βββ Gemini API Call (FREE) βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 312 |
def ask_claude(problem: str, sympy_info: dict, history: list) -> str:
|
| 313 |
+
# Configure Gemini with key from HF secrets
|
| 314 |
+
api_key = os.environ.get("GEMINI_API_KEY", "")
|
| 315 |
+
if not api_key:
|
| 316 |
+
return (
|
| 317 |
+
"β οΈ **GEMINI_API_KEY not found.**\n\n"
|
| 318 |
+
"Go to Hugging Face Space β Settings β Variables and Secrets β "
|
| 319 |
+
"Add secret: `GEMINI_API_KEY`\n\n"
|
| 320 |
+
"Get a free key at: https://aistudio.google.com"
|
| 321 |
+
)
|
| 322 |
+
genai.configure(api_key=api_key)
|
| 323 |
+
|
| 324 |
sympy_context = ""
|
| 325 |
if (sympy_info.get("result")
|
| 326 |
and sympy_info["result"] not in (None, "matrix_detected", "mod_detected")):
|
| 327 |
sympy_context = f"""
|
| 328 |
SYMPY VERIFIED RESULT (mathematically exact β your final answer MUST match this):
|
| 329 |
+
- Operation : {sympy_info.get("type", "N/A")}
|
| 330 |
+
- Result : {sympy_info.get("result", "N/A")}
|
| 331 |
+
- LaTeX : {sympy_info.get("latex", "N/A")}
|
| 332 |
"""
|
| 333 |
|
| 334 |
system_prompt = f"""You are SAAD AI β an elite BSc Mathematics tutor.
|
|
|
|
| 346 |
Differential Geometry | Hydro Mechanics | Theory of Numbers | Real Analysis II | General Math
|
| 347 |
"""
|
| 348 |
|
| 349 |
+
# Build conversation history for Gemini
|
| 350 |
+
gemini_history = []
|
| 351 |
for msg in history[-8:]:
|
| 352 |
+
role = "user" if msg["role"] == "user" else "model"
|
| 353 |
+
gemini_history.append({"role": role, "parts": [msg["content"]]})
|
| 354 |
+
|
| 355 |
+
model = genai.GenerativeModel(
|
| 356 |
+
model_name="gemini-1.5-flash", # free tier, fast, capable
|
| 357 |
+
system_instruction=system_prompt
|
|
|
|
|
|
|
|
|
|
| 358 |
)
|
| 359 |
+
chat = model.start_chat(history=gemini_history)
|
| 360 |
+
response = chat.send_message(problem)
|
| 361 |
+
return response.text
|
| 362 |
|
| 363 |
|
| 364 |
# βββ Sidebar ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
requirements.txt
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
streamlit>=1.32.0
|
| 2 |
-
|
| 3 |
sympy>=1.12
|
|
|
|
| 1 |
streamlit>=1.32.0
|
| 2 |
+
google-generativeai>=0.7.0
|
| 3 |
sympy>=1.12
|