-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
191 lines (167 loc) · 4.92 KB
/
app.js
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import express from 'express';
import bodyParser from 'body-parser';
import dotenv from 'dotenv';
// import Anthropic from '@anthropic-ai/sdk';
import { env } from 'process';
import Axios from 'axios'
dotenv.config();
const getPrompt = async (character, data) => {
try {
const response = await Axios.post('https://api.openai.com/v1/chat/completions', {
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "system",
"content": "You are a city planner for " + city + ". You are tasked with making green spaces. Given that the city has a " + character + " of " + data + " what should be considered about building greenspaces?"
},
]
}, {
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer sk-proj-i8wOPe2wCcRXzwafuKDrT3BlbkFJLyg58wVN0fUTmjpBHAIq'
}
});
console.log(response.data.choices[0].message.content);
return response.data.choices[0].message.content;
} catch (error) {
console.error(error);
}
};
const port = 3000;
const app = express();
app.use(express.static("public"));
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(
express.static("node_modules/bootstrap/dist/")
);
const getPrediction = async (town) => {
return Axios.get('http://127.0.0.1:5000/' + town)
.then(response => {
console.log(response)
return response;
})
.catch(error => {
console.error(error);
});
}
app.post('/predict', async(req, res) => {
const formInfo = req.body.city;
console.log(formInfo)
const response = (await getPrediction(formInfo)).data
console.log("Response: " + response)
//city = response.City[1]
//population = response.Population[1]
//res.send("Hello")
data = Object.keys(response).reduce((obj, key) => ({... obj, [key]:response[key][Object.keys(response[key])[0]]}), {});
city = data["City"]
temperature = data["Temperature"]
airQuality = data["Air Quality"]
population = data["Population"]
density = data["Population Density"]
precipitation = data["Precipation"]
landUse = data["Land Type"]
cities = cities
zipCode = data["Zip Code"]
hasPark = data["Has Park"]
res.render('index.ejs', {
hasPark: data["Has Park"],
city: city,
temperature: data["Temperature"],
airQuality: data["Air Quality"],
population: data["Population"],
density: data["Population Density"],
precipitation: data["Precipation"],
landUse: data["Land Type"],
cities: cities,
zipCode: zipCode
})
})
// Define the endpoint
app.get('/prompt', async (req, res) => {
try {
const prompt = await getPrompt();
res.send(prompt);
} catch (error) {
res.status(500).send('Error communicating with the API');
}
});
let data = {}
let city = "Pasadena";
let temperature = 0;
let airQuality = 0;
let population = 0;
let density = 50;
let precipitation = 0;
let landUse = 0;
let cities = [];
let hasPark = 0;
let zipCode = 0;
app.get('/', (req, res) => {
Axios.get("http://127.0.0.1:5000/cities").then(response => {
// Extracting just the values from the JSON object and converting it to an array
cities = Object.values(response.data);
console.log(cities);
// Render the page after retrieving and processing the data
res.render('index.ejs', {
hasPark: false,
city: city,
temperature: temperature,
airQuality: airQuality,
population: population,
density: density,
precipitation: precipitation,
landUse: landUse,
cities: cities,
zipCode: 91950
});
})
.catch(error => {
console.error(error);
// If there's an error, render the page with empty cities array
res.render('index.ejs', {
temperature: temperature,
airQuality: airQuality,
population: population,
density: density,
precipitation: precipitation,
landUse: landUse,
cities: cities
});
});
});
app.get('/prompttemperature', async (req, res) => {
try {
const prompt = await getPrompt("temperature", temperature);
res.send(prompt);
} catch (error) {
res.status(500).send('Error communicating with the API');
}
});
app.get('/promptairquality', async (req, res) => {
try {
const prompt = await getPrompt("air quality", airQuality);
res.send(prompt);
} catch (error) {
res.status(500).send('Error communicating with the API');
}
});
app.get('/promptpopulation', async (req, res) => {
try {
const prompt = await getPrompt("population", population);
res.send(prompt);
} catch (error) {
res.status(500).send('Error communicating with the API');
}
});
app.get('/promptdensity', async (req, res) => {
try {
const prompt = await getPrompt("density", density);
res.send(prompt);
} catch (error) {
res.send(error)
}
});
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});