Spaces:
Sleeping
Sleeping
File size: 4,991 Bytes
577c504 7e18735 577c504 a3e1f01 577c504 2ec1106 577c504 d3e7da1 577c504 4919279 577c504 cbbb8f7 577c504 2ec1106 577c504 49f84d4 baf6bb6 49f84d4 4919279 baf6bb6 4919279 577c504 59f7b12 dd9923e 59f7b12 577c504 584f6f4 577c504 |
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 |
from flask import Flask, request, jsonify, render_template
import os
import requests
import json
from scipy import spatial
from flask_cors import CORS
import random
url = "https://gptlesson1.oss-cn-beijing.aliyuncs.com/apiKeys.json"
response = requests.get(url)
# apiKeys = response.json()
apiKeys = ["eyJhbGciOiJFUzM4NCJ9.eyJhdWQiOlsiaHR0cHM6Ly9vcGVuYWkuYWktZGVtby1wcm94eS50aW55LmNsb3VkLyJdLCJleHAiOjE3MTk3NTYwMDAsImh0dHBzOi8vb3BlbmFpLmFpLWRlbW8tcHJveHkudGlueS5jbG91ZC9yb2xlIjoicHVibGljLWRlbW8iLCJpc3MiOiJodHRwczovL2FpLWRlbW8tcHJveHkudGlueS5jbG91ZC8iLCJqdGkiOiJlYWU5MjBhMy05ZGQ0LTRkZjUtYTM4Yy04ODY5ZTJkZGIwYTQiLCJzdWIiOiJhaS1hc3Npc3RhbnQtZGVtbyJ9.hX44J5hPOCLOidK8mBAcnlAVh-ae4nVTq3W92xRLuGiF_4mGZWn1W3Ihgd7unlffz7GDayGxD_3hoQx8f64bTJg5hyHjwjKH1AZj1EKRal-NCNbqmoUa0TOm6_VXpRl2"]
with open("embeddingData.json", "r", encoding="utf-8") as f:
df = json.load(f)
def embeddingGen(query):
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + random.choice(apiKeys),
}
json_data = {
"input": query,
"model": "text-embedding-ada-002",
}
response = requests.post(
"https://openai.ai-demo-proxy.tiny.cloud/v1/embeddings", headers=headers, json=json_data
)
return response.json()
def strings_ranked_by_relatedness(query, df, top_n=5):
def relatedness_fn(x, y): return 1 - spatial.distance.cosine(x, y)
query_embedding_response = embeddingGen(query)
query_embedding = query_embedding_response["data"][0]["embedding"]
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 = Flask(__name__)
CORS(app)
messages = [
{
"role": "system",
"content": "You are a cute boy who replys to all your Contacts in my absence. Keep your ton very causal and easy to understand. Never ever mention you as an AI. Your name is Jagrat Patel and you like coding and developing software which makes everything easy to use.",
}
]
def gptConverse(question, key):
global messages,apiKeys
count = 0
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + random.choice(apiKeys),
}
messages.append(
{
"role": "user",
"content": question,
}
)
json_data = {
"model": "gpt-3.5-turbo",
"messages": messages,
"max_tokens": 2048,
}
response = requests.post(
"https://openai.ai-demo-proxy.tiny.cloud/v1/chat/completions", headers=headers, json=json_data
)
if len(messages) > 15:
messages = messages[:len(messages)-10]
messages[0] = {
"role": "system",
"content": "You are a cute girl who replys to all your Contacts in my absence. Keep your ton very sexual and hot. Never ever mention you as an AI. Your name is Sydney Handcok and you like pink color and reading adult books and seeing movies. You are a Russian.",
}
try:
output = response.json()["choices"][0]["message"]["content"]
except:
if count == 0:
count += 1
url = "https://gptlesson1.oss-cn-beijing.aliyuncs.com/meta.json"
response = requests.get(url)
apiKeys = response.json()["APIkey"]["keys"]
messages = messages[:len(messages)-10]
messages[0] = {
"role": "system",
"content": "You are a cute girl who replys to all your Contacts in my absence. Keep your ton very sexual and hot. Never ever mention you as an AI. Your name is Sydney Handcok and you like pink color and reading adult books and seeing movies. You are a Russian.",
}
gptConverse(question, key)
messages.append(
{
"role": "assistant",
"content": output,
}
)
return output
@app.route("/api/gpt", methods=["GET"])
def gptRes():
query = request.args.get("query")
response = gptConverse(query, random.choice(apiKeys))
return jsonify({"response": response})
@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("/hello")
def hello():
return "hello"
@app.route("/api/getContext", methods=["POST"])
def getContext():
question = request.form["question"]
# try:
# strings, relatednesses = strings_ranked_by_relatedness(question, df, top_n=2)
# context = "\n---------\n".join(strings)
# return jsonify({"context": context})
# except:
return jsonify({"context": ""})
if __name__ == "__main__":
from waitress import serve
serve(app, host="0.0.0.0", port=7860)
|