This repository has been archived by the owner on Sep 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.py
137 lines (111 loc) · 3.83 KB
/
backend.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
import os
from flask import Flask, request, jsonify
from flask_cors import CORS
import openai
import torch
from transformers import AutoTokenizer, AutoModelForQuestionAnswering, pipeline
import traceback
from elasticsearch import Elasticsearch
from elasticsearch.helpers import bulk
app = Flask(__name__)
CORS(app)
# Set up GPT-3
openai.api_key = "write Openai api key here"
# Set up BERT
tokenizer = AutoTokenizer.from_pretrained("savasy/bert-base-turkish-squad")
model = AutoModelForQuestionAnswering.from_pretrained("savasy/bert-base-turkish-squad")
nlp = pipeline("question-answering", model=model, tokenizer=tokenizer)
# Connect to Elasticsearch
es = Elasticsearch(
hosts=['https://localhost:9200'],
http_auth=('elastic', 'write elastic password here'),
verify_certs=False
)
# Create index mapping
index_name = 'passages'
index_mapping = {
"mappings": {
"properties": {
"passage_id": {"type": "integer"},
"passage": {"type": "text", "analyzer": "turkish"},
}
}
}
# Create the index with the custom mapping if it doesn't exist
if not es.indices.exists(index=index_name):
es.indices.create(index=index_name, body=index_mapping)
# Index the passages
def index_passages():
with open("Datasets/passage.txt", "r", encoding="utf-8") as f:
passages = f.readlines()
bulk_data = []
for i, passage in enumerate(passages):
doc = {
'passage_id': i,
'passage': passage.strip()
}
bulk_data.append({
"_index": index_name,
"_id": i,
"_source": doc
})
bulk(es, bulk_data)
# Call the function to index the passages
index_passages()
@app.route("/api/status", methods=["GET"])
def get_status():
try:
response = openai.Completion.create(engine="davinci", prompt="test", max_tokens=5)
return jsonify({"status": "Online"})
except Exception as e:
return jsonify({"status": "Offline"})
@app.route("/generate-response-gpt3", methods=["POST"])
def get_gpt3_response():
message = request.json["message"]
print(message)
try:
response = openai.Completion.create(
model="write trained gpt3 model name here",
prompt=message,
temperature=0.0,
max_tokens=150,
top_p=1.0,
best_of=3,
frequency_penalty=0.0,
presence_penalty=0.0,
stop=['. ',"\n"]
)
return jsonify({"text": response.choices[0].text.strip()})
except Exception as e:
print(e)
return jsonify({"error": "Error generating response"}), 500
@app.route("/generate-response-bert", methods=["POST"])
def get_bert_response():
question = request.json["message"]
# Search for relevant passages using Elasticsearch
search_results = es.search(index=index_name, body={
'query': {
'match': {
'passage': {
'query': question,
'analyzer': 'turkish'
}
}
},
'size': 5 # Return top 5 passages
})
try:
passages = '\n'.join([hit['_source']['passage'] for hit in search_results['hits']['hits']])
if not passages:
return jsonify({'text': 'Üzgünüm ne dediğinizi anlayamadım. Tekrar sorunuzu sorabilir misiniz?'}), 200
results = nlp(question=question, context=passages, top_k=5)
result = max(results, key=lambda x: len(x['answer']))
if result is None:
return jsonify({'text': 'Üzgünüm ne dediğinizi anlayamadım. Tekrar sorunuzu sorabilir misiniz?'}), 200
else:
return jsonify({'text': result['answer']}), 200
except Exception as e:
print(traceback.format_exc())
return jsonify({"error": "Error generating response"}), 500
if __name__ == "__main__":
app.run(debug=True)