AI-assistant / app.py
sairaarif89's picture
Update app.py
edd520f verified
import streamlit as st
import fitz # PyMuPDF for PDF extraction
import google.generativeai as genai
from io import BytesIO
# Configure Gemini API
genai.configure(api_key="AIzaSyCiFKbaF-HQsRaKT7_c1HlgzPE25TxwUr4")
# Function to extract text from PDF using BytesIO
def extract_text_from_pdf(pdf_file):
# Read the file into memory as a byte stream
file_bytes = BytesIO(pdf_file.read())
# Open the byte stream with fitz (PyMuPDF)
doc = fitz.open(stream=file_bytes)
text = ""
for page in doc:
text += page.get_text("text") + "\n"
return text
# Function to summarize the research paper
def summarize_paper(text):
model = genai.GenerativeModel("gemini-pro")
response = model.generate_content(f"Summarize this research paper:\n{text}")
return response.text
# Function to answer user questions based on the paper
def answer_question(question, paper_text):
model = genai.GenerativeModel("gemini-pro")
response = model.generate_content(f"Based on this research paper:\n{paper_text}\nAnswer this question: {question}")
return response.text
# Streamlit UI
st.title("AI-Powered Research Paper Summarizer")
st.write("Upload your research paper (PDF) and ask questions related to it.")
# Upload PDF
uploaded_file = st.file_uploader("Choose a PDF file", type=["pdf"])
if uploaded_file is not None:
# Extract and summarize the paper
paper_text = extract_text_from_pdf(uploaded_file)
summary = summarize_paper(paper_text)
# Display the summary
st.subheader("Summary of the Research Paper")
st.write(summary)
# Question input
st.subheader("Ask a Question About the Paper")
user_question = st.text_input("Your question:")
if user_question:
answer = answer_question(user_question, paper_text)
st.write(f"**Answer:** {answer}")