Spaces:
Running
Running
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: | |
return self._call_hf_api(input) | |
def embed_query(self, input: Documents) -> Embeddings: | |
return self._call_hf_api([input]) | |
def _call_hf_api(self, inputs): | |
url = "https://api-inference.huggingface.co/models/BAAI/bge-large-en-v1.5" | |
headers = { | |
'accept': '*/*', | |
'content-type': 'application/json', | |
} | |
payload = {"inputs": inputs} | |
try: | |
response = requests.post(url, headers=headers, json=payload) | |
response.raise_for_status() | |
return response.json()[0] | |
except Exception as e: | |
print("Embedding API Error:", str(e)) | |
return [] | |
try: | |
CHROMA_PATH = "chroma" | |
custom_embeddings = MyEmbeddingFunction() | |
db = Chroma(persist_directory=CHROMA_PATH, embedding_function=custom_embeddings) | |
except Exception as e: | |
print("Database Initialization Error:", 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"): | |
try: | |
conn = sqlite3.connect(db_path) | |
cursor = conn.cursor() | |
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, content, embedding_json = row | |
embedding = json.loads(embedding_json) | |
collection.add(ids=[doc_id], documents=[content], embeddings=[embedding]) | |
conn.close() | |
print("Documents loaded into Chroma.") | |
except Exception as e: | |
print("Error loading documents:", str(e)) | |
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" | |
headers = {'accept': '*/*', 'content-type': 'application/json'} | |
payload = {"inputs": [query]} | |
try: | |
response = requests.post(url, headers=headers, json=payload) | |
response.raise_for_status() | |
return response.json()[0] | |
except Exception as e: | |
print("Embedding Generation Error:", str(e)) | |
return [] | |
def strings_ranked_by_relatedness(query, df, top_n=5): | |
def cosine_similarity(x, y): | |
return np.dot(x, y) / (np.linalg.norm(x) * np.linalg.norm(y)) | |
query_embedding = embeddingGen(query) | |
ranked = sorted( | |
[(row["text"], cosine_similarity(query_embedding, row["embedding"])) for row in df], | |
key=lambda x: x[1], | |
reverse=True | |
) | |
strings, scores = zip(*ranked) | |
return strings[:top_n], scores[:top_n] | |
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/Llama-4-Maverick-17B-128E-Instruct-FP8", | |
"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}) | |
def index(): | |
return render_template("index.html") | |
def getAPI(): | |
return jsonify({"API": random.choice(apiKeys)}) | |
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"]}) | |
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)}) | |
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) |