Spaces:
Build error
Build error
Download app.py from isalaarkhan/PDF_BOT: direct link, hf CLI and curl.
- Browser
- Download file 3.07 kB
-
https://huggingface.co/spaces/isalaarkhan/PDF_BOT/resolve/main/app.py
- Command line
-
hf download hf://spaces/isalaarkhan/PDF_BOT/app.py
-
curl -L -o app.py https://huggingface.co/spaces/isalaarkhan/PDF_BOT/resolve/main/app.py
3.07 kB
| import os | |
| import streamlit as st | |
| from PIL import Image | |
| from PyMuPDF import fitz # PyMuPDF | |
| import google.generativeai as genai | |
| # Fetch API key from environment variable | |
| api_key = os.getenv("GENAI_API_KEY") | |
| # Configure the Generative AI model | |
| genai.configure(api_key=api_key) | |
| model = genai.GenerativeModel('gemini-1.5-flash') | |
| # Set page configuration | |
| st.set_page_config(page_title="ICT PDF BOT PROJECT β¨ ", page_icon="books") | |
| # App title and description | |
| st.title("PDF BOTβ¨") | |
| # Displaying an image from a folder | |
| img = Image.open('pdfimage.png') | |
| st.image(img, use_column_width=True) | |
| # Sidebar PDF Actions selection | |
| st.sidebar.header("WELCOME TO PDF BOTβ¨") | |
| # Add a dropdown menu | |
| pdfAction_options = ["π PDF SUMMARY", "π SUMMARIZE in BULLETS", "π PDF AUDIO", "π Title and Heading Generation"] | |
| selected_option = st.sidebar.selectbox("Please select an option from the dropdown below:", pdfAction_options) | |
| # Session state for storing responses | |
| if 'response_text' not in st.session_state: | |
| st.session_state['response_text'] = "" | |
| uploaded_file = st.file_uploader("π Upload your document", type=["pdf"]) | |
| # PDF SUMMARY | |
| if selected_option == "π PDF SUMMARY": | |
| st.write("### π PDF SUMMARY") | |
| if uploaded_file is not None: | |
| doc = fitz.open(stream=uploaded_file.read(), filetype="pdf") | |
| text_content = "".join([page.get_text() for page in doc]) | |
| prompt = f"""Provide a very short summary, no more than three to six sentences for {text_content} | |
| Summary: | |
| """ | |
| response = model.generate_content([text_content, prompt]) | |
| st.session_state['response_text'] = response.text | |
| st.write(st.session_state['response_text']) | |
| # Bullet Summarization | |
| elif selected_option == "π SUMMARIZE in BULLETS": | |
| st.write("### π SUMMARIZE in BULLETS") | |
| if uploaded_file is not None: | |
| doc = fitz.open(stream=uploaded_file.read(), filetype="pdf") | |
| text_content = "".join([page.get_text() for page in doc]) | |
| prompt = f"""Provide a very short summary in four bullet points for {text_content}""" | |
| response = model.generate_content([text_content, prompt]) | |
| st.session_state['response_text'] = response.text | |
| st.write(st.session_state['response_text']) | |
| # Title and Heading Generation | |
| elif selected_option == "π Title and Heading Generation": | |
| st.write("### π Title and Heading Generation") | |
| if uploaded_file is not None: | |
| doc = fitz.open(stream=uploaded_file.read(), filetype="pdf") | |
| text_content = "".join([page.get_text() for page in doc]) | |
| prompt = f"""Write a title for this text, give me five options: for {text_content}""" | |
| response = model.generate_content([text_content, prompt]) | |
| st.session_state['response_text'] = response.text | |
| st.write(st.session_state['response_text']) | |
| # Create a download button | |
| st.download_button( | |
| label="Download Text", | |
| data=st.session_state['response_text'], | |
| file_name="generated_text.txt", | |
| mime="text/plain" | |
| ) | |
| st.sidebar.subheader('Created by SALAAR TARIQ.....') | |