trial inf-app
Browse files
app.py
CHANGED
|
@@ -1,86 +1,86 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
import requests
|
| 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 |
def greet(query):
|
| 80 |
# first we retrieve relevant items from Pinecone
|
| 81 |
-
|
| 82 |
-
#
|
| 83 |
-
|
| 84 |
response = requests.post("https://siddh4rth-narrify.hf.space/run/predict", json={
|
| 85 |
"data": [
|
| 86 |
query,
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
+
import openai
|
| 4 |
+
import pinecone
|
| 5 |
import requests
|
| 6 |
|
| 7 |
+
openai.api_key = "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"
|
| 8 |
+
|
| 9 |
+
pinecone.init(
|
| 10 |
+
api_key="d307ef27-f3ee-4335-b89d-c866395df920",
|
| 11 |
+
environment="us-east1-gcp",
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
limit = 5000
|
| 15 |
+
# 3750
|
| 16 |
+
|
| 17 |
+
embed_model = "text-embedding-ada-002"
|
| 18 |
+
|
| 19 |
+
index_name = 'gen-qa'
|
| 20 |
+
index = pinecone.Index(index_name)
|
| 21 |
+
|
| 22 |
+
# retrieve relevant answers
|
| 23 |
+
def retrieve(query):
|
| 24 |
+
res = openai.Embedding.create(
|
| 25 |
+
input=[query],
|
| 26 |
+
engine=embed_model,
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
# retrieve from Pinecone
|
| 30 |
+
xq = res['data'][0]['embedding']
|
| 31 |
+
|
| 32 |
+
# get relevant contexts
|
| 33 |
+
res = index.query(xq, top_k=3, include_metadata=True)
|
| 34 |
+
contexts = [
|
| 35 |
+
x['metadata']['text'] for x in res['matches']
|
| 36 |
+
]
|
| 37 |
+
|
| 38 |
+
# build our prompt with the retrieved contexts included
|
| 39 |
+
prompt_start = (
|
| 40 |
+
"Answer the question based on the context below.\n\n"+
|
| 41 |
+
"Context:\n"
|
| 42 |
+
)
|
| 43 |
+
prompt_end = (
|
| 44 |
+
f"\n\nQuestion: {query}\nAnswer:"
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
# append contexts until hitting limit
|
| 48 |
+
for i in range(1, len(contexts)):
|
| 49 |
+
if len("\n\n---\n\n".join(contexts[:i])) >= limit:
|
| 50 |
+
prompt = (
|
| 51 |
+
prompt_start +
|
| 52 |
+
"\n\n---\n\n".join(contexts[:i-1]) +
|
| 53 |
+
prompt_end
|
| 54 |
+
)
|
| 55 |
+
break
|
| 56 |
+
elif i == len(contexts)-1:
|
| 57 |
+
prompt = (
|
| 58 |
+
prompt_start +
|
| 59 |
+
"\n\n---\n\n".join(contexts) +
|
| 60 |
+
prompt_end
|
| 61 |
+
)
|
| 62 |
+
return prompt
|
| 63 |
+
|
| 64 |
+
# then we complete the context-infused query
|
| 65 |
+
def complete(prompt):
|
| 66 |
+
# query text-davinci-003
|
| 67 |
+
res = openai.Completion.create(
|
| 68 |
+
engine='text-davinci-003',
|
| 69 |
+
prompt=prompt,
|
| 70 |
+
temperature=0,
|
| 71 |
+
max_tokens=500,
|
| 72 |
+
top_p=1,
|
| 73 |
+
frequency_penalty=0,
|
| 74 |
+
presence_penalty=0,
|
| 75 |
+
stop=None
|
| 76 |
+
)
|
| 77 |
+
return res['choices'][0]['text'].strip()
|
| 78 |
|
| 79 |
def greet(query):
|
| 80 |
# first we retrieve relevant items from Pinecone
|
| 81 |
+
query_with_contexts = retrieve(query)
|
| 82 |
+
# return only the main answer
|
| 83 |
+
result = complete(query_with_contexts)
|
| 84 |
response = requests.post("https://siddh4rth-narrify.hf.space/run/predict", json={
|
| 85 |
"data": [
|
| 86 |
query,
|