Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,79 @@
|
|
1 |
-
import
|
|
|
|
|
|
|
|
|
|
|
2 |
import gradio as gr
|
3 |
|
4 |
-
def slow_echo(message, history):
|
5 |
-
for i in range(len(message)):
|
6 |
-
time.sleep(0.05)
|
7 |
-
yield "You typed: " + message[: i+1]
|
8 |
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
if __name__ == "__main__":
|
12 |
demo.launch()
|
|
|
|
1 |
+
import sklearn
|
2 |
+
import sqlite3
|
3 |
+
import numpy as np
|
4 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
5 |
+
import openai
|
6 |
+
import os
|
7 |
import gradio as gr
|
8 |
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
openai.api_key = os.environ["Secret"]
|
11 |
+
|
12 |
+
def find_closest_neighbors(vector1, dictionary_of_vectors):
|
13 |
+
"""
|
14 |
+
Takes a vector and a dictionary of vectors and returns the three closest neighbors
|
15 |
+
"""
|
16 |
+
vector = openai.Embedding.create(
|
17 |
+
input=vector1,
|
18 |
+
engine="text-embedding-ada-002"
|
19 |
+
)['data'][0]['embedding']
|
20 |
+
|
21 |
+
vector = np.array(vector)
|
22 |
+
|
23 |
+
cosine_similarities = {}
|
24 |
+
for key, value in dictionary_of_vectors.items():
|
25 |
+
cosine_similarities[key] = cosine_similarity(vector.reshape(1, -1), value.reshape(1, -1))[0][0]
|
26 |
+
|
27 |
+
sorted_cosine_similarities = sorted(cosine_similarities.items(), key=lambda x: x[1], reverse=True)
|
28 |
+
match_list = sorted_cosine_similarities[0:4]
|
29 |
+
|
30 |
+
return match_list
|
31 |
+
|
32 |
+
def predict(message, history):
|
33 |
+
# Connect to the database
|
34 |
+
conn = sqlite3.connect('QRIdatabase7.db')
|
35 |
+
cursor = conn.cursor()
|
36 |
+
cursor.execute('''SELECT text, embedding FROM chunks''')
|
37 |
+
rows = cursor.fetchall()
|
38 |
+
|
39 |
+
dictionary_of_vectors = {}
|
40 |
+
for row in rows:
|
41 |
+
text = row[0]
|
42 |
+
embedding_str = row[1]
|
43 |
+
embedding = np.fromstring(embedding_str, sep=' ')
|
44 |
+
dictionary_of_vectors[text] = embedding
|
45 |
+
conn.close()
|
46 |
+
|
47 |
+
# Find the closest neighbors
|
48 |
+
match_list = find_closest_neighbors(message, dictionary_of_vectors)
|
49 |
+
context = ''
|
50 |
+
for match in match_list:
|
51 |
+
context += str(match[0])
|
52 |
+
context = context[:-1500]
|
53 |
+
|
54 |
+
prep = f"This is an OpenAI model tuned to answer questions specific to the Qualia Research institute, a research institute that focuses on consciousness. Here is some question-specific context, and then the Question to answer, related to consciousness, the human experience, and phenomenology: {context}. Here is a question specific to QRI and consciousness in general Q: {message} A: "
|
55 |
+
|
56 |
+
history_openai_format = []
|
57 |
+
for human, assistant in history:
|
58 |
+
history_openai_format.append({"role": "user", "content": human })
|
59 |
+
history_openai_format.append({"role": "assistant", "content":assistant})
|
60 |
+
history_openai_format.append({"role": "user", "content": prep})
|
61 |
+
|
62 |
+
response = openai.ChatCompletion.create(
|
63 |
+
model='gpt-3.5-turbo',
|
64 |
+
messages= history_openai_format,
|
65 |
+
temperature=1.0,
|
66 |
+
stream=True
|
67 |
+
)
|
68 |
+
|
69 |
+
partial_message = ""
|
70 |
+
for chunk in response:
|
71 |
+
if len(chunk['choices'][0]['delta']) != 0:
|
72 |
+
partial_message = partial_message + chunk['choices'][0]['delta']['content']
|
73 |
+
yield partial_message
|
74 |
+
|
75 |
+
demo = gr.ChatInterface(predict).queue()
|
76 |
|
77 |
if __name__ == "__main__":
|
78 |
demo.launch()
|
79 |
+
|