Update app.py
Browse files
app.py
CHANGED
@@ -3,13 +3,25 @@ from PIL import Image
|
|
3 |
from main import ImageProcessor, PDFProcessor # Replace 'your_module' with the actual module where your ImageProcessor and PDFProcessor classes are defined
|
4 |
import google.generativeai as genai
|
5 |
import os
|
|
|
|
|
|
|
6 |
|
7 |
genai.configure(api_key=os.getenv("API_KEY"))
|
8 |
llm = genai.GenerativeModel("gemini-pro")
|
9 |
|
10 |
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
# File upload for image or PDF
|
15 |
file_type = st.radio("Select File Type:", ["Image", "PDF"])
|
@@ -21,21 +33,35 @@ query = st.text_input("Enter your question:")
|
|
21 |
# Display box for generated answer
|
22 |
answer_display = st.empty()
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
from main import ImageProcessor, PDFProcessor # Replace 'your_module' with the actual module where your ImageProcessor and PDFProcessor classes are defined
|
4 |
import google.generativeai as genai
|
5 |
import os
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
|
10 |
genai.configure(api_key=os.getenv("API_KEY"))
|
11 |
llm = genai.GenerativeModel("gemini-pro")
|
12 |
|
13 |
|
14 |
+
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'application_default_credentials.json'
|
15 |
+
|
16 |
+
# title
|
17 |
+
st.markdown(
|
18 |
+
"""
|
19 |
+
<div style="text-align:center">
|
20 |
+
<h1>Q&A with Images & PDF 🤖</h1>
|
21 |
+
</div>
|
22 |
+
""",
|
23 |
+
unsafe_allow_html=True
|
24 |
+
)
|
25 |
|
26 |
# File upload for image or PDF
|
27 |
file_type = st.radio("Select File Type:", ["Image", "PDF"])
|
|
|
33 |
# Display box for generated answer
|
34 |
answer_display = st.empty()
|
35 |
|
36 |
+
# Creating instances outside the conditional check
|
37 |
+
image_processor = ImageProcessor(uploaded_file) if uploaded_file and file_type == "Image" else None
|
38 |
+
pdf_processor = PDFProcessor(uploaded_file) if uploaded_file and file_type == "PDF" else None
|
39 |
+
|
40 |
+
|
41 |
+
if st.button("Generate answer"):
|
42 |
+
try:
|
43 |
+
if not query:
|
44 |
+
raise ValueError("Please provide the question. DO NOT KEEP IT EMPTY!")
|
45 |
+
|
46 |
+
if uploaded_file is not None:
|
47 |
+
if file_type == "Image":
|
48 |
+
# Image processing
|
49 |
+
|
50 |
+
captions = image_processor.get_caption(uploaded_file)
|
51 |
+
detections = image_processor.detect_objects(uploaded_file)
|
52 |
+
prompt = image_processor.make_prompt(query, captions, detections)
|
53 |
+
answer = image_processor.generate_answer(prompt)
|
54 |
+
answer_display.markdown(answer)
|
55 |
+
|
56 |
+
elif file_type == "PDF":
|
57 |
+
# PDF processing
|
58 |
+
|
59 |
+
pdf_vector_stores = pdf_processor.create_embedding_df(uploaded_file)
|
60 |
+
relevant_passage = pdf_processor.find_best_passage(query, pdf_vector_stores)
|
61 |
+
prompt = pdf_processor.make_prompt(query, relevant_passage)
|
62 |
+
answer = pdf_processor.generate_answer(prompt)
|
63 |
+
answer_display.markdown(answer)
|
64 |
+
|
65 |
+
|
66 |
+
except ValueError as e:
|
67 |
+
st.warning(str(e))
|