-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
161 lines (142 loc) · 4.65 KB
/
app.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
from flask import Flask, request, jsonify
from elasticsearch import Elasticsearch,helpers
from openai import OpenAI
import threading
from urllib.request import urlopen
import json
import os
import numpy as np
from gtts import gTTS
from scipy.stats import norm
from urllib.request import urlopen
from flask_cors import CORS
# Initialize Flask app
import requests
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("all-MiniLM-L6-v2")
app = Flask(__name__)
CORS(app)
# Set up OpenAI and Elasticsearch clients
API_KEY = <YOUR_OPENAI_API>
ELASTIC_CLOUD_ID = <YOUR_cLOUD_API>
ELASTIC_API_KEY = <YOUR_ELASTIC_API>
client = Elasticsearch(
cloud_id = ELASTIC_CLOUD_ID ,
api_key = ELASTIC_API_KEY
)
openai_client = OpenAI(
api_key=API_KEY,
)
# url ="https://raw.githubusercontent.com/linuxacademy/content-elasticsearch-deep-dive/refs/heads/master/sample_data/shakespeare.json"
# Ensure the folder for saving MP3 files exists
MP3_FOLDER = "mp3_files"
if not os.path.exists(MP3_FOLDER):
os.makedirs(MP3_FOLDER)
#THIS SECTION OF THE CODE NEEDS TO BE RUN THE FIRST TIME , COMMENT AFTER SECOND RUN - IT TAKES A WHILE..TO PERFORM INDEXING
# response = requests.get(url)
# if response.status_code == 200:
# data = response.text.splitlines()
# else:
# print("FAILURE!!")
# openai_client = OpenAI(api_key=API_KEY)
# client = Elasticsearch(
# cloud_id=ELASTIC_CLOUD_ID,
# api_key=ELASTIC_API_KEY,
# )
# mapping = {
# "mappings": {
# "properties": {
# "type": {
# "type": "keyword"
# },
# "line_id": {
# "type": "integer"
# },
# "play_name": {
# "type": "text"
# },
# "speech_number": {
# "type": "integer"
# },
# "line_number": {
# "type": "text"
# },
# "speaker": {
# "type": "text"
# },
# "text_entry": {
# "type": "text"
# },
# "embedding": {
# "type": "dense_vector",
# "dims": 384
# }
# }
# }
# }
# # Assuming `data` is a list of lines read from the Shakespeare JSON file
# bulk_data = []
# for i in range(0, len(data), 2): # Process two lines at a time
# metadata = json.loads(data[i]) # Parse the metadata line
# record = json.loads(data[i + 1]) # Parse the record line
# # Generate embedding if `text_entry` exists
# if "text_entry" in record and record["text_entry"]:
# embedding = model.encode(record["text_entry"]).tolist()
# record['embedding'] = embedding
# # Add to bulk_data
# bulk_data.append({
# "_op_type": "index",
# "_index": metadata["index"]["_index"],
# "_id": metadata["index"]["_id"],
# **record
# })
# helpers.bulk(client, bulk_data)
# Flask route for handling queries
@app.route('/ask', methods=['POST'])
def get_query():
QUERY = request.json
print('THE QUERY IS:',QUERY)
# Perform KNN search (dummy example since exact search is missing)
# Replace this part with the actual Elasticsearch query
query_vector = model.encode([QUERY['message']]).tolist()[0]
response = client.search(
index="shakespeare",
query={
"knn": {
"field": "embedding",
"query_vector": query_vector,
"k": 1,
"num_candidates": 10
}
}
)
if not response["hits"]["hits"]:
return jsonify({"error": "No results found"}), 404
for hit in response['hits']['hits']:
print(f"Passing to GPT: Score: {hit['_score']}, Text: {hit['_source']['text_entry']}")
# Create the GPT prompt
gpt_prompt = f"""
I have retrieved a line from a Shakespeare database based on a KNN search with the query '{QUERY}'.
Here is the closest result:
Score: {hit['_score']}
Text: {hit['_source']['text_entry']}
Please provide an analysis or interpretation of the retrieved line in the context of Shakespeare's works, focusing on themes and language style.
"""
text_entry = hit['_source']['text_entry']
response = openai_client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are a specialized text extraction tool."},
{"role": "user", "content": gpt_prompt},
]
)
response_text = response.choices[0].message.content.strip()
tts = gTTS(response_text, lang='en', slow=False)
audio_path = os.path.join(MP3_FOLDER, "trail.mp3")
tts.save(audio_path)
return jsonify({
"response": response_text,
"audio_file": audio_path
})
if __name__ == "__main__":
app.run(debug=True)