siddh4rth commited on
Commit
491514e
·
1 Parent(s): 19bf8ae

trial app

Browse files
Files changed (1) hide show
  1. app.py +76 -76
app.py CHANGED
@@ -1,86 +1,86 @@
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,
 
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,