rasyosef commited on
Commit
4051112
1 Parent(s): 05822b7

Update app.py

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