-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
149 lines (110 loc) · 4.86 KB
/
server.py
File metadata and controls
149 lines (110 loc) · 4.86 KB
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
# Blueberry - your personalized AI assistant.
# Copyright (C) 2024 BillyDoesDev
# Email: DarkKnight450@protonmail.com
from flask import Flask, render_template, request
from flask_socketio import SocketIO, emit
import requests
import json
import os
import shutil
try:
from static.assets.scripts import source_text
except ModuleNotFoundError:
print("Ahh, looks like no creative mode for you :/ Check the error logs and try again")
# print(source_text.parse_source_file("uploads/climate.txt", "what is climate change"))
app = Flask(__name__)
socketio = SocketIO(app)
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
# print(request.files)
# Check if the POST request has the file part
if "file" not in request.files:
return render_template("index.html", message="No file part")
# print(request.files)
file = request.files["file"]
# If the user does not select a file, browser submits an empty file
if file.filename == "":
return render_template("index.html", message="No selected file")
# If the file is present and has an allowed extension, save it
if file:
print("******************DOWNLOADED***********************")
# You can customize the upload path as needed
# if str(file.filename).split(".")[1] != "txt":
# return render_template("index.html", confirmation_message="accept only .txt files")
try:
shutil.rmtree("uploads")
except FileNotFoundError: pass
os.mkdir("uploads")
file.save("uploads/" + file.filename)
message = "File successfully uploaded: " + file.filename
print(message)
# generated_text = source_text.parse_source_file(f"uploads/{file.filename}", "what is climate change")
socketio.emit("ass-response", {"current_condition": 0})
print(">>>>>>>>>>>>>>>>>>>>>>>>> I'm here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
return render_template("index.html", confirmation_message=f"{file.filename} was successfully uploaded.")
return render_template("index.html", message=None)
@socketio.on("activee")
def handle_my_custom_event(json):
print("received json: " + str(json))
# @app.route('/alarms')
# def alarms():
# @after_this_request
# def add_header(response):
# response.headers['Access-Control-Allow-Origin'] = '*'
# return response
# with open("data/alarms.json", mode="r") as f:
# data = json.load(f)
# return data
@socketio.on("weather-request")
def fetch_weather(json_data):
print(f"recieved: {json_data}")
phrase = str(json_data["data"]).strip()
# if phrase.endswith("weather"):
# location = phrase.split(" ")[0]
# else:
# location = phrase.split(" ")[-1]
location = phrase.split('in')[1].strip()
try:
r = requests.get(f"http://wttr.in/{location}", params={"format": "j1"})
emit("weather-response", json.loads(r.text))
print(r.url, r.text)
except requests.exceptions.ConnectionError:
emit("weather-response", {"current_condition": 0})
print("server fault :/")
@socketio.on("alarm-set")
def alarm_set(json_data):
print(f"recieved: {json_data}")
phrase = str(json_data["data"]).strip() # set an alarm for 5:04 p.m.
hourNminute = __import__("re").search("\d+:\d+", phrase).group()
time_string = f"{hourNminute}:00 {'PM' if 'p.m.' in phrase else 'AM'}"
print(time_string)
with open("data/alarms.json", mode="r") as f:
alarm_data = json.load(f)
with open("data/alarms.json", mode="w") as f:
current_alarms = alarm_data["alarms"]
if time_string not in current_alarms:
current_alarms.append(time_string)
alarm_data["alarms"] = current_alarms
json.dump(alarm_data, f, indent=4)
emit("alarm-response", alarm_data)
@socketio.on("alarm-modify") # this is for removing the alarm that finished ringing
def alarm_modify(json_data):
print(f"recieved: {json_data}") # {'data': '8:21:00 PM'}
current_alarmaaaa = json_data["data"]
with open("data/alarms.json", mode="r") as f:
alarm_data = json.load(f)
with open("data/alarms.json", mode="w") as f:
current_alarms = alarm_data["alarms"]
current_alarms.remove(current_alarmaaaa)
alarm_data["alarms"] = current_alarms
json.dump(alarm_data, f, indent=4)
emit("alarm-response", alarm_data)
@socketio.on("creative-query")
def creative_parse(json_data):
print(f"recieved: {json_data}")
phrase = str(json_data["data"]).strip()
generated_text = source_text.parse_source_file(f"uploads/{os.listdir('uploads')[0]}", phrase)
emit('creative-response', {"response": generated_text})
if __name__ == "__main__":
socketio.run(app, debug=True)