Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,39 @@
|
|
| 1 |
-
# app.py
|
| 2 |
|
| 3 |
import os
|
| 4 |
import uuid
|
| 5 |
import tempfile
|
| 6 |
from typing import List
|
| 7 |
-
|
| 8 |
-
from fastapi.responses import FileResponse
|
| 9 |
-
from pydantic import BaseModel
|
| 10 |
import fitz # PyMuPDF
|
| 11 |
import requests
|
| 12 |
import openai
|
| 13 |
from transformers import pipeline
|
| 14 |
-
import torch
|
| 15 |
from gtts import gTTS
|
| 16 |
import shutil
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
# ---------- CONFIG ----------
|
| 19 |
-
openai.api_key = os.getenv("
|
| 20 |
|
| 21 |
def summarize_text(text: str) -> str:
|
| 22 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 23 |
return summarizer(text, max_length=200, min_length=30, do_sample=False)[0]['summary_text']
|
| 24 |
|
| 25 |
-
# ---------- FASTAPI SETUP ----------
|
| 26 |
-
app = FastAPI(title="Research Paper Summarization App")
|
| 27 |
-
|
| 28 |
-
class SummaryRequest(BaseModel):
|
| 29 |
-
topic: str
|
| 30 |
-
urls: List[str] = []
|
| 31 |
-
|
| 32 |
-
# ---------- HELPERS ----------
|
| 33 |
def extract_text_from_pdf(pdf_path: str) -> str:
|
| 34 |
doc = fitz.open(pdf_path)
|
| 35 |
text = ""
|
|
@@ -46,9 +50,10 @@ def generate_audio(text: str, output_path: str):
|
|
| 46 |
tts = gTTS(text)
|
| 47 |
tts.save(output_path)
|
| 48 |
|
| 49 |
-
# ----------
|
|
|
|
| 50 |
|
| 51 |
-
@
|
| 52 |
def upload_paper(file: UploadFile = File(...), topics: str = Form(...)):
|
| 53 |
temp_dir = tempfile.mkdtemp()
|
| 54 |
file_path = os.path.join(temp_dir, file.filename)
|
|
@@ -63,31 +68,52 @@ def upload_paper(file: UploadFile = File(...), topics: str = Form(...)):
|
|
| 63 |
audio_path = os.path.join(temp_dir, "summary.mp3")
|
| 64 |
generate_audio(summary, audio_path)
|
| 65 |
|
| 66 |
-
result = {
|
| 67 |
-
"topic": classified_topic,
|
| 68 |
-
"summary": summary,
|
| 69 |
-
"audio_file": audio_path
|
| 70 |
-
}
|
| 71 |
-
|
| 72 |
return FileResponse(audio_path, media_type="audio/mpeg", filename="summary.mp3")
|
| 73 |
|
| 74 |
-
|
| 75 |
-
def
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py (Combined FastAPI + Streamlit UI)
|
| 2 |
|
| 3 |
import os
|
| 4 |
import uuid
|
| 5 |
import tempfile
|
| 6 |
from typing import List
|
| 7 |
+
|
|
|
|
|
|
|
| 8 |
import fitz # PyMuPDF
|
| 9 |
import requests
|
| 10 |
import openai
|
| 11 |
from transformers import pipeline
|
|
|
|
| 12 |
from gtts import gTTS
|
| 13 |
import shutil
|
| 14 |
|
| 15 |
+
import streamlit as st
|
| 16 |
+
from fastapi import FastAPI, UploadFile, File, Form
|
| 17 |
+
from fastapi.responses import FileResponse
|
| 18 |
+
from fastapi.middleware.wsgi import WSGIMiddleware
|
| 19 |
+
from starlette.responses import Response
|
| 20 |
+
from starlette.routing import Mount
|
| 21 |
+
from starlette.applications import Starlette
|
| 22 |
+
from starlette.middleware.cors import CORSMiddleware
|
| 23 |
+
from starlette.staticfiles import StaticFiles
|
| 24 |
+
from pydantic import BaseModel
|
| 25 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 26 |
+
from fastapi.staticfiles import StaticFiles
|
| 27 |
+
from fastapi.middleware.wsgi import WSGIMiddleware
|
| 28 |
+
import uvicorn
|
| 29 |
+
|
| 30 |
# ---------- CONFIG ----------
|
| 31 |
+
openai.api_key = os.getenv("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")
|
| 32 |
|
| 33 |
def summarize_text(text: str) -> str:
|
| 34 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 35 |
return summarizer(text, max_length=200, min_length=30, do_sample=False)[0]['summary_text']
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
def extract_text_from_pdf(pdf_path: str) -> str:
|
| 38 |
doc = fitz.open(pdf_path)
|
| 39 |
text = ""
|
|
|
|
| 50 |
tts = gTTS(text)
|
| 51 |
tts.save(output_path)
|
| 52 |
|
| 53 |
+
# ---------- FASTAPI BACKEND ----------
|
| 54 |
+
fastapi_app = FastAPI()
|
| 55 |
|
| 56 |
+
@fastapi_app.post("/upload")
|
| 57 |
def upload_paper(file: UploadFile = File(...), topics: str = Form(...)):
|
| 58 |
temp_dir = tempfile.mkdtemp()
|
| 59 |
file_path = os.path.join(temp_dir, file.filename)
|
|
|
|
| 68 |
audio_path = os.path.join(temp_dir, "summary.mp3")
|
| 69 |
generate_audio(summary, audio_path)
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
return FileResponse(audio_path, media_type="audio/mpeg", filename="summary.mp3")
|
| 72 |
|
| 73 |
+
# ---------- STREAMLIT UI ----------
|
| 74 |
+
def streamlit_ui():
|
| 75 |
+
st.set_page_config(page_title="Research Paper Summarizer", layout="centered")
|
| 76 |
+
st.title("📄 AI Research Paper Summarizer")
|
| 77 |
+
|
| 78 |
+
st.markdown("""
|
| 79 |
+
Upload a research paper (PDF) and a list of topics. The app will:
|
| 80 |
+
1. Extract and summarize the paper
|
| 81 |
+
2. Classify it into a topic
|
| 82 |
+
3. Generate an audio summary 🎧
|
| 83 |
+
""")
|
| 84 |
+
|
| 85 |
+
with st.form("upload_form"):
|
| 86 |
+
uploaded_file = st.file_uploader("Upload a PDF file", type=["pdf"])
|
| 87 |
+
topic_input = st.text_input("Enter comma-separated topics")
|
| 88 |
+
submitted = st.form_submit_button("Summarize and Generate Audio")
|
| 89 |
+
|
| 90 |
+
if submitted and uploaded_file and topic_input:
|
| 91 |
+
with st.spinner("Processing paper..."):
|
| 92 |
+
files = {"file": (uploaded_file.name, uploaded_file, "application/pdf")}
|
| 93 |
+
data = {"topics": topic_input}
|
| 94 |
+
|
| 95 |
+
response = requests.post("http://localhost:8000/upload", files=files, data=data)
|
| 96 |
+
|
| 97 |
+
if response.status_code == 200:
|
| 98 |
+
audio_path = "summary.mp3"
|
| 99 |
+
with open(audio_path, "wb") as f:
|
| 100 |
+
f.write(response.content)
|
| 101 |
+
st.audio(audio_path)
|
| 102 |
+
st.success("Audio summary generated!")
|
| 103 |
+
else:
|
| 104 |
+
st.error("Something went wrong during processing.")
|
| 105 |
+
|
| 106 |
+
# ---------- ENTRY POINT ----------
|
| 107 |
+
if __name__ == "__main__":
|
| 108 |
+
import threading
|
| 109 |
+
from multiprocessing import Process
|
| 110 |
+
|
| 111 |
+
def run_api():
|
| 112 |
+
uvicorn.run(fastapi_app, host="0.0.0.0", port=8000)
|
| 113 |
+
|
| 114 |
+
api_process = Process(target=run_api)
|
| 115 |
+
api_process.start()
|
| 116 |
+
|
| 117 |
+
streamlit_ui()
|
| 118 |
+
|
| 119 |
+
api_process.terminate()
|