Spaces:
Sleeping
Sleeping
Update app.py
Browse files12345678910111213141 unmodified line16171819202122232425262728293031323334353637383940414243444546474849501 unmodified line5253545556575859606162636465667 unmodified lines7475767778798023 unmodified lines104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227import base64import streamlit as stimport openaiimport osfrom PIL import Imageimport ioimport tempfileimport pdf2image# Set OpenAI API key from Streamlit secretsopenai.api_key = st.secrets["OPENAI_API_KEY"]def convert_pdf_to_images(pdf_file):"""Convert PDF to list of images"""1 unmodified linetmp_file.write(pdf_file.getvalue())pdf_path = tmp_file.nametry:images = pdf2image.convert_from_path(pdf_path)os.unlink(pdf_path)return imagesexcept Exception as e:st.error(f"Error converting PDF: {str(e)}")return []def format_response(text):"""Format the analysis response with clean styling"""formatted_text = """<div style="background-color: white;padding: 20px;border-radius: 5px;font-family: Arial, sans-serif;box-shadow: 0 2px 4px rgba(0,0,0,0.1);">"""# Split into pagespages = text.split("Page")for page_num, page_content in enumerate(pages[1:], 1):# Add page headerformatted_text += f'<div style="margin-bottom: 30px;">'formatted_text += f'<h3 style="color: #2c3e50; margin-bottom: 15px;">Page {page_num}</h3>'# Process each linelines = page_content.split('\n')for line in lines:if line.strip() and not line.strip().startswith('*') and not line.strip().startswith('Here'):1 unmodified lineif ':' in line:label, value = line.split(':', 1)formatted_text += f'<div style="margin-bottom: 10px; display: flex;">'formatted_text += f'<span style="font-weight: bold; color: #2c3e50; min-width: 200px;">{label.strip()}</span>'formatted_text += f'<span style="color: #34495e; flex: 1;">{value.strip()}</span>'formatted_text += '</div>'formatted_text += '</div>'if page_num < len(pages) - 1:formatted_text += '<hr style="border: 1px solid #eee; margin: 20px 0;">'formatted_text += '</div>'return formatted_text7 unmodified linesbase64_image = base64.b64encode(img_byte_arr).decode("utf-8")response = openai.ChatCompletion.create(model="gpt-4-vision-preview",messages=[{23 unmodified linesmax_tokens=1000)return response.choices[0].message['content']except Exception as e:st.error(f"API Error: {str(e)}")return f"An error occurred: {str(e)}"def main():st.set_page_config(page_title="Document Analysis App", layout="wide")# Updated styling with better contrast and modern lookst.markdown("""<style>.stApp {background-color: #f8f9fa;}.stButton>button {width: 100%;background-color: #0066cc; /* Changed to a more vibrant blue */color: white;border: none;padding: 12px 24px;border-radius: 6px;margin-top: 20px;margin-bottom: 20px;font-weight: 500;transition: background-color 0.3s ease;}.stButton>button:hover {background-color: #0052a3;}.uploadedFile {margin-bottom: 20px;background-color: white;padding: 15px;border-radius: 6px;border: 1px solid #e0e0e0;}div[data-testid="stFileUploader"] {background-color: white;padding: 20px;border-radius: 10px;border: 2px dashed #cccccc;margin: 20px 0;}h1 {color: #1a1a1a;margin-bottom: 30px;text-align: center;}</style>""", unsafe_allow_html=True)# Update the format_response function stylingdef format_response(text):formatted_text = """<div style="background-color: white;padding: 30px;border-radius: 10px;font-family: 'Segoe UI', Arial, sans-serif;box-shadow: 0 4px 6px rgba(0,0,0,0.1);margin: 20px 0;border: 1px solid #e0e0e0;">"""pages = text.split("Page")for page_num, page_content in enumerate(pages[1:], 1):formatted_text += f'<div style="margin-bottom: 30px;">'formatted_text += f'<h3 style="color: #0066cc; margin-bottom: 20px; padding-bottom: 10px; border-bottom: 2px solid #f0f0f0;">Page {page_num}</h3>'lines = page_content.split('\n')for line in lines:if line.strip() and not line.strip().startswith('*') and not line.strip().startswith('Here'):line = line.replace('**', '').replace('- ', '')if ':' in line:label, value = line.split(':', 1)formatted_text += f'<div style="margin-bottom: 15px; display: flex; align-items: center;">'formatted_text += f'<span style="font-weight: 600; color: #2c3e50; min-width: 200px; padding-right: 20px;">{label.strip()}</span>'formatted_text += f'<span style="color: #333333; flex: 1;">{value.strip()}</span>'formatted_text += '</div>'formatted_text += '</div>'if page_num < len(pages) - 1:formatted_text += '<hr style="border: none; height: 1px; background-color: #e0e0e0; margin: 30px 0;">'formatted_text += '</div>'return formatted_textcol1, col2, col3 = st.columns([1,2,1])with col2:st.title("Document Analysis App")uploaded_file = st.file_uploader("Upload document (PDF/Image)", type=['pdf', 'png', 'jpg', 'jpeg'])if uploaded_file is not None:if uploaded_file.type == "application/pdf":with st.spinner("Processing PDF..."):images = convert_pdf_to_images(uploaded_file)if st.button("Extract Information"):with st.spinner("Analyzing document..."):all_results = []for i, image in enumerate(images, 1):result = analyze_image(image)all_results.append(f"Page {i} Information:\n{result}")combined_results = "\n\n".join(all_results)st.markdown(format_response(combined_results), unsafe_allow_html=True)else:image = Image.open(uploaded_file)if st.button("Extract Information"):with st.spinner("Analyzing document..."):result = analyze_image(image)st.markdown(format_response(result), unsafe_allow_html=True)if __name__ == "__main__":main()12345678910111213141516171 unmodified line19202122232425262728293031323334353637383940414243444546474849505152531 unmodified line55565758596061626364656667686970717273747576777879807 unmodified lines8889909192939423 unmodified lines118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210import base64import streamlit as stfrom openai import OpenAIimport osfrom dotenv import load_dotenvimport pdf2imagefrom PIL import Imageimport ioimport tempfile# Load environment variablesload_dotenv()# Initialize OpenAI clientclient = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))def convert_pdf_to_images(pdf_file):"""Convert PDF to list of images"""1 unmodified linetmp_file.write(pdf_file.getvalue())pdf_path = tmp_file.nameimages = pdf2image.convert_from_path(pdf_path)os.unlink(pdf_path)return imagesdef format_response(text):formatted_text = """<div style="background-color: white;padding: 30px;border-radius: 10px;font-family: Arial, sans-serif;box-shadow: 0 2px 8px rgba(0,0,0,0.1);margin: 20px 0;">"""pages = text.split("Page")for page_num, page_content in enumerate(pages[1:], 1):formatted_text += f'''<div style="margin-bottom: 40px;"><h3 style="color: #2c3e50;margin-bottom: 20px;padding-bottom: 10px;border-bottom: 2px solid #eee;">Page {page_num}</h3>'''lines = page_content.split('\n')for line in lines:if line.strip() and not line.strip().startswith('*') and not line.strip().startswith('Here'):1 unmodified lineif ':' in line:label, value = line.split(':', 1)formatted_text += f'''<div style="margin-bottom: 15px;display: flex;align-items: baseline;gap: 20px;"><span style="font-weight: 600;color: #2c3e50;min-width: 200px;">{label.strip()}</span><span style="color: #34495e; flex: 1;">{value.strip()}</span></div>'''formatted_text += '</div>'if page_num < len(pages) - 1:formatted_text += '<hr style="border: none; border-top: 1px solid #eee; margin: 30px 0;">'formatted_text += '</div>'return formatted_text7 unmodified linesbase64_image = base64.b64encode(img_byte_arr).decode("utf-8")response = client.chat.completions.create(model="gpt-4-vision-preview",messages=[{23 unmodified linesmax_tokens=1000)return response.choices[0].message.contentexcept Exception as e:return f"An error occurred: {str(e)}"def main():st.set_page_config(page_title="Document Analysis App", layout="wide")# Custom CSSst.markdown("""<style>.stApp {background-color: #ffffff !important;}.main {padding: 20px;max-width: 1200px;margin: 0 auto;}.stButton>button {width: 100%;background-color: #2c3e50;color: white;border: none;padding: 10px 20px;border-radius: 5px;margin: 20px 0;}.stButton>button:hover {background-color: #34495e;}.uploadedFile {margin: 20px 0;padding: 20px;background-color: #f8f9fa;border-radius: 5px;}[data-testid="stHeader"] {background-color: transparent;}.stDeployButton {display: none !important;}.main .block-container {padding-top: 2rem;padding-bottom: 2rem;}</style>""", unsafe_allow_html=True)# Main containerwith st.container():col1, col2, col3 = st.columns([1,3,1])with col2:st.title("Document Analysis App")upload_container = st.container()with upload_container:uploaded_file = st.file_uploader("Upload document (PDF/Image)",type=['pdf', 'png', 'jpg', 'jpeg'])if uploaded_file is not None:if uploaded_file.type == "application/pdf":# Handle PDFwith st.spinner("Processing PDF..."):images = convert_pdf_to_images(uploaded_file)if st.button("Extract Information"):with st.spinner("Analyzing document..."):all_results = []for i, image in enumerate(images, 1):result = analyze_image(image)all_results.append(f"Page {i} Information:\n{result}")combined_results = "\n\n".join(all_results)st.markdown(format_response(combined_results), unsafe_allow_html=True)else:# Handle single imageimage = Image.open(uploaded_file)if st.button("Extract Information"):with st.spinner("Analyzing document..."):result = analyze_image(image)st.markdown(format_response(f"Page 1\n{result}"), unsafe_allow_html=True)if __name__ == "__main__":main()
app.py+99-116