Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
|
| 4 |
+
# Load Hugging Face Model
|
| 5 |
+
@st.cache_resource
|
| 6 |
+
def load_model():
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained("xsanskarx/calculator-smollm2_v2")
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained("xsanskarx/calculator-smollm2_v2")
|
| 9 |
+
return tokenizer, model
|
| 10 |
+
|
| 11 |
+
tokenizer, model = load_model()
|
| 12 |
+
|
| 13 |
+
def calculate_expression(expression: str) -> str:
|
| 14 |
+
# Encode the user input and generate the result
|
| 15 |
+
input_ids = tokenizer.encode(expression, return_tensors="pt")
|
| 16 |
+
output_ids = model.generate(input_ids, max_new_tokens=50)
|
| 17 |
+
result = tokenizer.decode(output_ids[:, input_ids.shape[-1]:][0], skip_special_tokens=True)
|
| 18 |
+
return result
|
| 19 |
+
|
| 20 |
+
# Streamlit Interface
|
| 21 |
+
st.title("AI-Powered Calculator")
|
| 22 |
+
st.write("Enter a mathematical expression, and let the model solve it.")
|
| 23 |
+
|
| 24 |
+
expression = st.text_input("Enter Expression (e.g., 5 + 3 * (2 - 1)):")
|
| 25 |
+
|
| 26 |
+
if st.button("Calculate"):
|
| 27 |
+
if expression.strip():
|
| 28 |
+
try:
|
| 29 |
+
result = calculate_expression(expression)
|
| 30 |
+
st.success(f"Result: {result}")
|
| 31 |
+
except Exception as e:
|
| 32 |
+
st.error(f"Error: {str(e)}")
|
| 33 |
+
else:
|
| 34 |
+
st.warning("Please enter a valid expression.")
|