Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,50 @@
|
|
| 1 |
-
import
|
|
|
|
| 2 |
import sympy as sp
|
| 3 |
-
import gradio as gr
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
# Solve the equation using sympy
|
| 12 |
-
if "=" in equation:
|
| 13 |
-
lhs, rhs = equation.split("=")
|
| 14 |
-
equation = sp.Eq(sp.sympify(lhs), sp.sympify(rhs))
|
| 15 |
-
solution = sp.solve(equation)
|
| 16 |
-
else:
|
| 17 |
-
# Just evaluate the expression
|
| 18 |
-
solution = sp.sympify(equation)
|
| 19 |
-
|
| 20 |
-
return f"The solution is: {solution}"
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
|
| 25 |
-
# Function to
|
| 26 |
def is_math_question(question):
|
| 27 |
-
|
|
|
|
| 28 |
return True
|
| 29 |
-
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
|
| 39 |
-
inputs=gr.Textbox(label="Enter your mathematical question"),
|
| 40 |
-
outputs="text",
|
| 41 |
-
title="Math Chatbot (Open Source)",
|
| 42 |
-
description="Ask any mathematical question and get an answer. Non-mathematical questions will be restricted.")
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
import sympy as sp
|
|
|
|
| 4 |
|
| 5 |
+
# Cache the model so it's loaded only once
|
| 6 |
+
@st.cache_resource
|
| 7 |
+
def load_model():
|
| 8 |
+
# Load an open-source Hugging Face model for natural language processing
|
| 9 |
+
return pipeline('text-classification', model='mrm8488/t5-base-finetuned-summarize-news')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# Initialize the model
|
| 12 |
+
nlp_model = load_model()
|
| 13 |
|
| 14 |
+
# Function to check if the question is mathematical
|
| 15 |
def is_math_question(question):
|
| 16 |
+
try:
|
| 17 |
+
parsed_expr = sp.sympify(question)
|
| 18 |
return True
|
| 19 |
+
except (sp.SympifyError, SyntaxError):
|
| 20 |
+
return False
|
| 21 |
|
| 22 |
+
# Function to solve mathematical questions using SymPy
|
| 23 |
+
def solve_math_question(question):
|
| 24 |
+
try:
|
| 25 |
+
# Parse and solve the mathematical expression
|
| 26 |
+
solution = sp.solve(sp.sympify(question))
|
| 27 |
+
return f"The solution is: {solution}"
|
| 28 |
+
except Exception as e:
|
| 29 |
+
return f"Error solving the equation: {e}"
|
| 30 |
+
|
| 31 |
+
# Streamlit UI
|
| 32 |
+
st.title("Math Chatbot (Open Source)")
|
| 33 |
+
st.write("Ask any mathematical question and get an answer. Non-mathematical questions will be restricted.")
|
| 34 |
|
| 35 |
+
# User input
|
| 36 |
+
question = st.text_input("Enter your mathematical question:")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
# Processing the input
|
| 39 |
+
if st.button("Submit"):
|
| 40 |
+
if is_math_question(question):
|
| 41 |
+
# Solve the mathematical question
|
| 42 |
+
answer = solve_math_question(question)
|
| 43 |
+
st.write(f"Answer: {answer}")
|
| 44 |
+
else:
|
| 45 |
+
# Filter non-mathematical questions using NLP model
|
| 46 |
+
nlp_result = nlp_model(question)[0]
|
| 47 |
+
if nlp_result['label'] == 'Math':
|
| 48 |
+
st.write("Answer: Processing your math question...")
|
| 49 |
+
else:
|
| 50 |
+
st.write("This chatbot only answers questions related to mathematics. Please ask a mathematical question.")
|