Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import sqlite3
|
| 3 |
+
import numpy as np
|
| 4 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 5 |
+
import openai
|
| 6 |
+
|
| 7 |
+
def find_closest_neighbors(vector1, dictionary_of_vectors):
|
| 8 |
+
"""
|
| 9 |
+
Takes a vector and a dictionary of vectors and returns the three closest neighbors
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
# Convert the input string to a vector
|
| 13 |
+
vector = openai.Embedding.create(
|
| 14 |
+
input=vector1,
|
| 15 |
+
engine="text-embedding-ada-002"
|
| 16 |
+
)['data'][0]['embedding']
|
| 17 |
+
|
| 18 |
+
vector = np.array(vector)
|
| 19 |
+
|
| 20 |
+
# Finds cosine similarities between the vector and values in the dictionary and Creates a dictionary of cosine similarities with its text key
|
| 21 |
+
cosine_similarities = {}
|
| 22 |
+
for key, value in dictionary_of_vectors.items():
|
| 23 |
+
cosine_similarities[key] = cosine_similarity(vector.reshape(1, -1), value.reshape(1, -1))[0][0]
|
| 24 |
+
|
| 25 |
+
# Sorts the dictionary by value and returns the three highest values
|
| 26 |
+
sorted_cosine_similarities = sorted(cosine_similarities.items(), key=lambda x: x[1], reverse=True)
|
| 27 |
+
match_list = sorted_cosine_similarities[0:4]
|
| 28 |
+
web = str(sorted_cosine_similarities[0][0])
|
| 29 |
+
return match_list
|
| 30 |
+
|
| 31 |
+
# Connect to the database
|
| 32 |
+
conn = sqlite3.connect('QRIdatabase.db')
|
| 33 |
+
|
| 34 |
+
# Create a cursor
|
| 35 |
+
cursor = conn.cursor()
|
| 36 |
+
|
| 37 |
+
# Select the text and embedding from the chunks table
|
| 38 |
+
cursor.execute('''SELECT text, embedding FROM chunks''')
|
| 39 |
+
|
| 40 |
+
# Fetch the rows
|
| 41 |
+
rows = cursor.fetchall()
|
| 42 |
+
|
| 43 |
+
# Create a dictionary to store the text and embedding for each row
|
| 44 |
+
dictionary_of_vectors = {}
|
| 45 |
+
|
| 46 |
+
# Iterate through the rows and add them to the dictionary
|
| 47 |
+
for row in rows:
|
| 48 |
+
text = row[0]
|
| 49 |
+
embedding_str = row[1]
|
| 50 |
+
# Convert the embedding string to a NumPy array
|
| 51 |
+
embedding = np.fromstring(embedding_str, sep=' ')
|
| 52 |
+
dictionary_of_vectors[text] = embedding
|
| 53 |
+
|
| 54 |
+
# Close the connection
|
| 55 |
+
conn.close()
|
| 56 |
+
|
| 57 |
+
def context_gpt_response(question):
|
| 58 |
+
"""
|
| 59 |
+
Takes a question and returns an answer
|
| 60 |
+
"""
|
| 61 |
+
|
| 62 |
+
# Find the closest neighbors
|
| 63 |
+
match_list = find_closest_neighbors(question, dictionary_of_vectors)
|
| 64 |
+
|
| 65 |
+
# Create a string of the text from the closest neighbors
|
| 66 |
+
context = ''
|
| 67 |
+
for match in match_list:
|
| 68 |
+
context += str(match[0])
|
| 69 |
+
|
| 70 |
+
# Generate an answer
|
| 71 |
+
response = openai.Completion.create(
|
| 72 |
+
engine="text-davinci-003",
|
| 73 |
+
prompt=context + question,
|
| 74 |
+
temperature=0.7,
|
| 75 |
+
max_tokens=150,
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
# Return the answer
|
| 79 |
+
return response['choices'][0]['text']
|
| 80 |
+
|
| 81 |
+
import gradio as gr
|
| 82 |
+
|
| 83 |
+
iface = gr.Interface(fn=context_gpt_response, inputs="text", outputs="text")
|
| 84 |
+
iface.launch()
|
| 85 |
+
|