-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
69 lines (59 loc) · 2.93 KB
/
app.py
File metadata and controls
69 lines (59 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import os
import matplotlib.pyplot as plt
import numpy as np
import sympy as sp
import streamlit as st
from dotenv import load_dotenv
load_dotenv()
import google.generativeai as genai
genai.configure(api_key=os.getenv("API_KEY"))
from sympy import sin, cos, tan, exp, log, sqrt
def getContent(inputText, prompt):
model = genai.GenerativeModel("gemini-1.5-flash")
res = model.generate_content(prompt + inputText)
return res.text
def parse_equation(equation_str):
try:
equation = sp.sympify(equation_str)
return equation
except sp.SympifyError:
return None
def generate_graph_data(equation, x_min, x_max, num_points=500):
x_vals = np.linspace(x_min, x_max, num_points)
try:
y_vals = np.array([float(equation.subs('x', val)) for val in x_vals])
except ZeroDivisionError:
raise ValueError("Math error occurred while evaluating the equation.")
return x_vals, y_vals
prompt = """Prompt for Generating SymPy Mathematical Expressions:
You are tasked with converting a mathematical equation involving one variable, x, into a valid SymPy mathematical expression. The equation can include standard mathematical functions such as sin(x), cos(x), tan(x), exp(x), log(x), sqrt(x), and other mathematical operations.
Please ensure that your output follows the SymPy syntax correctly and supports standard functions like sin, cos, exp, log, sqrt, etc. Make sure that the expression uses x as the only variable. Return the expression as a valid SymPy code, and do not include any extra text.
If the sympy syntax isn't possible just return a -1 with no extra text.
"""
st.title("Equation Visualizer")
inputText = st.text_input("Enter the expression: ")
x_min = st.number_input("Enter the minimum value for x:", -200, 0, -100)
x_max = st.number_input("Enter the maximum value for x:", 0, 200, 100)
if st.button("Graph the equation"):
# Show loading spinner while the equation is being processed
with st.spinner('Processing the equation...'):
# Preprocessing: Correcting syntax issues like '^' to '**'
inputText = inputText.replace("^", "**").replace(" ", "")
input2 = getContent(inputText, prompt)
if input2 == "-1":
st.error("Invalid mathematical expression! Please check your syntax or use only supported functions.")
else:
eqn = parse_equation(input2)
if eqn is None:
st.error("There was an error processing the equation. Please ensure it is a valid mathematical expression.")
else:
try:
# Generate graph data
x_vals, y_vals = generate_graph_data(eqn, x_min, x_max)
# Plot the graph
fig, ax = plt.subplots()
ax.plot(x_vals, y_vals)
ax.grid(True)
st.pyplot(fig)
except ValueError as e:
st.error(str(e))