Spaces:
Runtime error
Runtime error
Commit
•
8094526
1
Parent(s):
ef4e733
Upload 5 files
Browse files- Dockerfile +14 -0
- main.py +120 -0
- requirements.txt +6 -0
Dockerfile
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
|
2 |
+
# you will also find guides on how best to write your Dockerfile
|
3 |
+
|
4 |
+
FROM python:3.9
|
5 |
+
|
6 |
+
WORKDIR /code
|
7 |
+
|
8 |
+
COPY ./requirements.txt /code/requirements.txt
|
9 |
+
|
10 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
11 |
+
|
12 |
+
COPY . .
|
13 |
+
|
14 |
+
CMD ["gunicorn", "-b", "0.0.0.0:7860", "main:app"]
|
main.py
ADDED
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, jsonify, request, make_response
|
2 |
+
from qdrant_client.http import models
|
3 |
+
from flask_cors import CORS
|
4 |
+
import openai
|
5 |
+
import qdrant_client
|
6 |
+
from sentence_transformers import SentenceTransformer
|
7 |
+
|
8 |
+
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 ...'
|
9 |
+
|
10 |
+
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: '
|
11 |
+
question = ''
|
12 |
+
|
13 |
+
|
14 |
+
app = Flask(__name__)
|
15 |
+
CORS(app, origins='*')
|
16 |
+
openai.api_key = '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'
|
17 |
+
|
18 |
+
|
19 |
+
|
20 |
+
|
21 |
+
client = qdrant_client.QdrantClient(
|
22 |
+
"https://efc68112-69cc-475c-bdcb-200a019b5096.us-east4-0.gcp.cloud.qdrant.io:6333",
|
23 |
+
api_key="ZQ6jySuPxY5rSh0mJ4jDMoxbZsPqDdbqFBOPwotl9B8N0Ru3S8bzoQ"
|
24 |
+
)
|
25 |
+
|
26 |
+
model = SentenceTransformer('sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2') # good so far
|
27 |
+
collection_names = ["new_lir"] # replace with the collection name used on Qdrant
|
28 |
+
|
29 |
+
import re
|
30 |
+
|
31 |
+
def filtergpt(text):
|
32 |
+
# Define a regular expression pattern to extract law and article number
|
33 |
+
pattern = re.compile(r"Loi ([^,]+), article (\d+(\.\d+)?)")
|
34 |
+
# Find all matches in the text
|
35 |
+
matches = pattern.findall(text)
|
36 |
+
# Create a list of tuples containing law and article number
|
37 |
+
law_article_list = [(law.strip(), float(article.strip())) for law, article, _ in matches]
|
38 |
+
gpt_results = [(law, str(int(article)) if article.is_integer() else str(article)) for law, article in law_article_list]
|
39 |
+
return gpt_results
|
40 |
+
|
41 |
+
|
42 |
+
@app.route('/chat', methods=['OPTIONS'])
|
43 |
+
def options():
|
44 |
+
response = make_response()
|
45 |
+
response.headers.add("Access-Control-Allow-Origin", "http://localhost:3000")
|
46 |
+
response.headers.add("Access-Control-Allow-Methods", "POST")
|
47 |
+
response.headers.add("Access-Control-Allow-Headers", "Content-Type, Authorization")
|
48 |
+
response.headers.add("Access-Control-Allow-Credentials", "true")
|
49 |
+
return response
|
50 |
+
|
51 |
+
@app.route('/chat', methods=['POST'])
|
52 |
+
def chat():
|
53 |
+
try:
|
54 |
+
data = request.get_json()
|
55 |
+
messages = data.get('messages', [])
|
56 |
+
|
57 |
+
if messages:
|
58 |
+
results = []
|
59 |
+
# Update the model name to "text-davinci-003" (Ada)
|
60 |
+
prompt = "\n".join([f"{msg['role']}: {msg['content']}" for msg in messages])
|
61 |
+
response = openai.completions.create(
|
62 |
+
model="gpt-3.5-turbo-instruct",
|
63 |
+
prompt=start_message +'\n'+ context + question ,
|
64 |
+
max_tokens=500,
|
65 |
+
temperature=0
|
66 |
+
)
|
67 |
+
resulta = response.choices[0].text
|
68 |
+
chat_references = filtergpt(resulta)
|
69 |
+
for law, article in chat_references:
|
70 |
+
search_results = perform_search_and_get_results_with_filter(collection_names[0], prompt, reference_filter=article)
|
71 |
+
results.extend(search_results)
|
72 |
+
for collection_name in collection_names:
|
73 |
+
search_results = perform_search_and_get_results(collection_name, prompt)
|
74 |
+
results.extend(search_results)
|
75 |
+
return jsonify({'result_qdrant':results})
|
76 |
+
else:
|
77 |
+
return jsonify({'error': 'Invalid request'}), 400
|
78 |
+
except Exception as e:
|
79 |
+
return jsonify({'error': str(e)}), 500
|
80 |
+
|
81 |
+
def perform_search_and_get_results(collection_name, query, limit=6):
|
82 |
+
search_results = client.search(
|
83 |
+
collection_name=collection_name,
|
84 |
+
query_vector=model.encode(query).tolist(),
|
85 |
+
limit=limit
|
86 |
+
)
|
87 |
+
resultes = []
|
88 |
+
for result in search_results:
|
89 |
+
result_dict = {
|
90 |
+
"Score": result.score,
|
91 |
+
"La_loi": result.payload["reference"],
|
92 |
+
"Paragraphe": result.payload["paragraph"],
|
93 |
+
"source": result.payload["source"],
|
94 |
+
"collection": collection_name
|
95 |
+
}
|
96 |
+
resultes.append(result_dict)
|
97 |
+
return resultes
|
98 |
+
|
99 |
+
def perform_search_and_get_results_with_filter(collection_name, query,reference_filter , limit=6):
|
100 |
+
search_results = client.search(
|
101 |
+
collection_name=collection_name,
|
102 |
+
query_filter=models.Filter(must=[models.FieldCondition(key="numero_article",match=models.MatchValue(value=reference_filter+"aymane",),)]),
|
103 |
+
query_vector=model.encode(query).tolist(),
|
104 |
+
limit=1
|
105 |
+
)
|
106 |
+
resultes = []
|
107 |
+
for result in search_results:
|
108 |
+
result_dict = {
|
109 |
+
"Score": result.score,
|
110 |
+
"La_loi": result.payload["reference"],
|
111 |
+
"Paragraphe": result.payload["paragraph"],
|
112 |
+
"source": result.payload["source"],
|
113 |
+
"collection": collection_name
|
114 |
+
}
|
115 |
+
resultes.append(result_dict)
|
116 |
+
return resultes
|
117 |
+
|
118 |
+
if __name__ == '__main__':
|
119 |
+
app.run(debug=True, port=5001)
|
120 |
+
import qdrant_client
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Flask
|
2 |
+
Flask-CORS
|
3 |
+
openai
|
4 |
+
qdrant-client
|
5 |
+
sentence-transformers
|
6 |
+
gunicorn
|