Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import openai
|
2 |
+
import sqlite3
|
3 |
+
import numpy as np
|
4 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
# Your OpenAI API Key
|
8 |
+
openai.api_key = os.environ["Secret"]
|
9 |
+
|
10 |
+
# Connect to the SQLite database
|
11 |
+
db_path = "text_chunks_with_embeddings.db" # Update with the path to your database
|
12 |
+
conn = sqlite3.connect(db_path)
|
13 |
+
cursor = conn.cursor()
|
14 |
+
|
15 |
+
# Fetch the rows from the database
|
16 |
+
cursor.execute("SELECT text, embedding FROM chunks")
|
17 |
+
rows = cursor.fetchall()
|
18 |
+
|
19 |
+
# Create a dictionary to store the text and embedding for each row
|
20 |
+
dictionary_of_vectors = {}
|
21 |
+
for row in rows:
|
22 |
+
text = row[0]
|
23 |
+
embedding_str = row[1]
|
24 |
+
embedding = np.fromstring(embedding_str, sep=' ')
|
25 |
+
dictionary_of_vectors[text] = embedding
|
26 |
+
|
27 |
+
# Close the connection
|
28 |
+
conn.close()
|
29 |
+
|
30 |
+
def find_closest_neighbors(vector):
|
31 |
+
cosine_similarities = {}
|
32 |
+
for key, value in dictionary_of_vectors.items():
|
33 |
+
cosine_similarities[key] = cosine_similarity(vector.reshape(1, -1), value.reshape(1, -1))[0][0]
|
34 |
+
|
35 |
+
sorted_cosine_similarities = sorted(cosine_similarities.items(), key=lambda x: x[1], reverse=True)
|
36 |
+
return sorted_cosine_similarities[0:4]
|
37 |
+
|
38 |
+
def generate_embedding(text):
|
39 |
+
response = openai.Embedding.create(
|
40 |
+
input=text,
|
41 |
+
engine="text-embedding-ada-002"
|
42 |
+
)
|
43 |
+
embedding = np.array(response['data'][0]['embedding'])
|
44 |
+
return embedding
|
45 |
+
|
46 |
+
def context_gpt_response(question):
|
47 |
+
vector = generate_embedding(question)
|
48 |
+
match_list = find_closest_neighbors(vector)
|
49 |
+
|
50 |
+
context = ''
|
51 |
+
for match in match_list:
|
52 |
+
context += str(match[0])
|
53 |
+
|
54 |
+
context = context[:1500] # Limit context to the last 1500 characters
|
55 |
+
|
56 |
+
prep = f"This is an OpenAI model designed to answer questions specific to grant-making applications for an aquarium. Here is some question-specific context: {context}. Q: {question} A: "
|
57 |
+
response = openai.Completion.create(
|
58 |
+
engine="gpt-4",
|
59 |
+
prompt=prep,
|
60 |
+
temperature=0.7,
|
61 |
+
max_tokens=220,
|
62 |
+
)
|
63 |
+
|
64 |
+
return response['choices'][0]['text']
|
65 |
+
|
66 |
+
iface = gr.Interface(fn=context_gpt_response, inputs="text", outputs="text", title="Aquarium Grant Application Chatbot", description="Context-specific chatbot for grant writing", examples=[["What types of projects are eligible for funding?"], ["Tell me more about the application process."], ["What will be the most impactful grant opportunities?"]])
|
67 |
+
iface.launch()
|