PercyHo commited on
Commit
93d7763
·
verified ·
1 Parent(s): 9192a9e

Upload ai.py

Browse files
Files changed (1) hide show
  1. ai.py +112 -0
ai.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+