-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
208 lines (184 loc) · 7.9 KB
/
main.py
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
from flask import Flask, redirect, request, Response
from g4f.client import Client
from flask_cors import CORS
from werkzeug.exceptions import HTTPException
import json
from datetime import datetime
import speech_recognition as sr
import requests
import subprocess
import os
import os.path
from g4f.cookies import set_cookies_dir, read_cookie_files
import asyncio
import g4f.debug
g4f.debug.logging = True
if os.name == "nt":
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
app = Flask(__name__)
CORS(app, origins="https://www.projet-voltaire.fr")
client = Client()
if os.path.exists("har_and_cookies"):
cookies_dir = os.path.join(os.path.dirname(__file__), "har_and_cookies")
set_cookies_dir(cookies_dir)
read_cookie_files(cookies_dir)
r = sr.Recognizer()
@app.route("/")
def home():
"""This route redirect to the GitHub repository of the project."""
return redirect("https://github.com/JustYuuto/projet-voltaire-bot", code=301)
@app.route("/robots.txt")
def robots():
"""This route return the robots.txt file."""
response = Response("User-agent: *\nDisallow:")
response.content_type = "text/plain"
return response
@app.route("/fix-sentence", methods=["POST"])
def fix_sentence():
"""
This route will fix a sentence, given in the body of the request. Hence, the only method allowed is POST.
In the body, one parameter need to be given: "sentence", which is the sentence to fix.
Here, we're using G4F to fix the sentence.
"""
if not request.json or "sentence" not in request.json:
response = Response(json.dumps({
"status": 400,
"message": "Bad Request",
"description": "The request must be a JSON with a key \"sentence\"."
}), status=400, content_type="application/json")
raise HTTPException("Bad Request", response=response)
now = datetime.now()
sentence = request.json["sentence"]
prompt = ("Corrige les fautes dans cette phrase : \"{}\". Répond avec du JSON avec la clé \"word_to_click\" avec "
"comme valeur le mot non corrigé qui a été corrigé, ou null s'il n'y a pas de fautes.").format(sentence)
print("Prompt:", prompt)
response = client.chat.completions.create(
model="gpt-4",
response_format={"type": "json_object"},
messages=[{
"role": "user", "content": prompt
}],
max_tokens=500,
)
res_json = json.loads(response.choices[0].message.content)
print("Response:", res_json)
return Response(json.dumps({
"word_to_click": res_json["word_to_click"],
"time_taken": (datetime.now() - now).total_seconds(),
}), content_type="application/json")
@app.route("/intensive-training", methods=["POST"])
def intensive_training():
"""
Perform intensive training using the Projet Voltaire Bot.
This function takes a JSON request with a list of sentences and a rule, and uses the Projet Voltaire Bot to generate a response in French. The response is a JSON object that contains the correctness of each sentence according to the given rule.
Returns:
A JSON response containing the correctness of each sentence.
Raises:
HTTPException: If the request is invalid or missing required fields.
thank you github copilot
"""
if not request.json or "sentences" not in request.json or "rule" not in request.json:
response = Response(json.dumps({
"status": 400,
"message": "Bad Request",
"description": "The request must be a JSON with a key \"sentences\" and a key \"rule\"."
}), status=400, content_type="application/json")
raise HTTPException("Bad Request", response=response)
sentences = request.json["sentences"]
rule = request.json["rule"]
#prompt = "Suivant la règle : \"{}\" Les phrases :\n- {}\nSont elles correctes ? Répond avec du JSON avec un tableau d'objets qui prend comme clés \"sentence\" pour la phrase et la clé \"correct\" si cette dernière est correcte.".format(rule, "\n- ".join(sentences))
prompt = ("Les phrases :\n- {}\nSont elles correctes ? Répond avec un tableau JSON qui prend comme valeur un boolean"
" si cette dernière est correcte (sous le format [true, false, true]).".format("\n- ".join(sentences)))
print("Prompt:", prompt)
response = client.chat.completions.create(
model="gpt-4",
response_format={"type": "json_object"},
messages=[{
"role": "user", "content": prompt
}],
max_tokens=500,
)
res_json = json.loads(response.choices[0].message.content)
return Response(json.dumps(res_json), content_type="application/json")
@app.route("/put-word", methods=["POST"])
def put_word():
"""
This word will add a missing word to the sentence given in the body of the request.
The user also needs to provide the audio URL for completing the sentence with the missing word.
It's using SpeechRecognition to get the missing word from the audio.
"""
if not request.json or "sentence" not in request.json or "audio_url" not in request.json:
response = Response(json.dumps({
"status": 400,
"message": "Bad Request",
"description": "The request must be a JSON with a key \"sentence\" and a key \"audio_url\"."
}), status=400, content_type="application/json")
raise HTTPException("Bad Request", response=response)
sentence: str = request.json["sentence"]
if "{}" not in sentence:
response = Response(json.dumps({
"status": 400,
"message": "Bad Request",
"description": "The sentence must contain a \"{}\" to put the missing word."
}), status=400, content_type="application/json")
raise HTTPException("Bad Request", response=response)
audio_url: str = request.json["audio_url"]
print(sentence)
if " " in sentence:
sentence = sentence.replace(" ", " {} ")
audio_file = requests.get(audio_url)
audio_filename = os.path.abspath("./audio{}.mp3".format(datetime.timestamp(datetime.now())))
audio_wav_filename = audio_filename[:-3] + 'wav'
with open(audio_filename, "wb") as f:
f.write(audio_file.content)
subprocess.run(['ffmpeg', '-i', audio_filename, audio_wav_filename])
with sr.AudioFile(audio_wav_filename) as source:
audio = r.record(source)
fixed_sentence_stt: str = r.recognize_google(audio, language="fr-FR")
try:
missing_word_index = sentence.split(" ").index("{}")
except ValueError:
missing_word_index = sentence.split(" ").index("{}.")
missing_word = fixed_sentence_stt.split()[missing_word_index]
fixed_sentence = sentence.replace("{}", missing_word)
os.remove(audio_filename)
os.remove(audio_wav_filename)
return Response(json.dumps({
"sentence": sentence,
"fixed_sentence": fixed_sentence,
"missing_word": missing_word,
}), content_type="application/json")
@app.route("/nearest-word", methods=["POST"])
def nearest_word():
if not request.json or "word" not in request.json or "nearest_words" not in request.json:
response = Response(json.dumps({
"status": 400,
"message": "Bad Request",
"description": "The request must be a JSON with a key \"word\" and a key \"nearest_words\"."
}), status=400, content_type="application/json")
raise HTTPException("Bad Request", response=response)
word: str = request.json["word"]
nearest_words: list = request.json["nearest_words"]
prompt = "Quel est le mot le plus proche de \"{}\" parmi : {}. Répond en json avec une clé \"word\".".format(word, ", ".join(nearest_words))
nearest_word = json.loads(client.chat.completions.create(
model="gpt-4",
response_format={ "type": "json_object" },
messages=[{
"role": "user", "content": prompt
}],
max_tokens=500,
).choices[0].message.content)
return Response(json.dumps({
"word": nearest_word['word'],
}), content_type="application/json")
@app.errorhandler(HTTPException)
def handle_exception(e):
"""Return JSON instead of HTML for HTTP errors."""
response = e.get_response()
response.data = json.dumps({
"status": e.code,
"message": e.name,
"description": e.description,
})
response.content_type = "application/json"
return response