saad-sust commited on
Commit
4a3d4d9
Β·
verified Β·
1 Parent(s): b57bf2f

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +28 -16
  2. 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 anthropic
 
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
- # ─── Claude API Call ────────────────────────────────────────────────────────────
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('type', 'N/A')}
318
- - Result : {sympy_info.get('result', 'N/A')}
319
- - LaTeX : {sympy_info.get('latex', 'N/A')}
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
- messages = []
 
338
  for msg in history[-8:]:
339
- messages.append({"role": msg["role"], "content": msg["content"]})
340
- messages.append({"role": "user", "content": problem})
341
-
342
- client = anthropic.Anthropic()
343
- response = client.messages.create(
344
- model="claude-sonnet-4-5",
345
- max_tokens=3000,
346
- system=system_prompt,
347
- messages=messages
348
  )
349
- return response.content[0].text
 
 
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
- anthropic>=0.25.0
3
  sympy>=1.12
 
1
  streamlit>=1.32.0
2
+ google-generativeai>=0.7.0
3
  sympy>=1.12