louiecerv commited on
Commit
59e2122
Β·
1 Parent(s): e0b3ffe

full sync with remote repo

Browse files
Files changed (2) hide show
  1. app.py +94 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import base64
3
+ import requests
4
+ import streamlit as st
5
+ from reportlab.pdfgen import canvas
6
+ from reportlab.lib.pagesizes import letter
7
+ from io import BytesIO
8
+ from PyPDF2 import PdfReader
9
+ from PIL import Image
10
+
11
+ # OpenAI API Key
12
+ api_key = os.getenv("OPENAI_API_KEY") # Ensure this environment variable is set
13
+
14
+ headers = {
15
+ "Content-Type": "application/json",
16
+ "Authorization": f"Bearer {api_key}"
17
+ }
18
+
19
+ def extract_text_and_images_from_pdf(pdf_file):
20
+ text_content = ""
21
+ images = []
22
+
23
+ # Convert UploadedFile to BytesIO for compatibility
24
+ pdf_stream = BytesIO(pdf_file.read())
25
+
26
+ # Extract text using PdfReader
27
+ pdf_reader = PdfReader(pdf_stream)
28
+ for page in pdf_reader.pages:
29
+ text_content += page.extract_text()
30
+
31
+ # Extract images (This part requires creating images using ReportLab for demonstration)
32
+ # In a real scenario, extracting images from PDF is more complex and usually done with specialized libraries.
33
+ # Add image extraction logic if needed
34
+
35
+ return text_content, images
36
+
37
+ def main():
38
+ st.title("Multimodal PDF Processing using GPT-4 Turbo Model")
39
+
40
+ text = """Prof. Louie F. Cervantes, M. Eng. (Information Engineering)
41
+ CCS 229 - Intelligent Systems
42
+ Department of Computer Science
43
+ College of Information and Communications Technology
44
+ West Visayas State University
45
+ """
46
+ with st.expander("About"):
47
+ st.text(text)
48
+
49
+ st.write("Upload a PDF file for analysis.")
50
+
51
+ # File upload for PDF
52
+ uploaded_pdf = st.file_uploader("Upload a PDF", type=["pdf"])
53
+ if uploaded_pdf is not None:
54
+ text_content, images = extract_text_and_images_from_pdf(uploaded_pdf)
55
+
56
+ # Display extracted text
57
+ st.subheader("Extracted Text")
58
+ st.text(text_content)
59
+
60
+ # Display extracted images
61
+ if images:
62
+ st.subheader("Extracted Images")
63
+ for img in images:
64
+ st.image(img, caption="Extracted Image", use_container_width=True)
65
+
66
+ # Prepare the multimodal payload
67
+ payload = {
68
+ "model": "gpt-4-turbo",
69
+ "messages": [
70
+ {
71
+ "role": "user",
72
+ "content": [
73
+ {"type": "text", "text": text_content}
74
+ # Images can be added here if extracted
75
+ ]
76
+ }
77
+ ],
78
+ "max_tokens": 2048,
79
+ }
80
+
81
+ if st.button("Generate Response"):
82
+ with st.spinner("Processing..."):
83
+ response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
84
+
85
+ if response.status_code != 200:
86
+ st.error(f"Error: {response.status_code} - {response.text}")
87
+ else:
88
+ content = response.json()
89
+ content_string = content['choices'][0]['message']['content']
90
+ st.success("Response generated!")
91
+ st.markdown(f"AI Response: {content_string}")
92
+
93
+ if __name__ == "__main__":
94
+ main()
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ streamlit
2
+ requests
3
+ Pillow
4
+ reportlab
5
+ PyPDF2