Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
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 |
+
# Streamlit title
|
12 |
+
st.title("Q&A with Images & PDF")
|
13 |
+
|
14 |
+
# File upload for image or PDF
|
15 |
+
file_type = st.radio("Select File Type:", ["Image", "PDF"])
|
16 |
+
uploaded_file = st.file_uploader("Upload file:", type=["jpg", "jpeg", "png", "pdf"])
|
17 |
+
|
18 |
+
# Query text box
|
19 |
+
query = st.text_input("Enter your question:")
|
20 |
+
|
21 |
+
# Display box for generated answer
|
22 |
+
answer_display = st.empty()
|
23 |
+
|
24 |
+
if uploaded_file is not None:
|
25 |
+
if file_type == "Image":
|
26 |
+
# Image processing
|
27 |
+
image_processor = ImageProcessor(uploaded_file)
|
28 |
+
captions = image_processor.get_caption(uploaded_file)
|
29 |
+
detections = image_processor.detect_objects(uploaded_file)
|
30 |
+
prompt = image_processor.make_prompt(query, captions, detections)
|
31 |
+
answer = image_processor.generate_answer(prompt)
|
32 |
+
answer_display.text(answer)
|
33 |
+
|
34 |
+
elif file_type == "PDF":
|
35 |
+
# PDF processing
|
36 |
+
pdf_processor = PDFProcessor(uploaded_file)
|
37 |
+
pdf_vector_stores = pdf_processor.create_embedding_df(uploaded_file)
|
38 |
+
relevant_passage = pdf_processor.find_best_passage(query, pdf_vector_stores)
|
39 |
+
prompt = pdf_processor.make_prompt(query, relevant_passage)
|
40 |
+
answer = pdf_processor.generate_answer(prompt)
|
41 |
+
answer_display.text(answer)
|