Download app.py from Pezh/Embed: direct link, hf CLI and curl.
- Browser
- Download file 1.11 kB
-
https://huggingface.co/spaces/Pezh/Embed/resolve/main/app.py
- Command line
-
hf download hf://spaces/Pezh/Embed/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Pezh/Embed/resolve/main/app.py
1.11 kB
| import PyPDF2 | |
| import google.generativeai as genai | |
| import streamlit as st | |
| #genai.configure(api_key="AIzaSyBd36RWeqDpLur3E7TTlX3wnyIh_rdhsU8") | |
| def ask_question_from_pdf(pdf_file, query): | |
| # Open the PDF file | |
| with open(pdf_file, 'rb') as file: | |
| reader = PyPDF2.PdfReader(file) | |
| text = '' | |
| for page_number in range(len(reader.pages)): | |
| text += reader.pages[page_number].extract_text() | |
| # Embed the extracted text into Gemini AI | |
| embedded_text = genai.embed_content( | |
| model="models/embedding-001", | |
| content=[text], | |
| task_type="retrieval_document", | |
| title="Embedding of PDF text" | |
| ) | |
| # Use the embedded text to ask a question and get a response | |
| result = genai.ask( | |
| model="models/question-answering-001", | |
| inputs={ | |
| "question": query, | |
| "context": embedded_text | |
| } | |
| ) | |
| # Extract and return the answer | |
| answer = result['answers'][0]['answer'] | |
| return answer | |
| st.write(answer) | |
| if st.button('Find it'): | |
| ask_question_from_pdf('hoop.pdf', 'who will elect the officers?') |