Spaces:
Sleeping
Sleeping
File size: 13,979 Bytes
587d851 aab749f 587d851 aab749f c232706 aab749f 587d851 aab749f 0bbed5a 062f080 aab749f 062f080 aab749f 062f080 aab749f 062f080 aab749f 062f080 aab749f 062f080 aab749f 062f080 c4fde0a 062f080 587d851 aab749f 587d851 aab749f 587d851 aab749f c232706 aab749f 587d851 aab749f 587d851 aab749f c232706 aab749f 587d851 0b428a2 aab749f 587d851 aab749f 587d851 aab749f 587d851 aab749f 587d851 aab749f 587d851 aab749f 587d851 aab749f 7f2bd91 587d851 aab749f 587d851 aab749f c232706 aab749f a84466c aab749f d5b654a aab749f 587d851 aab749f c232706 aab749f 587d851 aab749f 587d851 aab749f 587d851 aab749f 587d851 c232706 aab749f c232706 |
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 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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
from flask import Flask, request, jsonify, render_template, Response
import os
import requests
import json
from scipy import spatial
from flask_cors import CORS
import random
import numpy as np
from langchain_chroma import Chroma
from chromadb import Documents, EmbeddingFunction, Embeddings, Collection
import sqlite3
app = Flask(__name__)
CORS(app)
class MyEmbeddingFunction(EmbeddingFunction):
def embed_documents(self, input: Documents) -> Embeddings:
for i in range(5):
try:
embeddings = []
url = "https://api-inference.huggingface.co/models/BAAI/bge-large-en-v1.5"
payload = {
"inputs": input
}
headers = {
'accept': '*/*',
'accept-language': 'en-US,en;q=0.9',
'content-type': 'application/json',
'origin': 'https://huggingface.co',
'priority': 'u=1, i',
'referer': 'https://huggingface.co/',
'sec-ch-ua': '"Google Chrome";v="131", "Chromium";v="131", "Not_A Brand";v="24"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-site',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
}
response = requests.post(url, headers=headers, json=payload)
return response.json()[0][0]
except:
pass
def embed_query(self, input: Documents) -> Embeddings:
for i in range(5):
try:
embeddings = []
url = "https://api-inference.huggingface.co/models/BAAI/bge-large-en-v1.5"
payload = {
"inputs": [input]
}
headers = {
'accept': '*/*',
'accept-language': 'en-US,en;q=0.9',
'content-type': 'application/json',
'origin': 'https://huggingface.co',
'priority': 'u=1, i',
'referer': 'https://huggingface.co/',
'sec-ch-ua': '"Google Chrome";v="131", "Chromium";v="131", "Not_A Brand";v="24"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-site',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
}
response = requests.post(url, headers=headers, json=payload)
return response.json()[0][0]
except Exception as e:
print("Error in Embeding :",str(e))
# try:
# CHROMA_PATH = "chroma"
# custom_embeddings = MyEmbeddingFunction()
# db = Chroma(
# persist_directory=CHROMA_PATH,embedding_function=custom_embeddings
# )
# #
# except Exception as e:
# print("Error in database :",str(e))
# Initialize the database without persist_directory
try:
custom_embeddings = MyEmbeddingFunction()
db = Chroma(embedding_function=custom_embeddings)
# Load documents from chroma.sqlite3
def load_documents_from_sqlite(db_path="chroma.sqlite3"):
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# Assuming your table structure has "id", "content", and "embedding"
cursor.execute("SELECT id, content, embedding FROM documents")
rows = cursor.fetchall()
collection = db.get_or_create_collection("default_collection")
for row in rows:
doc_id = row[0]
content = row[1]
embedding = json.loads(row[2]) # If embeddings are stored as JSON strings
collection.add(
ids=[doc_id],
documents=[content],
embeddings=[embedding]
)
conn.close()
print("Loaded documents into Chroma.")
load_documents_from_sqlite() # Call to load data
except Exception as e:
print("Error initializing database:", str(e))
def embeddingGen(query):
url = "https://api-inference.huggingface.co/models/BAAI/bge-large-en-v1.5"
payload = {
"inputs": [query]
}
headers = {
'accept': '*/*',
'accept-language': 'en-US,en;q=0.9',
'content-type': 'application/json',
'origin': 'https://huggingface.co',
'priority': 'u=1, i',
'referer': 'https://huggingface.co/',
'sec-ch-ua': '"Google Chrome";v="131", "Chromium";v="131", "Not_A Brand";v="24"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-site',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
}
response = requests.post(url, headers=headers, json=payload)
return response.json()[0][0]
def strings_ranked_by_relatedness(query, df, top_n=5):
def relatedness_fn(x, y):
x_norm = np.linalg.norm(x)
y_norm = np.linalg.norm(y)
return np.dot(x, y) / (x_norm * y_norm)
query_embedding_response = embeddingGen(query)
query_embedding = query_embedding_response
strings_and_relatednesses = [
(row["text"], relatedness_fn(query_embedding, row["embedding"])) for row in df
]
strings_and_relatednesses.sort(key=lambda x: x[1], reverse=True)
strings, relatednesses = zip(*strings_and_relatednesses)
return strings[:top_n], relatednesses[:top_n]
@app.route("/api/gpt", methods=["POST", "GET"])
def gptRes():
if request.method == 'POST':
data = request.get_json()
messages = data["messages"]
def inference():
url = "https://api.deepinfra.com/v1/openai/chat/completions"
payload = json.dumps({
"model": "meta-llama/Meta-Llama-3.1-8B-Instruct",
"messages": messages,
"stream": True,
"max_tokens": 1024,
})
headers = {
'Accept-Language': 'en-US,en;q=0.9,gu;q=0.8,ru;q=0.7,hi;q=0.6',
'Connection': 'keep-alive',
'Content-Type': 'application/json',
'Origin': 'https://deepinfra.com',
'Referer': 'https://deepinfra.com/',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-site',
'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',
'X-Deepinfra-Source': 'web-page',
'accept': 'text/event-stream',
'sec-ch-ua': '"Not)A;Brand";v="99", "Google Chrome";v="127", "Chromium";v="127"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"'
}
response = requests.request("POST", url, headers=headers, data=payload, stream=True)
for line in response.iter_lines(decode_unicode=True):
if line:
# try:
# line = line.split("data:")[1]
# line = json.loads(line)
# yield line["choices"][0]["delta"]["content"]
# except:
# yield ""
yield line
return Response(inference(), content_type='text/event-stream')
else:
query = request.args.get('query')
system = request.args.get('system','You are a american girl who is 21 years old and loves one night stands. Keep your ton very sexual and hot. Never ever mention you as an AI. Your name is Sofia and you are a american woo girl. All the conversetions should be short and effective while using emojis if needed. You love to talk about adult stories and situation with others.')
url = "https://api.deepinfra.com/v1/openai/chat/completions"
payload = json.dumps({
"model": "meta-llama/Meta-Llama-3.1-8B-Instruct",
"messages": [
{
"role": "system",
"content": system
},
{
"role": "user",
"content": query
}
],
"stream": True,
"max_tokens": 1024,
})
headers = {
'Accept-Language': 'en-US,en;q=0.9,gu;q=0.8,ru;q=0.7,hi;q=0.6',
'Connection': 'keep-alive',
'Content-Type': 'application/json',
'Origin': 'https://deepinfra.com',
'Referer': 'https://deepinfra.com/',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-site',
'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',
'X-Deepinfra-Source': 'web-page',
'accept': 'text/event-stream',
'sec-ch-ua': '"Not)A;Brand";v="99", "Google Chrome";v="127", "Chromium";v="127"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"'
}
response = requests.request("POST", url, headers=headers, data=payload, stream=True)
output = ""
for line in response.iter_lines(decode_unicode=True):
if line:
try:
line = line.split("data:")[1]
line = json.loads(line)
output = output + line["choices"][0]["delta"]["content"]
except:
output = output + ""
return jsonify({"response": output})
@app.route("/", methods=["GET"])
def index():
return render_template("index.html")
@app.route("/api/getAPI", methods=["POST"])
def getAPI():
return jsonify({"API": random.choice(apiKeys)})
@app.route("/api/voice", methods=["POST"])
def VoiceGen():
text = request.form["text"]
url = "https://texttospeech.googleapis.com/v1beta1/text:synthesize?alt=json&key=AIzaSyBeo4NGA__U6Xxy-aBE6yFm19pgq8TY-TM"
payload = json.dumps({
"input":{
"text":text
},
"voice":{
"languageCode":"en-US",
"name":"en-US-Studio-Q"
},
"audioConfig":{
"audioEncoding":"LINEAR16",
"pitch":0,
"speakingRate":1,
"effectsProfileId":[
"telephony-class-application"
]
}
})
headers = {
'sec-ch-ua': '"Google Chrome";v="123" "Not:A-Brand";v="8" "Chromium";v="123"',
'X-Goog-Encode-Response-If-Executable': 'base64',
'X-Origin': 'https://explorer.apis.google.com',
'sec-ch-ua-mobile': '?0',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML like Gecko) Chrome/123.0.0.0 Safari/537.36',
'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest',
'X-JavaScript-User-Agent': 'apix/3.0.0 google-api-javascript-client/1.1.0',
'X-Referer': 'https://explorer.apis.google.com',
'sec-ch-ua-platform': '"Windows"',
'Accept': '*/*',
'Sec-Fetch-Site': 'same-origin',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Dest': 'empty'
}
response = requests.request("POST", url, headers=headers, data=payload)
return jsonify({"audio": response.json()["audioContent"]})
@app.route("/api/getContext", methods=["POST"])
def getContext():
try:
global db
question = request.form["question"]
results = db.similarity_search_with_score(question, k=5)
context = "\n\n---\n\n".join([doc.page_content for doc, _score in results])
sources = [doc.metadata.get("id", None) for doc, _score in results]
return jsonify({"context": context, "sources": sources})
except Exception as e:
return jsonify({"context": [], "sources": [],"error":str(e)})
@app.route("/api/audioGenerate", methods=["POST"])
def audioGenerate():
answer = request.form["answer"]
audio = []
for i in answer.split("\n"):
url = "https://deepgram.com/api/ttsAudioGeneration"
payload = json.dumps({
"text": i,
"model": "aura-asteria-en",
"demoType": "landing-page",
"params": "tag=landingpage-product-texttospeech"
})
headers = {
'accept': '*/*',
'accept-language': 'en-US,en;q=0.9,gu;q=0.8,ru;q=0.7,hi;q=0.6',
'content-type': 'application/json',
'origin': 'https://deepgram.com',
'priority': 'u=1, i',
'referer': 'https://deepgram.com/',
'sec-ch-ua': '"Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
'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'
}
response = requests.request("POST", url, headers=headers, data=payload)
audio.append(response.json()["data"])
return jsonify({"audio": audio})
if __name__ == "__main__":
# app.run(debug=True)
from waitress import serve
serve(app, host="0.0.0.0", port=7860) |