Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,99 @@
|
|
| 1 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import tensorflow as tf
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import re
|
| 5 |
|
| 6 |
BOT_NAME = "Phu Quy Ho"
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
model = tf.keras.models.load_model("percyAI_model.h5")
|
| 10 |
-
|
| 11 |
def handle_math(user_input):
|
| 12 |
user_input = user_input.lower().strip()
|
| 13 |
|
|
|
|
| 14 |
greetings = ["hi", "hello", "hey", "yo"]
|
| 15 |
if any(greet in user_input for greet in greetings):
|
| 16 |
return f"Hello! I am {BOT_NAME}. I can help you with math. Try something like: (3 + 5) * -2"
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
math_expression = re.sub(r"[^\d\.\+\-\*/\(\)\s]", "", user_input)
|
| 20 |
math_expression = math_expression.replace("what is", "").strip()
|
| 21 |
|
| 22 |
try:
|
| 23 |
-
# Try to evaluate simple math expressions
|
| 24 |
result = eval(math_expression)
|
| 25 |
return f"The answer is {result}"
|
| 26 |
except:
|
| 27 |
-
# If that fails, optionally, try to use your ML model to predict sum (example)
|
| 28 |
-
try:
|
| 29 |
-
# Example: parse two numbers and predict their sum with ML model
|
| 30 |
-
numbers = [float(n) for n in re.findall(r"[-+]?\d*\.\d+|\d+", math_expression)]
|
| 31 |
-
if len(numbers) == 2:
|
| 32 |
-
x_input = np.array([numbers]) / 100.0 # normalize if model trained like that
|
| 33 |
-
pred = model.predict(x_input)
|
| 34 |
-
pred = pred[0][0] * 200.0 # scale back if needed
|
| 35 |
-
return f"My ML model predicts the sum is approximately: {pred:.2f}"
|
| 36 |
-
except:
|
| 37 |
-
pass
|
| 38 |
return "Sorry, I couldn't understand that. Please enter a valid math expression like (2 + 3) * -1"
|
| 39 |
|
| 40 |
-
|
|
|
|
| 41 |
fn=handle_math,
|
| 42 |
inputs="text",
|
| 43 |
outputs="text",
|
| 44 |
title="Phu Quy Ho – Smart Math Solver",
|
| 45 |
description="Say hello or ask me a math question! I support +, -, *, /, negative numbers, and parentheses."
|
| 46 |
-
)
|
| 47 |
-
|
| 48 |
-
demo.launch()
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
|
| 3 |
+
dataset = pd.read_csv('cancer.csv')
|
| 4 |
+
|
| 5 |
+
x = dataset.drop(columns=["diagnosis(1=m, 0=b)"])
|
| 6 |
+
|
| 7 |
+
y = dataset["diagnosis(1=m, 0=b)"]
|
| 8 |
+
|
| 9 |
+
from sklearn.model_selection import train_test_split
|
| 10 |
+
|
| 11 |
+
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
|
| 12 |
+
|
| 13 |
import tensorflow as tf
|
| 14 |
+
|
| 15 |
+
model = tf.keras.models.Sequential()
|
| 16 |
+
|
| 17 |
+
model.add(tf.keras.layers.Dense(256, input_shape=x_train.shape, activation='sigmoid'))
|
| 18 |
+
model.add(tf.keras.layers.Dense(256, activation='sigmoid'))
|
| 19 |
+
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))
|
| 20 |
+
|
| 21 |
+
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
|
| 22 |
+
|
| 23 |
+
model.fit(x_train, y_train, epochs=1000)
|
| 24 |
+
|
| 25 |
+
model.evaluate(x_test, y_test)
|
| 26 |
+
|
| 27 |
+
model.save("percyAI_model.h5")
|
| 28 |
+
|
| 29 |
+
from google.colab import files
|
| 30 |
+
files.download("percyAI_model.h5")
|
| 31 |
+
|
| 32 |
+
from tensorflow.keras.models import load_model
|
| 33 |
+
model = load_model("percyAI_model.h5")
|
| 34 |
+
|
| 35 |
import numpy as np
|
| 36 |
+
import tensorflow as tf
|
| 37 |
+
from tensorflow import keras
|
| 38 |
+
from tensorflow.keras import layers
|
| 39 |
+
|
| 40 |
+
x_values = np.random.randint(0, 100, (10000, 2))
|
| 41 |
+
y_values = np.sum(x_values, axis=1)
|
| 42 |
+
|
| 43 |
+
x_values = x_values / 100.0
|
| 44 |
+
y_values = y_values / 200.0
|
| 45 |
+
|
| 46 |
+
model = keras.Sequential([
|
| 47 |
+
layers.Dense(64, activation='relu', input_shape=(2,)),
|
| 48 |
+
layers.Dense(64, activation='relu'),
|
| 49 |
+
layers.Dense(1)
|
| 50 |
+
])
|
| 51 |
+
|
| 52 |
+
model.compile(optimizer='adam', loss='mse', metrics=['mae'])
|
| 53 |
+
|
| 54 |
+
model.fit(x_values, y_values, epochs=30, batch_size=32, validation_split=0.2)
|
| 55 |
+
|
| 56 |
+
test_input = np.array([[25, 30]]) / 100.0
|
| 57 |
+
prediction = model.predict(test_input) * 200.0
|
| 58 |
+
print("Predicted sum:", prediction[0][0])
|
| 59 |
+
|
| 60 |
+
model.save("percyAI_model.h5")
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
model.save("percyAI_model.h5")
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
import gradio as gr
|
| 69 |
import re
|
| 70 |
|
| 71 |
BOT_NAME = "Phu Quy Ho"
|
| 72 |
|
| 73 |
+
# Basic math evaluator (with safety)
|
|
|
|
|
|
|
| 74 |
def handle_math(user_input):
|
| 75 |
user_input = user_input.lower().strip()
|
| 76 |
|
| 77 |
+
# Respond to greetings
|
| 78 |
greetings = ["hi", "hello", "hey", "yo"]
|
| 79 |
if any(greet in user_input for greet in greetings):
|
| 80 |
return f"Hello! I am {BOT_NAME}. I can help you with math. Try something like: (3 + 5) * -2"
|
| 81 |
|
| 82 |
+
# Remove filler words like "what is"
|
| 83 |
+
math_expression = re.sub(r"[^\d\.\+\-\*/\(\)\s]", "", user_input) # Strip out words, keep symbols
|
| 84 |
math_expression = math_expression.replace("what is", "").strip()
|
| 85 |
|
| 86 |
try:
|
|
|
|
| 87 |
result = eval(math_expression)
|
| 88 |
return f"The answer is {result}"
|
| 89 |
except:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
return "Sorry, I couldn't understand that. Please enter a valid math expression like (2 + 3) * -1"
|
| 91 |
|
| 92 |
+
# Gradio UI
|
| 93 |
+
gr.Interface(
|
| 94 |
fn=handle_math,
|
| 95 |
inputs="text",
|
| 96 |
outputs="text",
|
| 97 |
title="Phu Quy Ho – Smart Math Solver",
|
| 98 |
description="Say hello or ask me a math question! I support +, -, *, /, negative numbers, and parentheses."
|
| 99 |
+
).launch()
|
|
|
|
|
|