Update app.py
Browse files
app.py
CHANGED
@@ -1,35 +1,16 @@
|
|
1 |
-
# app.py (
|
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']
|
@@ -50,70 +31,42 @@ def generate_audio(text: str, output_path: str):
|
|
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)
|
60 |
-
with open(file_path, "wb") as f:
|
61 |
-
f.write(file.file.read())
|
62 |
-
|
63 |
-
text = extract_text_from_pdf(file_path)
|
64 |
-
topic_list = [t.strip() for t in topics.split(",")]
|
65 |
-
classified_topic = classify_topic(text, topic_list)
|
66 |
-
summary = summarize_text(text)
|
67 |
-
|
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 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
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()
|
|
|
1 |
+
# app.py (Streamlit-only version for Hugging Face Spaces)
|
2 |
|
3 |
import os
|
|
|
4 |
import tempfile
|
5 |
from typing import List
|
6 |
|
7 |
import fitz # PyMuPDF
|
8 |
import requests
|
|
|
9 |
from transformers import pipeline
|
10 |
from gtts import gTTS
|
|
|
|
|
11 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
# ---------- CONFIG ----------
|
|
|
|
|
14 |
def summarize_text(text: str) -> str:
|
15 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
16 |
return summarizer(text, max_length=200, min_length=30, do_sample=False)[0]['summary_text']
|
|
|
31 |
tts = gTTS(text)
|
32 |
tts.save(output_path)
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
# ---------- STREAMLIT UI ----------
|
35 |
+
st.set_page_config(page_title="Research Paper Summarizer", layout="centered")
|
36 |
+
st.title("π AI Research Paper Summarizer")
|
37 |
+
|
38 |
+
st.markdown("""
|
39 |
+
Upload a research paper (PDF) and a list of topics. The app will:
|
40 |
+
1. Extract and summarize the paper
|
41 |
+
2. Classify it into a topic
|
42 |
+
3. Generate an audio summary π§
|
43 |
+
""")
|
44 |
+
|
45 |
+
with st.form("upload_form"):
|
46 |
+
uploaded_file = st.file_uploader("Upload a PDF file", type=["pdf"])
|
47 |
+
topic_input = st.text_input("Enter comma-separated topics")
|
48 |
+
submitted = st.form_submit_button("Summarize and Generate Audio")
|
49 |
+
|
50 |
+
if submitted and uploaded_file and topic_input:
|
51 |
+
with st.spinner("Processing paper..."):
|
52 |
+
temp_dir = tempfile.mkdtemp()
|
53 |
+
file_path = os.path.join(temp_dir, uploaded_file.name)
|
54 |
+
|
55 |
+
with open(file_path, "wb") as f:
|
56 |
+
f.write(uploaded_file.read())
|
57 |
+
|
58 |
+
text = extract_text_from_pdf(file_path)
|
59 |
+
topic_list = [t.strip() for t in topic_input.split(",") if t.strip()]
|
60 |
+
classified_topic = classify_topic(text, topic_list)
|
61 |
+
summary = summarize_text(text)
|
62 |
+
|
63 |
+
st.markdown(f"### π§ Classified Topic: `{classified_topic}`")
|
64 |
+
st.markdown("### βοΈ Summary:")
|
65 |
+
st.write(summary)
|
66 |
+
|
67 |
+
audio_path = os.path.join(temp_dir, "summary.mp3")
|
68 |
+
generate_audio(summary, audio_path)
|
69 |
+
|
70 |
+
st.markdown("### π Audio Summary")
|
71 |
+
st.audio(audio_path)
|
72 |
+
st.success("Done! Audio summary is ready.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|