Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from unsloth import FastLanguageModel
|
| 3 |
+
from transformers import AutoTokenizer
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
@st.cache_resource
|
| 7 |
+
def load_model_and_tokenizer(model_name, hf_token):
|
| 8 |
+
# Load the model and tokenizer
|
| 9 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
| 10 |
+
model_name=model_name,
|
| 11 |
+
max_seq_length=2048,
|
| 12 |
+
dtype=None,
|
| 13 |
+
load_in_4bit=True,
|
| 14 |
+
token=hf_token,
|
| 15 |
+
)
|
| 16 |
+
FastLanguageModel.for_inference(model) # Enable optimized inference
|
| 17 |
+
return model, tokenizer
|
| 18 |
+
|
| 19 |
+
def generate_solution(problem, model, tokenizer):
|
| 20 |
+
# Prepare the prompt using the same format as training
|
| 21 |
+
prompt_template = """Below is an instruction that describes a task, paired with an input that provides further context.
|
| 22 |
+
Write a response that appropriately completes the request.
|
| 23 |
+
Before answering, think carefully about the question and create a step-by-step chain of thoughts to ensure a logical and accurate response.
|
| 24 |
+
|
| 25 |
+
### Instruction:
|
| 26 |
+
You are a math expert. Please solve the following math problem.
|
| 27 |
+
|
| 28 |
+
### Problem:
|
| 29 |
+
{}
|
| 30 |
+
|
| 31 |
+
### Solution:
|
| 32 |
+
<think>
|
| 33 |
+
{{}}
|
| 34 |
+
</think>
|
| 35 |
+
{{}}"""
|
| 36 |
+
|
| 37 |
+
prompt = prompt_template.format(problem)
|
| 38 |
+
|
| 39 |
+
# Tokenize and prepare input
|
| 40 |
+
inputs = tokenizer(
|
| 41 |
+
[prompt],
|
| 42 |
+
return_tensors="pt",
|
| 43 |
+
padding=True,
|
| 44 |
+
).to("cuda")
|
| 45 |
+
|
| 46 |
+
# Generate solution
|
| 47 |
+
outputs = model.generate(
|
| 48 |
+
input_ids=inputs.input_ids,
|
| 49 |
+
attention_mask=inputs.attention_mask,
|
| 50 |
+
max_new_tokens=2000,
|
| 51 |
+
temperature=0.7,
|
| 52 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 53 |
+
use_cache=True,
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
# Decode and format output
|
| 57 |
+
full_response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 58 |
+
|
| 59 |
+
# Extract the generated solution part
|
| 60 |
+
try:
|
| 61 |
+
solution = full_response.split("### Solution:")[1].strip()
|
| 62 |
+
except IndexError:
|
| 63 |
+
solution = full_response # Fallback in case formatting fails
|
| 64 |
+
|
| 65 |
+
return solution
|
| 66 |
+
|
| 67 |
+
# Streamlit app
|
| 68 |
+
st.title("Math Problem Solver")
|
| 69 |
+
hf_token = st.text_input("Enter your Hugging Face token:")
|
| 70 |
+
model_name = "shukdevdatta123/DeepSeek-R1-Math-Solutions"
|
| 71 |
+
|
| 72 |
+
if hf_token:
|
| 73 |
+
# Load model and tokenizer
|
| 74 |
+
model, tokenizer = load_model_and_tokenizer(model_name, hf_token)
|
| 75 |
+
|
| 76 |
+
# Input for custom problem
|
| 77 |
+
custom_problem = st.text_input("Enter a math problem:")
|
| 78 |
+
|
| 79 |
+
if st.button("Generate Solution"):
|
| 80 |
+
if custom_problem:
|
| 81 |
+
solution = generate_solution(custom_problem, model, tokenizer)
|
| 82 |
+
st.write("### Generated Solution:")
|
| 83 |
+
st.write(solution)
|
| 84 |
+
else:
|
| 85 |
+
st.error("Please enter a math problem.")
|
| 86 |
+
else:
|
| 87 |
+
st.warning("Please enter your Hugging Face token to load the model.")
|