Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -1,232 +1,229 @@
|
|
1 |
-
from flask import Flask, request, jsonify, render_template, Response
|
2 |
-
import os
|
3 |
-
import requests
|
4 |
-
import json
|
5 |
-
from scipy import spatial
|
6 |
-
from flask_cors import CORS
|
7 |
-
import random
|
8 |
-
import numpy as np
|
9 |
-
from langchain_chroma import Chroma
|
10 |
-
from chromadb import Documents, EmbeddingFunction, Embeddings
|
11 |
-
|
12 |
-
|
13 |
-
app
|
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 |
-
CHROMA_PATH =
|
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 |
-
strings_and_relatednesses
|
122 |
-
|
123 |
-
]
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
def
|
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 |
-
'
|
209 |
-
'
|
210 |
-
'
|
211 |
-
'
|
212 |
-
'
|
213 |
-
'
|
214 |
-
'sec-
|
215 |
-
'sec-
|
216 |
-
'sec-
|
217 |
-
'
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
from waitress import serve
|
231 |
-
|
232 |
-
serve(app, host="0.0.0.0", port=7860)
|
|
|
1 |
+
from flask import Flask, request, jsonify, render_template, Response
|
2 |
+
import os
|
3 |
+
import requests
|
4 |
+
import json
|
5 |
+
from scipy import spatial
|
6 |
+
from flask_cors import CORS
|
7 |
+
import random
|
8 |
+
import numpy as np
|
9 |
+
from langchain_chroma import Chroma
|
10 |
+
from chromadb import Documents, EmbeddingFunction, Embeddings
|
11 |
+
|
12 |
+
app = Flask(__name__)
|
13 |
+
CORS(app)
|
14 |
+
|
15 |
+
class MyEmbeddingFunction(EmbeddingFunction):
|
16 |
+
def embed_documents(self, input: Documents) -> Embeddings:
|
17 |
+
for i in range(5):
|
18 |
+
try:
|
19 |
+
embeddings = []
|
20 |
+
url = "https://api.deepinfra.com/v1/inference/BAAI/bge-large-en-v1.5"
|
21 |
+
|
22 |
+
payload = json.dumps({
|
23 |
+
"inputs": input
|
24 |
+
})
|
25 |
+
headers = {
|
26 |
+
'Accept': 'application/json, text/plain, */*',
|
27 |
+
'Accept-Language': 'en-US,en;q=0.9,gu;q=0.8,ru;q=0.7,hi;q=0.6',
|
28 |
+
'Connection': 'keep-alive',
|
29 |
+
'Content-Type': 'application/json',
|
30 |
+
'Origin': 'https://deepinfra.com',
|
31 |
+
'Referer': 'https://deepinfra.com/',
|
32 |
+
'Sec-Fetch-Dest': 'empty',
|
33 |
+
'Sec-Fetch-Mode': 'cors',
|
34 |
+
'Sec-Fetch-Site': 'same-site',
|
35 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36',
|
36 |
+
'sec-ch-ua': '"Not)A;Brand";v="99", "Google Chrome";v="127", "Chromium";v="127"',
|
37 |
+
'sec-ch-ua-mobile': '?0',
|
38 |
+
'sec-ch-ua-platform': '"Windows"'
|
39 |
+
}
|
40 |
+
|
41 |
+
response = requests.request("POST", url, headers=headers, data=payload)
|
42 |
+
return response.json()["embeddings"]
|
43 |
+
except:
|
44 |
+
pass
|
45 |
+
|
46 |
+
def embed_query(self, input: Documents) -> Embeddings:
|
47 |
+
print(input)
|
48 |
+
for i in range(5):
|
49 |
+
try:
|
50 |
+
embeddings = []
|
51 |
+
url = "https://api.deepinfra.com/v1/inference/BAAI/bge-large-en-v1.5"
|
52 |
+
|
53 |
+
payload = json.dumps({
|
54 |
+
"inputs": [input]
|
55 |
+
})
|
56 |
+
headers = {
|
57 |
+
'Accept': 'application/json, text/plain, */*',
|
58 |
+
'Accept-Language': 'en-US,en;q=0.9,gu;q=0.8,ru;q=0.7,hi;q=0.6',
|
59 |
+
'Connection': 'keep-alive',
|
60 |
+
'Content-Type': 'application/json',
|
61 |
+
'Origin': 'https://deepinfra.com',
|
62 |
+
'Referer': 'https://deepinfra.com/',
|
63 |
+
'Sec-Fetch-Dest': 'empty',
|
64 |
+
'Sec-Fetch-Mode': 'cors',
|
65 |
+
'Sec-Fetch-Site': 'same-site',
|
66 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36',
|
67 |
+
'sec-ch-ua': '"Not)A;Brand";v="99", "Google Chrome";v="127", "Chromium";v="127"',
|
68 |
+
'sec-ch-ua-mobile': '?0',
|
69 |
+
'sec-ch-ua-platform': '"Windows"'
|
70 |
+
}
|
71 |
+
|
72 |
+
response = requests.request("POST", url, headers=headers, data=payload)
|
73 |
+
return response.json()["embeddings"][0]
|
74 |
+
except:
|
75 |
+
pass
|
76 |
+
|
77 |
+
CHROMA_PATH = "chroma"
|
78 |
+
custom_embeddings = MyEmbeddingFunction()
|
79 |
+
db = Chroma(
|
80 |
+
persist_directory=CHROMA_PATH, embedding_function=custom_embeddings
|
81 |
+
)
|
82 |
+
|
83 |
+
|
84 |
+
def embeddingGen(query):
|
85 |
+
url = "https://api.deepinfra.com/v1/inference/BAAI/bge-large-en-v1.5"
|
86 |
+
|
87 |
+
payload = json.dumps({
|
88 |
+
"inputs": [query]
|
89 |
+
})
|
90 |
+
headers = {
|
91 |
+
'Accept': 'application/json, text/plain, */*',
|
92 |
+
'Accept-Language': 'en-US,en;q=0.9,gu;q=0.8,ru;q=0.7,hi;q=0.6',
|
93 |
+
'Connection': 'keep-alive',
|
94 |
+
'Content-Type': 'application/json',
|
95 |
+
'Origin': 'https://deepinfra.com',
|
96 |
+
'Referer': 'https://deepinfra.com/',
|
97 |
+
'Sec-Fetch-Dest': 'empty',
|
98 |
+
'Sec-Fetch-Mode': 'cors',
|
99 |
+
'Sec-Fetch-Site': 'same-site',
|
100 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36',
|
101 |
+
'sec-ch-ua': '"Not)A;Brand";v="99", "Google Chrome";v="127", "Chromium";v="127"',
|
102 |
+
'sec-ch-ua-mobile': '?0',
|
103 |
+
'sec-ch-ua-platform': '"Windows"'
|
104 |
+
}
|
105 |
+
|
106 |
+
response = requests.request("POST", url, headers=headers, data=payload)
|
107 |
+
return response.json()
|
108 |
+
|
109 |
+
|
110 |
+
def strings_ranked_by_relatedness(query, df, top_n=5):
|
111 |
+
def relatedness_fn(x, y):
|
112 |
+
x_norm = np.linalg.norm(x)
|
113 |
+
y_norm = np.linalg.norm(y)
|
114 |
+
return np.dot(x, y) / (x_norm * y_norm)
|
115 |
+
|
116 |
+
query_embedding_response = embeddingGen(query)
|
117 |
+
query_embedding = query_embedding_response["embeddings"][0]
|
118 |
+
strings_and_relatednesses = [
|
119 |
+
(row["text"], relatedness_fn(query_embedding, row["embedding"])) for row in df
|
120 |
+
]
|
121 |
+
strings_and_relatednesses.sort(key=lambda x: x[1], reverse=True)
|
122 |
+
strings, relatednesses = zip(*strings_and_relatednesses)
|
123 |
+
return strings[:top_n], relatednesses[:top_n]
|
124 |
+
|
125 |
+
|
126 |
+
@app.route("/api/gpt", methods=["POST"])
|
127 |
+
def gptRes():
|
128 |
+
data = request.get_json()
|
129 |
+
messages = data["messages"]
|
130 |
+
def inference():
|
131 |
+
url = "https://api.deepinfra.com/v1/openai/chat/completions"
|
132 |
+
|
133 |
+
payload = json.dumps({
|
134 |
+
"model": "meta-llama/Meta-Llama-3.1-8B-Instruct",
|
135 |
+
"messages": messages,
|
136 |
+
"stream": True,
|
137 |
+
"max_tokens": 1024,
|
138 |
+
})
|
139 |
+
headers = {
|
140 |
+
'Accept-Language': 'en-US,en;q=0.9,gu;q=0.8,ru;q=0.7,hi;q=0.6',
|
141 |
+
'Connection': 'keep-alive',
|
142 |
+
'Content-Type': 'application/json',
|
143 |
+
'Origin': 'https://deepinfra.com',
|
144 |
+
'Referer': 'https://deepinfra.com/',
|
145 |
+
'Sec-Fetch-Dest': 'empty',
|
146 |
+
'Sec-Fetch-Mode': 'cors',
|
147 |
+
'Sec-Fetch-Site': 'same-site',
|
148 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36',
|
149 |
+
'X-Deepinfra-Source': 'web-page',
|
150 |
+
'accept': 'text/event-stream',
|
151 |
+
'sec-ch-ua': '"Not)A;Brand";v="99", "Google Chrome";v="127", "Chromium";v="127"',
|
152 |
+
'sec-ch-ua-mobile': '?0',
|
153 |
+
'sec-ch-ua-platform': '"Windows"'
|
154 |
+
}
|
155 |
+
|
156 |
+
response = requests.request("POST", url, headers=headers, data=payload, stream=True)
|
157 |
+
|
158 |
+
for line in response.iter_lines(decode_unicode=True):
|
159 |
+
if line:
|
160 |
+
# try:
|
161 |
+
# line = line.split("data:")[1]
|
162 |
+
# line = json.loads(line)
|
163 |
+
# yield line["choices"][0]["delta"]["content"]
|
164 |
+
# except:
|
165 |
+
# yield ""
|
166 |
+
yield line
|
167 |
+
|
168 |
+
return Response(inference(), content_type='text/event-stream')
|
169 |
+
|
170 |
+
|
171 |
+
@app.route("/", methods=["GET"])
|
172 |
+
def index():
|
173 |
+
return render_template("index.html")
|
174 |
+
|
175 |
+
|
176 |
+
@app.route("/api/getAPI", methods=["POST"])
|
177 |
+
def getAPI():
|
178 |
+
return jsonify({"API": random.choice(apiKeys)})
|
179 |
+
|
180 |
+
|
181 |
+
@app.route("/api/getContext", methods=["POST"])
|
182 |
+
def getContext():
|
183 |
+
global db
|
184 |
+
question = request.form["question"]
|
185 |
+
results = db.similarity_search_with_score(question, k=5)
|
186 |
+
context = "\n\n---\n\n".join([doc.page_content for doc, _score in results])
|
187 |
+
sources = [doc.metadata.get("id", None) for doc, _score in results]
|
188 |
+
return jsonify({"context": context, "sources": sources})
|
189 |
+
|
190 |
+
|
191 |
+
@app.route("/api/audioGenerate", methods=["POST"])
|
192 |
+
def audioGenerate():
|
193 |
+
answer = request.form["answer"]
|
194 |
+
audio = []
|
195 |
+
for i in answer.split("\n"):
|
196 |
+
url = "https://deepgram.com/api/ttsAudioGeneration"
|
197 |
+
|
198 |
+
payload = json.dumps({
|
199 |
+
"text": i,
|
200 |
+
"model": "aura-asteria-en",
|
201 |
+
"demoType": "landing-page",
|
202 |
+
"params": "tag=landingpage-product-texttospeech"
|
203 |
+
})
|
204 |
+
headers = {
|
205 |
+
'accept': '*/*',
|
206 |
+
'accept-language': 'en-US,en;q=0.9,gu;q=0.8,ru;q=0.7,hi;q=0.6',
|
207 |
+
'content-type': 'application/json',
|
208 |
+
'origin': 'https://deepgram.com',
|
209 |
+
'priority': 'u=1, i',
|
210 |
+
'referer': 'https://deepgram.com/',
|
211 |
+
'sec-ch-ua': '"Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"',
|
212 |
+
'sec-ch-ua-mobile': '?0',
|
213 |
+
'sec-ch-ua-platform': '"Windows"',
|
214 |
+
'sec-fetch-dest': 'empty',
|
215 |
+
'sec-fetch-mode': 'cors',
|
216 |
+
'sec-fetch-site': 'same-origin',
|
217 |
+
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36'
|
218 |
+
}
|
219 |
+
|
220 |
+
response = requests.request("POST", url, headers=headers, data=payload)
|
221 |
+
audio.append(response.json()["data"])
|
222 |
+
return jsonify({"audio": audio})
|
223 |
+
|
224 |
+
|
225 |
+
if __name__ == "__main__":
|
226 |
+
# app.run(debug=True)
|
227 |
+
from waitress import serve
|
228 |
+
|
229 |
+
serve(app, host="0.0.0.0", port=7860)
|
|
|
|
|
|