PercyHo commited on
Commit
5c870a0
·
verified ·
1 Parent(s): 2ae1280

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -85
app.py CHANGED
@@ -1,112 +1,48 @@
1
- # -*- coding: utf-8 -*-
2
- """AI.ipynb
3
-
4
- Automatically generated by Colab.
5
-
6
- Original file is located at
7
- https://colab.research.google.com/drive/1nPFOaItiXTX4ZGbA-dJrrXsrvf4yvsAz
8
- """
9
-
10
- import pandas as pd
11
-
12
- dataset = pd.read_csv('cancer.csv')
13
-
14
- x = dataset.drop(columns=["diagnosis(1=m, 0=b)"])
15
-
16
- y = dataset["diagnosis(1=m, 0=b)"]
17
-
18
- from sklearn.model_selection import train_test_split
19
-
20
- x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
21
-
22
  import tensorflow as tf
23
-
24
- model = tf.keras.models.Sequential()
25
-
26
- model.add(tf.keras.layers.Dense(256, input_shape=x_train.shape, activation='sigmoid'))
27
- model.add(tf.keras.layers.Dense(256, activation='sigmoid'))
28
- model.add(tf.keras.layers.Dense(1, activation='sigmoid'))
29
-
30
- model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
31
-
32
- model.fit(x_train, y_train, epochs=1000)
33
-
34
- model.evaluate(x_test, y_test)
35
-
36
- model.save("percyAI_model.h5")
37
-
38
- from google.colab import files
39
- files.download("percyAI_model.h5")
40
-
41
- from tensorflow.keras.models import load_model
42
- model = load_model("percyAI_model.h5")
43
-
44
  import numpy as np
45
- import tensorflow as tf
46
- from tensorflow import keras
47
- from tensorflow.keras import layers
48
-
49
- x_values = np.random.randint(0, 100, (10000, 2))
50
- y_values = np.sum(x_values, axis=1)
51
-
52
- x_values = x_values / 100.0
53
- y_values = y_values / 200.0
54
-
55
- model = keras.Sequential([
56
- layers.Dense(64, activation='relu', input_shape=(2,)),
57
- layers.Dense(64, activation='relu'),
58
- layers.Dense(1)
59
- ])
60
-
61
- model.compile(optimizer='adam', loss='mse', metrics=['mae'])
62
-
63
- model.fit(x_values, y_values, epochs=30, batch_size=32, validation_split=0.2)
64
-
65
- test_input = np.array([[25, 30]]) / 100.0
66
- prediction = model.predict(test_input) * 200.0
67
- print("Predicted sum:", prediction[0][0])
68
-
69
- model.save("percyAI_model.h5")
70
-
71
- from google.colab import files
72
- files.download("percyAI_model.h5")
73
-
74
- model.save("percyAI_model.h5")
75
-
76
- !pip install gradio
77
-
78
- import gradio as gr
79
  import re
80
 
81
  BOT_NAME = "Phu Quy Ho"
82
 
83
- # Basic math evaluator (with safety)
 
 
84
  def handle_math(user_input):
85
  user_input = user_input.lower().strip()
86
 
87
- # Respond to greetings
88
  greetings = ["hi", "hello", "hey", "yo"]
89
  if any(greet in user_input for greet in greetings):
90
  return f"Hello! I am {BOT_NAME}. I can help you with math. Try something like: (3 + 5) * -2"
91
 
92
- # Remove filler words like "what is"
93
- math_expression = re.sub(r"[^\d\.\+\-\*/\(\)\s]", "", user_input) # Strip out words, keep symbols
94
  math_expression = math_expression.replace("what is", "").strip()
95
 
96
  try:
 
97
  result = eval(math_expression)
98
  return f"The answer is {result}"
99
  except:
 
 
 
 
 
 
 
 
 
 
 
100
  return "Sorry, I couldn't understand that. Please enter a valid math expression like (2 + 3) * -1"
101
 
102
- # Gradio UI
103
- gr.Interface(
104
  fn=handle_math,
105
  inputs="text",
106
  outputs="text",
107
  title="Phu Quy Ho – Smart Math Solver",
108
  description="Say hello or ask me a math question! I support +, -, *, /, negative numbers, and parentheses."
109
- ).launch()
110
-
111
- model.save("percyAI_model.h5")
112
 
 
 
1
+ import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import tensorflow as tf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  import numpy as np
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  import re
5
 
6
  BOT_NAME = "Phu Quy Ho"
7
 
8
+ # Load your trained model (make sure percyAI_model.h5 is uploaded to your Space)
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
+ # Clean the input, only keep math symbols and digits
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
+ demo = gr.Interface(
 
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()