Update app.py
Browse files
app.py
CHANGED
@@ -1,111 +1,112 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
|
4 |
-
from
|
5 |
-
from llama_index.
|
6 |
-
from llama_index.
|
7 |
-
from llama_index.core import VectorStoreIndex
|
8 |
-
from
|
9 |
-
from
|
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 |
-
HarmCategory.
|
43 |
-
HarmCategory.
|
44 |
-
HarmCategory.
|
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 |
-
|
80 |
-
|
81 |
-
|
82 |
-
###
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
gr.
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
"
|
104 |
-
"
|
105 |
-
"
|
106 |
-
"
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
|
|
111 |
demo.queue().launch(debug=True)
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
import google
|
4 |
+
from pinecone import Pinecone
|
5 |
+
from llama_index.embeddings.gemini import GeminiEmbedding
|
6 |
+
from llama_index.vector_stores.pinecone import PineconeVectorStore
|
7 |
+
from llama_index.core.indices import VectorStoreIndex
|
8 |
+
from llama_index.core import VectorStoreIndex
|
9 |
+
from google.generativeai.types import HarmCategory, HarmBlockThreshold
|
10 |
+
from llama_index.llms.gemini import Gemini
|
11 |
+
|
12 |
+
PINECONE_API_KEY = os.environ["PINECONE_API_KEY"]
|
13 |
+
|
14 |
+
def is_valid_gemini_api_key(api_key):
|
15 |
+
if len(api_key.strip()) == 39:
|
16 |
+
return True
|
17 |
+
return False
|
18 |
+
|
19 |
+
def prepare_query_engine(api_key):
|
20 |
+
# Gemini Embeddings
|
21 |
+
embed_model = GeminiEmbedding(
|
22 |
+
model="models/embedding-001",
|
23 |
+
title="Oppenheimer movie wikipedia",
|
24 |
+
embed_batch_size=16,
|
25 |
+
api_key=api_key
|
26 |
+
)
|
27 |
+
|
28 |
+
# Pinecone Vector Store
|
29 |
+
INDEX_NAME = "rag"
|
30 |
+
pinecone = Pinecone(api_key=PINECONE_API_KEY)
|
31 |
+
pinecone_index = pinecone.Index(INDEX_NAME)
|
32 |
+
vector_store = PineconeVectorStore(pinecone_index=pinecone_index)
|
33 |
+
|
34 |
+
# Load Index
|
35 |
+
index_loaded = VectorStoreIndex.from_vector_store(
|
36 |
+
vector_store=vector_store,
|
37 |
+
embed_model=embed_model
|
38 |
+
)
|
39 |
+
|
40 |
+
# Gemini Safety Settings
|
41 |
+
safety_settings={
|
42 |
+
HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE,
|
43 |
+
HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
|
44 |
+
HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
|
45 |
+
HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
|
46 |
+
}
|
47 |
+
|
48 |
+
# Gemini Pro
|
49 |
+
llm = Gemini(
|
50 |
+
model_name="models/gemini-pro",
|
51 |
+
temperature=0,
|
52 |
+
max_tokens=512,
|
53 |
+
safety_settings=safety_settings,
|
54 |
+
api_key=api_key
|
55 |
+
)
|
56 |
+
|
57 |
+
# Query Engine
|
58 |
+
query_engine = index_loaded.as_query_engine(
|
59 |
+
llm=llm,
|
60 |
+
similarity_top_k=3,
|
61 |
+
)
|
62 |
+
|
63 |
+
return query_engine
|
64 |
+
|
65 |
+
# Generates response using the query engine
|
66 |
+
def generate(query, api_key):
|
67 |
+
if api_key.strip()=='' or not is_valid_gemini_api_key(api_key):
|
68 |
+
return "Please enter a valid Gemini api key"
|
69 |
+
else:
|
70 |
+
try:
|
71 |
+
query_engine = prepare_query_engine(api_key)
|
72 |
+
response = query_engine.query(query)
|
73 |
+
return response.response
|
74 |
+
except google.api_core.exceptions.BadRequest as br:
|
75 |
+
return "API key not valid. Please enter a valid API key"
|
76 |
+
except Exception as e:
|
77 |
+
return str(e)
|
78 |
+
|
79 |
+
with gr.Blocks() as demo:
|
80 |
+
gr.Markdown("""
|
81 |
+
# Retrieval Augmented Generation with Gemini Pro, Pinecone and LlamaIndex: Question Answering demo
|
82 |
+
### This demo uses the Gemini Pro LLM and Pinecone Vector Search for fast and performant Retrieval Augmented Generation (RAG).
|
83 |
+
### The context is the new Oppenheimer movie's entire wikipedia page. The movie came out very recently in July, 2023, so the Gemini Pro model is not aware of it.
|
84 |
+
Retrieval Augmented Generation (RAG) enables us to retrieve just the few small chunks of the document that are relevant to the our query and inject it into our prompt.
|
85 |
+
The model is then able to answer questions by incorporating knowledge from the newly provided document. RAG can be used with thousands of documents, but this demo is limited to just one txt file.
|
86 |
+
""")
|
87 |
+
GEMINI_API_KEY = gr.Textbox(label="GEMINI_API_KEY", placeholder="Enter your GEMINI API KEY", lines=1, type="password")
|
88 |
+
gr.Markdown("## Enter your question")
|
89 |
+
with gr.Row():
|
90 |
+
with gr.Column():
|
91 |
+
ques = gr.Textbox(label="Question", placeholder="Enter text here", lines=2)
|
92 |
+
with gr.Column():
|
93 |
+
ans = gr.Textbox(label="Answer", lines=4, interactive=False)
|
94 |
+
with gr.Row():
|
95 |
+
with gr.Column():
|
96 |
+
btn = gr.Button("Submit")
|
97 |
+
with gr.Column():
|
98 |
+
clear = gr.ClearButton([ques, ans])
|
99 |
+
|
100 |
+
btn.click(fn=generate, inputs=[ques, GEMINI_API_KEY], outputs=[ans])
|
101 |
+
examples = gr.Examples(
|
102 |
+
examples=[
|
103 |
+
"Who portrayed J. Robert Oppenheimer in the new Oppenheimer movie?",
|
104 |
+
"In the plot of the movie, why did Lewis Strauss resent Robert Oppenheimer?",
|
105 |
+
"What happened while Oppenheimer was a student at the University of Cambridge?",
|
106 |
+
"How much money did the Oppenheimer movie make at the US and global box office?",
|
107 |
+
"What score did the Oppenheimer movie get on Rotten Tomatoes and Metacritic?"
|
108 |
+
],
|
109 |
+
inputs=[ques],
|
110 |
+
)
|
111 |
+
|
112 |
demo.queue().launch(debug=True)
|