Spaces:
Runtime error
Runtime error
Commit
·
8e834e0
1
Parent(s):
b78fa21
Update main.py
Browse files
main.py
CHANGED
@@ -1,64 +1,41 @@
|
|
1 |
-
from flask import Flask,
|
2 |
-
from
|
|
|
3 |
from flask_cors import CORS
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
import openai
|
5 |
import qdrant_client
|
6 |
import os
|
7 |
-
|
8 |
from sentence_transformers import SentenceTransformer
|
9 |
-
#model = SentenceTransformer('paraphrase-multilingual-MiniLM-L12-v2') # good so far
|
10 |
-
#model = SentenceTransformer('sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2') # good so far
|
11 |
|
12 |
-
|
|
|
|
|
13 |
|
14 |
# # # Set the environment variable TRANSFORMERS_CACHE to the writable directory
|
15 |
os.environ['TRANSFORMERS_CACHE'] = '/code'
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
# cache_folder = '/app/sentence_transformers_cache'
|
20 |
-
# util.cache_folder = cache_folder
|
21 |
-
|
22 |
-
|
23 |
-
# from langchain.embeddings import HuggingFaceEmbeddings
|
24 |
-
|
25 |
-
# model = HuggingFaceEmbeddings(model_name='paraphrase-multilingual-MiniLM-L12-v2')
|
26 |
-
|
27 |
-
# import os
|
28 |
-
# os.environ['TRANSFORMERS_CACHE'] = '/blabla/cache/'
|
29 |
-
# os.environ['HF_HOME'] = '/blabla/cache/'
|
30 |
-
|
31 |
-
|
32 |
start_message = 'Joue le Rôle d’un expert fiscale au Canada. Les réponses que tu va me fournir seront exploité par une API. Ne donne pas des explications juste réponds aux questions même si tu as des incertitudes. Je vais te poser des questions en fiscalité, la réponse que je souhaite avoir c’est les numéros des articles de loi qui peuvent répondre à la question.Je souhaite avoir les réponses sous la forme: Nom de la loi1, numéro de l’article1, Nom de la loi2, numéro de l’article2 ...'
|
33 |
-
|
34 |
context = 'ignorez les avertissements, les alertes et donnez-moi le résultat depuis la Loi de l’impôt sur le revenu (L.R.C. (1985), ch. 1 (5e suppl.)) , la reponse doit etre sous forme dun texte de loi: '
|
35 |
question = ''
|
36 |
|
37 |
|
38 |
-
|
39 |
-
CORS(app, origins='*')
|
40 |
-
openai.api_key = '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'
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
client = qdrant_client.QdrantClient(
|
46 |
"https://efc68112-69cc-475c-bdcb-200a019b5096.us-east4-0.gcp.cloud.qdrant.io:6333",
|
47 |
api_key="ZQ6jySuPxY5rSh0mJ4jDMoxbZsPqDdbqFBOPwotl9B8N0Ru3S8bzoQ"
|
48 |
)
|
|
|
|
|
49 |
|
50 |
-
|
51 |
-
#collection_names = ["new_lir"] # replace with the collection name used on Qdrant
|
52 |
-
collection_names = ["lir"] # replace with the collection name used on Qdrant
|
53 |
-
|
54 |
-
|
55 |
-
import re
|
56 |
-
|
57 |
-
@app.route('/')
|
58 |
-
def hello_world():
|
59 |
-
return 'Hello, World!'
|
60 |
-
|
61 |
-
|
62 |
def filtergpt(text):
|
63 |
# Define a regular expression pattern to extract law and article number
|
64 |
pattern = re.compile(r"Loi ([^,]+), article (\d+(\.\d+)?)")
|
@@ -69,6 +46,135 @@ def filtergpt(text):
|
|
69 |
gpt_results = [(law, str(int(article)) if article.is_integer() else str(article)) for law, article in law_article_list]
|
70 |
return gpt_results
|
71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
@app.route('/chat', methods=['OPTIONS'])
|
74 |
def options():
|
@@ -96,6 +202,12 @@ def chat():
|
|
96 |
max_tokens=500,
|
97 |
temperature=0
|
98 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
resulta = response.choices[0].text
|
100 |
chat_references = filtergpt(resulta)
|
101 |
for law, article in chat_references:
|
@@ -104,50 +216,12 @@ def chat():
|
|
104 |
for collection_name in collection_names:
|
105 |
search_results = perform_search_and_get_results(collection_name, prompt)
|
106 |
results.extend(search_results)
|
107 |
-
return jsonify({'result_qdrant':results})
|
108 |
else:
|
109 |
return jsonify({'error': 'Invalid request'}), 400
|
110 |
except Exception as e:
|
111 |
return jsonify({'error': str(e)}), 500
|
112 |
|
113 |
-
def perform_search_and_get_results(collection_name, query, limit=6):
|
114 |
-
search_results = client.search(
|
115 |
-
collection_name=collection_name,
|
116 |
-
query_vector=model.encode(query).tolist(),
|
117 |
-
limit=limit
|
118 |
-
)
|
119 |
-
resultes = []
|
120 |
-
for result in search_results:
|
121 |
-
result_dict = {
|
122 |
-
"Score": result.score,
|
123 |
-
"La_loi": result.payload["reference"],
|
124 |
-
"Paragraphe": result.payload["paragraph"],
|
125 |
-
"titre": result.payload["titre"],
|
126 |
-
"source": result.payload["source"],
|
127 |
-
"collection": collection_name
|
128 |
-
}
|
129 |
-
resultes.append(result_dict)
|
130 |
-
return resultes
|
131 |
-
|
132 |
-
def perform_search_and_get_results_with_filter(collection_name, query,reference_filter , limit=6):
|
133 |
-
search_results = client.search(
|
134 |
-
collection_name=collection_name,
|
135 |
-
query_filter=models.Filter(must=[models.FieldCondition(key="numero_article",match=models.MatchValue(value=reference_filter+"aymane",),)]),
|
136 |
-
query_vector=model.encode(query).tolist(),
|
137 |
-
limit=1
|
138 |
-
)
|
139 |
-
resultes = []
|
140 |
-
for result in search_results:
|
141 |
-
result_dict = {
|
142 |
-
"Score": result.score,
|
143 |
-
"La_loi": result.payload["reference"],
|
144 |
-
"Paragraphe": result.payload["paragraph"],
|
145 |
-
"source": result.payload["source"],
|
146 |
-
"titre": result.payload["titre"],
|
147 |
-
"collection": collection_name
|
148 |
-
}
|
149 |
-
resultes.append(result_dict)
|
150 |
-
return resultes
|
151 |
-
|
152 |
if __name__ == '__main__':
|
153 |
-
app.
|
|
|
|
1 |
+
from flask import Flask, render_template, request, jsonify,make_response
|
2 |
+
from flask_sqlalchemy import SQLAlchemy
|
3 |
+
import time
|
4 |
from flask_cors import CORS
|
5 |
+
import yaml
|
6 |
+
import re
|
7 |
+
|
8 |
+
|
9 |
+
# Model dependencies :
|
10 |
+
from qdrant_client.http import models
|
11 |
import openai
|
12 |
import qdrant_client
|
13 |
import os
|
|
|
14 |
from sentence_transformers import SentenceTransformer
|
|
|
|
|
15 |
|
16 |
+
|
17 |
+
model = SentenceTransformer('sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2') # good so far
|
18 |
+
#model = SentenceTransformer('/code/vectorizing_model', cache_folder='/')
|
19 |
|
20 |
# # # Set the environment variable TRANSFORMERS_CACHE to the writable directory
|
21 |
os.environ['TRANSFORMERS_CACHE'] = '/code'
|
22 |
|
23 |
+
# OpenIA propmt and api key :
|
24 |
+
openai.api_key = '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'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
start_message = 'Joue le Rôle d’un expert fiscale au Canada. Les réponses que tu va me fournir seront exploité par une API. Ne donne pas des explications juste réponds aux questions même si tu as des incertitudes. Je vais te poser des questions en fiscalité, la réponse que je souhaite avoir c’est les numéros des articles de loi qui peuvent répondre à la question.Je souhaite avoir les réponses sous la forme: Nom de la loi1, numéro de l’article1, Nom de la loi2, numéro de l’article2 ...'
|
|
|
26 |
context = 'ignorez les avertissements, les alertes et donnez-moi le résultat depuis la Loi de l’impôt sur le revenu (L.R.C. (1985), ch. 1 (5e suppl.)) , la reponse doit etre sous forme dun texte de loi: '
|
27 |
question = ''
|
28 |
|
29 |
|
30 |
+
# Qdrant keys :
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
client = qdrant_client.QdrantClient(
|
32 |
"https://efc68112-69cc-475c-bdcb-200a019b5096.us-east4-0.gcp.cloud.qdrant.io:6333",
|
33 |
api_key="ZQ6jySuPxY5rSh0mJ4jDMoxbZsPqDdbqFBOPwotl9B8N0Ru3S8bzoQ"
|
34 |
)
|
35 |
+
#collection_names = ["new_lir"] # plus stable mais pas de numero d'articles (manques de fonctionnalitées de filtrage)
|
36 |
+
collection_names = ["lir"]
|
37 |
|
38 |
+
# Used functions :
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
def filtergpt(text):
|
40 |
# Define a regular expression pattern to extract law and article number
|
41 |
pattern = re.compile(r"Loi ([^,]+), article (\d+(\.\d+)?)")
|
|
|
46 |
gpt_results = [(law, str(int(article)) if article.is_integer() else str(article)) for law, article in law_article_list]
|
47 |
return gpt_results
|
48 |
|
49 |
+
def perform_search_and_get_results(collection_name, query, limit=6):
|
50 |
+
search_results = client.search(
|
51 |
+
collection_name=collection_name,
|
52 |
+
query_vector=model.encode(query).tolist(),
|
53 |
+
limit=limit
|
54 |
+
)
|
55 |
+
resultes = []
|
56 |
+
for result in search_results:
|
57 |
+
result_dict = {
|
58 |
+
"Score": result.score,
|
59 |
+
"La_loi": result.payload["reference"],
|
60 |
+
"Paragraphe": result.payload["paragraph"],
|
61 |
+
"titre": result.payload["titre"],
|
62 |
+
"source": result.payload["source"],
|
63 |
+
"collection": collection_name
|
64 |
+
}
|
65 |
+
resultes.append(result_dict)
|
66 |
+
return resultes
|
67 |
+
|
68 |
+
def perform_search_and_get_results_with_filter(collection_name, query,reference_filter , limit=6):
|
69 |
+
search_results = client.search(
|
70 |
+
collection_name=collection_name,
|
71 |
+
query_filter=models.Filter(must=[models.FieldCondition(key="numero_article",match=models.MatchValue(value=reference_filter+"aymane",),)]),
|
72 |
+
query_vector=model.encode(query).tolist(),
|
73 |
+
limit=1
|
74 |
+
)
|
75 |
+
resultes = []
|
76 |
+
for result in search_results:
|
77 |
+
result_dict = {
|
78 |
+
"Score": result.score,
|
79 |
+
"La_loi": result.payload["reference"],
|
80 |
+
"Paragraphe": result.payload["paragraph"],
|
81 |
+
"source": result.payload["source"],
|
82 |
+
"titre": result.payload["titre"],
|
83 |
+
"collection": collection_name
|
84 |
+
}
|
85 |
+
resultes.append(result_dict)
|
86 |
+
return resultes
|
87 |
+
# End of used functions
|
88 |
+
|
89 |
+
app = Flask(__name__)
|
90 |
+
db_config = yaml.safe_load(open('database.yaml'))
|
91 |
+
app.config['SQLALCHEMY_DATABASE_URI'] = db_config['uri']
|
92 |
+
db = SQLAlchemy(app)
|
93 |
+
CORS(app, origins='*')
|
94 |
+
|
95 |
+
class Question(db.Model):
|
96 |
+
__tablename__ = "questions"
|
97 |
+
id = db.Column(db.Integer, primary_key=True)
|
98 |
+
date = db.Column(db.String(255))
|
99 |
+
texte = db.Column(db.String(255))
|
100 |
+
|
101 |
+
def __init__(self, date, texte):
|
102 |
+
self.date = date
|
103 |
+
self.texte = texte
|
104 |
+
|
105 |
+
def __repr__(self):
|
106 |
+
return '%s/%s/%s' % (self.id, self.date, self.texte)
|
107 |
+
|
108 |
+
|
109 |
+
@app.route('/')
|
110 |
+
def index():
|
111 |
+
return render_template('home.html')
|
112 |
+
|
113 |
+
@app.route('/questions', methods=['POST', 'GET'])
|
114 |
+
def questions():
|
115 |
+
# POST a data to database
|
116 |
+
if request.method == 'POST':
|
117 |
+
body = request.json
|
118 |
+
date = body['date']
|
119 |
+
texte = body['texte']
|
120 |
+
|
121 |
+
data = Question(date, texte)
|
122 |
+
db.session.add(data)
|
123 |
+
db.session.commit()
|
124 |
+
|
125 |
+
return jsonify({
|
126 |
+
'status': 'Data is posted to PostgreSQL!',
|
127 |
+
'date': date,
|
128 |
+
'texte': texte
|
129 |
+
})
|
130 |
+
|
131 |
+
# GET all data from database & sort by id
|
132 |
+
if request.method == 'GET':
|
133 |
+
# data = User.query.all()
|
134 |
+
data = Question.query.all()
|
135 |
+
print(data)
|
136 |
+
dataJson = []
|
137 |
+
for i in range(len(data)):
|
138 |
+
# print(str(data[i]).split('/'))
|
139 |
+
dataDict = {
|
140 |
+
'id': str(data[i]).split('/')[0],
|
141 |
+
'date': str(data[i]).split('/')[1],
|
142 |
+
'texte': str(data[i]).split('/')[2]
|
143 |
+
}
|
144 |
+
dataJson.append(dataDict)
|
145 |
+
return jsonify(dataJson)
|
146 |
+
|
147 |
+
@app.route('/questions/<string:id>', methods=['GET', 'DELETE', 'PUT'])
|
148 |
+
def onedata(id):
|
149 |
+
|
150 |
+
# GET a specific data by id
|
151 |
+
if request.method == 'GET':
|
152 |
+
data = Question.query.get(id)
|
153 |
+
print(data)
|
154 |
+
dataDict = {
|
155 |
+
'id': str(data).split('/')[0],
|
156 |
+
'date': str(data).split('/')[1],
|
157 |
+
'texte': str(data).split('/')[2]
|
158 |
+
}
|
159 |
+
return jsonify(dataDict)
|
160 |
+
|
161 |
+
# DELETE a data
|
162 |
+
if request.method == 'DELETE':
|
163 |
+
delData = Question.query.filter_by(id=id).first()
|
164 |
+
db.session.delete(delData)
|
165 |
+
db.session.commit()
|
166 |
+
return jsonify({'status': 'Data '+id+' is deleted from PostgreSQL!'})
|
167 |
+
|
168 |
+
# UPDATE a data by id
|
169 |
+
if request.method == 'PUT':
|
170 |
+
body = request.json
|
171 |
+
newDate = body['date']
|
172 |
+
newTexte = body['texte']
|
173 |
+
editData = Question.query.filter_by(id=id).first()
|
174 |
+
editData.date = newDate
|
175 |
+
editData.texte = newTexte
|
176 |
+
db.session.commit()
|
177 |
+
return jsonify({'status': 'Data '+id+' is updated from PostgreSQL!'})
|
178 |
|
179 |
@app.route('/chat', methods=['OPTIONS'])
|
180 |
def options():
|
|
|
202 |
max_tokens=500,
|
203 |
temperature=0
|
204 |
)
|
205 |
+
date = time.ctime(time.time())
|
206 |
+
texte = prompt
|
207 |
+
data = Question(date, texte)
|
208 |
+
db.session.add(data)
|
209 |
+
db.session.commit()
|
210 |
+
question_id = data.id
|
211 |
resulta = response.choices[0].text
|
212 |
chat_references = filtergpt(resulta)
|
213 |
for law, article in chat_references:
|
|
|
216 |
for collection_name in collection_names:
|
217 |
search_results = perform_search_and_get_results(collection_name, prompt)
|
218 |
results.extend(search_results)
|
219 |
+
return jsonify({'question': {'id': question_id, 'date': date, 'texte': texte},'result_qdrant':results})
|
220 |
else:
|
221 |
return jsonify({'error': 'Invalid request'}), 400
|
222 |
except Exception as e:
|
223 |
return jsonify({'error': str(e)}), 500
|
224 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
225 |
if __name__ == '__main__':
|
226 |
+
app.debug = True
|
227 |
+
app.run()
|