Delete app.py
Browse files
app.py
DELETED
|
@@ -1,87 +0,0 @@
|
|
| 1 |
-
import numpy as np
|
| 2 |
-
import plotly.graph_objects as go
|
| 3 |
-
import gradio as gr
|
| 4 |
-
from transformers import pipeline
|
| 5 |
-
|
| 6 |
-
# Load LLM
|
| 7 |
-
commentator = pipeline("text-generation", model="tiiuae/falcon-7b-instruct", max_new_tokens=100)
|
| 8 |
-
|
| 9 |
-
MAX_DEGREE = 10 # Threshold for simplification
|
| 10 |
-
|
| 11 |
-
def format_polynomial(coeffs):
|
| 12 |
-
terms = []
|
| 13 |
-
deg = len(coeffs) - 1
|
| 14 |
-
for i, c in enumerate(coeffs):
|
| 15 |
-
power = deg - i
|
| 16 |
-
if np.isclose(c, 0):
|
| 17 |
-
continue
|
| 18 |
-
if power == 0:
|
| 19 |
-
terms.append(f"{c:+.2g}")
|
| 20 |
-
elif power == 1:
|
| 21 |
-
terms.append(f"{c:+.2g}x")
|
| 22 |
-
else:
|
| 23 |
-
terms.append(f"{c:+.2g}x^{power}")
|
| 24 |
-
return " ".join(terms).replace("+-", "- ")
|
| 25 |
-
|
| 26 |
-
def approximate_polynomial(coeffs):
|
| 27 |
-
if len(coeffs) > MAX_DEGREE + 1:
|
| 28 |
-
coeffs = coeffs[:MAX_DEGREE + 1]
|
| 29 |
-
return coeffs
|
| 30 |
-
|
| 31 |
-
def generate_plot(coeffs):
|
| 32 |
-
x = np.linspace(-10, 10, 500)
|
| 33 |
-
y = np.polyval(coeffs, x)
|
| 34 |
-
|
| 35 |
-
fig = go.Figure()
|
| 36 |
-
fig.add_trace(go.Scatter(x=x, y=y, mode='lines', name='Polynomial'))
|
| 37 |
-
fig.update_layout(title="Interactive Polynomial Plot",
|
| 38 |
-
xaxis_title="x",
|
| 39 |
-
yaxis_title="f(x)",
|
| 40 |
-
hovermode='closest')
|
| 41 |
-
return fig
|
| 42 |
-
|
| 43 |
-
def on_click(trace, points, state):
|
| 44 |
-
return f"Clicked point: x = {points.xs[0]}, y = {points.ys[0]}"
|
| 45 |
-
|
| 46 |
-
def process_polynomial(degree, coeff_input):
|
| 47 |
-
try:
|
| 48 |
-
coeffs = list(map(float, coeff_input.strip().split()))
|
| 49 |
-
if len(coeffs) != degree + 1:
|
| 50 |
-
return f"β οΈ Please enter exactly {degree + 1} coefficients.", None, None
|
| 51 |
-
|
| 52 |
-
if degree > MAX_DEGREE:
|
| 53 |
-
coeffs = approximate_polynomial(coeffs)
|
| 54 |
-
|
| 55 |
-
poly_str = format_polynomial(coeffs)
|
| 56 |
-
roots = np.roots(coeffs)
|
| 57 |
-
real_roots = [round(r.real, 3) for r in roots if np.isclose(r.imag, 0)]
|
| 58 |
-
root_text = ", ".join(map(str, real_roots)) if real_roots else "No real roots."
|
| 59 |
-
|
| 60 |
-
plot = generate_plot(coeffs)
|
| 61 |
-
|
| 62 |
-
description = commentator(f"Say something interesting about this polynomial: {poly_str}")[0]['generated_text']
|
| 63 |
-
|
| 64 |
-
return f"π Polynomial: {poly_str}\nβ
Real roots: {root_text}", plot, description
|
| 65 |
-
|
| 66 |
-
except Exception as e:
|
| 67 |
-
return f"Error: {str(e)}", None, None
|
| 68 |
-
|
| 69 |
-
# Gradio UI
|
| 70 |
-
with gr.Blocks() as demo:
|
| 71 |
-
gr.Markdown("## π Welcome to MathWizz (Hugging Face Edition) π")
|
| 72 |
-
|
| 73 |
-
with gr.Row():
|
| 74 |
-
degree_input = gr.Number(label="Enter Polynomial Degree", value=2)
|
| 75 |
-
coeff_input = gr.Textbox(label="Enter Coefficients (space-separated)", placeholder="e.g. 1 -3 2")
|
| 76 |
-
|
| 77 |
-
submit_btn = gr.Button("Analyze Polynomial")
|
| 78 |
-
|
| 79 |
-
result_output = gr.Textbox(label="Result")
|
| 80 |
-
graph_output = gr.Plot(label="Interactive Plot")
|
| 81 |
-
llm_output = gr.Textbox(label="LLM Says...")
|
| 82 |
-
|
| 83 |
-
submit_btn.click(process_polynomial,
|
| 84 |
-
inputs=[degree_input, coeff_input],
|
| 85 |
-
outputs=[result_output, graph_output, llm_output])
|
| 86 |
-
|
| 87 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|