ai-mechanicaldesign's picture
Update app.py
6889bf3 verified
import streamlit as st
import PyPDF2
import google.generativeai as genai
# Step 1: Set up Gemini API
GEMINI_API_KEY = "AIzaSyAJ9xJ83VSmJoyVXmplPw-HaDkBfro-jiM" # Replace with your Gemini API key
genai.configure(api_key=GEMINI_API_KEY)
# Initialize the Gemini model
model = genai.GenerativeModel('gemini-pro')
# Step 2: Function to extract text from PDF
def extract_text_from_pdf(pdf_file):
reader = PyPDF2.PdfReader(pdf_file)
text = ""
for page in reader.pages:
text += page.extract_text()
return text
# Step 3: Function to summarize text using Gemini
def summarize_text(text):
prompt = f"Summarize the following research paper in 200 words:\n\n{text}"
response = model.generate_content(prompt)
return response.text
# Step 4: Function to answer user questions using Gemini
def answer_question(text, question):
prompt = f"Based on the following research paper, answer the question:\n\n{text}\n\nQuestion: {question}"
response = model.generate_content(prompt)
return response.text
# Step 5: Streamlit App
def main():
st.title("Haroon Research Paper Summarizer")
st.write("Upload a research paper in PDF format, and I'll summarize it and answer your questions!")
# File uploader
uploaded_file = st.file_uploader("Upload a PDF file", type="pdf")
if uploaded_file is not None:
# Extract text from the uploaded PDF
text = extract_text_from_pdf(uploaded_file)
st.success("PDF uploaded and text extracted successfully!")
# Summarize the text
st.subheader("Summary of the Research Paper")
summary = summarize_text(text)
st.write(summary)
# Interactive Q&A
st.subheader("Ask Questions About the Research Paper")
question = st.text_input("Type your question here:")
if question:
answer = answer_question(text, question)
st.write("**Answer:**")
st.write(answer)
if __name__ == "__main__":
main()