ashwinaravind commited on
Commit
b6d0bda
·
verified ·
1 Parent(s): 67fb07f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import openai
3
+ import json
4
+ from pinecone import Pinecone
5
+ pc = Pinecone(api_key='72f7d24d-c080-4ce6-a060-a489713284cb')
6
+ index_name = 'imdb-vector'
7
+ # connect to index
8
+ index = pc.Index(index_name)
9
+ # get api key from platform.openai.com
10
+ openai.api_key = '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'
11
+ embed_model = "text-embedding-ada-002"
12
+ from openai import OpenAI
13
+ client = OpenAI(api_key=openai.api_key)
14
+ def generate_context(text):
15
+ body=json.dumps({"inputText": text})
16
+ res = client.embeddings.create(
17
+ input=[text],
18
+ model=embed_model
19
+ ).data[0].embedding
20
+ result = index.query(vector=res, top_k=7, include_metadata=True)
21
+ contexts = []
22
+ contexts = contexts + [x['metadata']['text'] for x in result['matches']]
23
+ return contexts
24
+
25
+ def invoke_openai(prompt):
26
+ sys_prompt = "You are a helpful assistant that always answers questions."
27
+ # query text-davinci-003
28
+ res = client.chat.completions.create(
29
+ model='gpt-3.5-turbo',
30
+ messages=[
31
+ {"role": "system", "content": sys_prompt},
32
+ {"role": "user", "content": prompt}
33
+ ],
34
+ temperature=0
35
+ )
36
+ return res.choices[0].message.content
37
+
38
+ def build_prompt(message,history):
39
+ context=generate_context(message)
40
+ messages=[]
41
+ prompt=f'Context - {context}\nBased on the above context, answer this question - {message}'
42
+ print(prompt)
43
+ return invoke_openai(prompt)
44
+
45
+
46
+ iface = gr.ChatInterface(build_prompt, chatbot=gr.Chatbot(height=300), textbox=gr.Textbox(placeholder="Ask me a question", container=False, scale=7), title="IMDB Sentiment Data Analysis Chat", examples = ["Which is most postive review for a movie?", "What is the most reviewed movie?"], theme="soft", cache_examples=False, retry_btn=None, undo_btn="Delete Previous", clear_btn="Clear",)
47
+ iface.launch(share=True