Pbhat commited on
Commit
8d9cf36
·
verified ·
1 Parent(s): fef407d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -33
app.py CHANGED
@@ -1,18 +1,18 @@
1
  # streamlit_app.py
2
- # app.py
3
  import streamlit as st
4
  import re
5
  from sympy import symbols, integrate, exp, pi
6
  from transformers import AutoTokenizer, AutoModelForCausalLM
7
  import torch
8
 
9
- st.set_page_config(page_title="AI Physics Solver", page_icon="🧠")
10
 
11
  x, t = symbols("x t")
12
 
13
  def extract_integral(problem_text):
14
  match = re.search(r'(\d+)\*?[tx]\^(\d+)', problem_text)
15
- limits = re.findall(r'[tx]\s*=\s*([\d\.\\\w]+)', problem_text)
16
  exp_match = re.search(r'(\d+)e\^([\-\+]?\d+\.?\d*)[tx]', problem_text)
17
 
18
  if 'radioactive' in problem_text or 'half-life' in problem_text:
@@ -35,61 +35,61 @@ def extract_integral(problem_text):
35
  return "Could not parse the integral format."
36
 
37
  @st.cache_resource
38
- def load_deepseek():
39
- model_name = "deepseek-ai/deepseek-math-7b-base"
 
 
 
 
 
 
 
40
  tokenizer = AutoTokenizer.from_pretrained(model_name)
41
 
42
- if torch.cuda.is_available():
43
- model = AutoModelForCausalLM.from_pretrained(
44
- model_name,
45
- torch_dtype=torch.float16,
46
- device_map="auto"
47
- )
48
- else:
49
- model = AutoModelForCausalLM.from_pretrained(model_name)
50
 
51
  return tokenizer, model
52
 
53
- def run_deepseek(user_question):
54
- tokenizer, model = load_deepseek()
55
- solution_steps = """
56
- ### Solution:
57
- 1. Understand the problem and extract known quantities.
58
- 2. Apply relevant physical laws or mathematical formulas.
59
- 3. Solve algebraically or numerically as required.
60
- 4. Clearly present the final answer.
61
 
62
  ### Final Answer Format:
63
  Final Answer: [VARIABLE] = [ANSWER] [UNIT]
64
- """
65
- prompt = f"Q: Solve the following physics problem using rigorous mathematical reasoning. Do not skip any steps.\n\nProblem: {user_question}\n\n{solution_steps}\nA:"
66
- inputs = tokenizer(prompt, return_tensors="pt")
67
 
68
- # Move inputs to GPU if available
69
- if torch.cuda.is_available():
70
- inputs = inputs.to("cuda")
71
 
72
  with torch.no_grad():
73
  outputs = model.generate(
74
  **inputs,
75
  max_new_tokens=500,
76
- temperature=0.1,
77
  repetition_penalty=1.0,
78
  eos_token_id=tokenizer.eos_token_id,
79
  pad_token_id=tokenizer.eos_token_id
80
  )
 
81
  return tokenizer.decode(outputs[0], skip_special_tokens=True).split("A:")[-1].strip()
82
 
83
- # ---------------- UI Layout ----------------
84
- st.title("🧠 AI Science Solver")
85
 
86
- task_type = st.selectbox("Choose Task Type", ["LLM Reasoning (DeepSeek)", "Symbolic Integration"])
87
  user_question = st.text_area("Enter your physics or math question below:")
88
 
89
  if st.button("Solve"):
90
  with st.spinner("Solving..."):
91
- if task_type == "LLM Reasoning (DeepSeek)":
92
- result = run_deepseek(user_question)
93
  else:
94
  result = extract_integral(user_question)
95
 
 
1
  # streamlit_app.py
2
+ # streamlit_app.py
3
  import streamlit as st
4
  import re
5
  from sympy import symbols, integrate, exp, pi
6
  from transformers import AutoTokenizer, AutoModelForCausalLM
7
  import torch
8
 
9
+ st.set_page_config(page_title="AI Problem Solver By Mathematically Modelling", page_icon="🧠")
10
 
11
  x, t = symbols("x t")
12
 
13
  def extract_integral(problem_text):
14
  match = re.search(r'(\d+)\*?[tx]\^(\d+)', problem_text)
15
+ limits = re.findall(r'[tx]\s*=\s*([\d\.\w]+)', problem_text)
16
  exp_match = re.search(r'(\d+)e\^([\-\+]?\d+\.?\d*)[tx]', problem_text)
17
 
18
  if 'radioactive' in problem_text or 'half-life' in problem_text:
 
35
  return "Could not parse the integral format."
36
 
37
  @st.cache_resource
38
+ def load_model():
39
+ # Change this if you want to fallback to a smaller model on CPU
40
+ use_light_model = not torch.cuda.is_available()
41
+
42
+ model_name = (
43
+ "deepseek-ai/deepseek-math-7b-base" if not use_light_model
44
+ else "tiiuae/falcon-7b-instruct"
45
+ )
46
+
47
  tokenizer = AutoTokenizer.from_pretrained(model_name)
48
 
49
+ model = AutoModelForCausalLM.from_pretrained(
50
+ model_name,
51
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
52
+ device_map="auto" if torch.cuda.is_available() else None
53
+ )
 
 
 
54
 
55
  return tokenizer, model
56
 
57
+ def run_llm_reasoning(user_question):
58
+ tokenizer, model = load_model()
59
+
60
+ prompt = f"""
61
+ Q: Solve the following physics problem using rigorous mathematical reasoning. Do not skip any steps.
62
+
63
+ Problem: {user_question}
 
64
 
65
  ### Final Answer Format:
66
  Final Answer: [VARIABLE] = [ANSWER] [UNIT]
67
+ A:"""
 
 
68
 
69
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
 
 
70
 
71
  with torch.no_grad():
72
  outputs = model.generate(
73
  **inputs,
74
  max_new_tokens=500,
75
+ temperature=0.2,
76
  repetition_penalty=1.0,
77
  eos_token_id=tokenizer.eos_token_id,
78
  pad_token_id=tokenizer.eos_token_id
79
  )
80
+
81
  return tokenizer.decode(outputs[0], skip_special_tokens=True).split("A:")[-1].strip()
82
 
83
+ # ---------------- UI ----------------
84
+ st.title("🧠 AI Physics & Math Solver")
85
 
86
+ task_type = st.selectbox("Choose Task Type", ["LLM Reasoning (DeepSeek/Fallback)", "Symbolic Integration"])
87
  user_question = st.text_area("Enter your physics or math question below:")
88
 
89
  if st.button("Solve"):
90
  with st.spinner("Solving..."):
91
+ if task_type == "LLM Reasoning (DeepSeek/Fallback)":
92
+ result = run_llm_reasoning(user_question)
93
  else:
94
  result = extract_integral(user_question)
95