Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import gradio as gr
|
3 |
+
import openai
|
4 |
+
import os
|
5 |
+
|
6 |
+
# Ensure your OpenAI key is set in your HF Secrets (Settings > Secrets)
|
7 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
8 |
+
|
9 |
+
def summarize_pdf(file):
|
10 |
+
try:
|
11 |
+
# Read PDF file content
|
12 |
+
with open(file.name, "rb") as f:
|
13 |
+
import PyPDF2
|
14 |
+
reader = PyPDF2.PdfReader(f)
|
15 |
+
text = ""
|
16 |
+
for page in reader.pages:
|
17 |
+
text += page.extract_text()
|
18 |
+
|
19 |
+
if not text.strip():
|
20 |
+
return "❌ No extractable text found in the uploaded PDF."
|
21 |
+
|
22 |
+
# Send to OpenAI for summarization
|
23 |
+
response = openai.ChatCompletion.create(
|
24 |
+
model="gpt-3.5-turbo",
|
25 |
+
messages=[
|
26 |
+
{"role": "system", "content": "You are a helpful assistant that summarizes academic papers."},
|
27 |
+
{"role": "user", "content": f"Summarize this paper:\n\n{text}"}
|
28 |
+
],
|
29 |
+
temperature=0.7,
|
30 |
+
max_tokens=1000
|
31 |
+
)
|
32 |
+
|
33 |
+
summary = response["choices"][0]["message"]["content"]
|
34 |
+
return summary.strip()
|
35 |
+
|
36 |
+
except Exception as e:
|
37 |
+
return f"⚠️ Error: {str(e)}"
|
38 |
+
|
39 |
+
# Gradio UI
|
40 |
+
demo = gr.Interface(
|
41 |
+
fn=summarize_pdf,
|
42 |
+
inputs=gr.File(label="Upload Research Paper PDF", file_types=[".pdf"]),
|
43 |
+
outputs=gr.Textbox(label="Full Summary"),
|
44 |
+
title="📄 Paper News Summarizer",
|
45 |
+
description="Upload a research paper PDF and get a human-friendly summary. Powered by GPT-3.5."
|
46 |
+
)
|
47 |
+
|
48 |
+
demo.launch()
|