Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>AI Summary Generator</title>
<style>
body { font-family: Arial; max-width: 600px; margin: 50px auto; }
input, button { margin-top: 10px; }
#summary { margin-top: 20px; white-space: pre-wrap; background: #f0f0f0; padding: 10px; }
</style>
</head>
<body>
<h2>AI Summary Generator</h2>
<input type="file" id="fileInput" />
<button onclick="uploadFile()">Summarize</button>
<div id="summary"></div>

<script>
async function uploadFile() {
const fileInput = document.getElementById("fileInput");
if (!fileInput.files.length) {
alert("Please select a file!");
return;
}

const file = fileInput.files[0];
const formData = new FormData();
formData.append("file", file);

document.getElementById("summary").innerText = "Summarizing...";

try {
const response = await fetch("http://127.0.0.1:8000/summarize", {
method: "POST",
body: formData
});

const data = await response.json();

if (data.summary) {
document.getElementById("summary").innerText = data.summary;
} else if (data.error) {
document.getElementById("summary").innerText = "Error: " + data.error;
}
} catch (err) {
document.getElementById("summary").innerText = "Server error: " + err;
}
}
</script>
</body>
</html>
60 changes: 60 additions & 0 deletions summarizer_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from fastapi import FastAPI, File, UploadFile
from fastapi.middleware.cors import CORSMiddleware
import PyPDF2
import docx
import openai

app = FastAPI()

# Allow requests from frontend
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)

# Set your OpenAI API key
openai.api_key = "YOUR_OPENAI_API_KEY"

def read_pdf(file):
pdf_reader = PyPDF2.PdfReader(file)
text = ""
for page in pdf_reader.pages:
text += page.extract_text()
return text

def read_docx(file):
doc = docx.Document(file)
text = ""
for para in doc.paragraphs:
text += para.text + "\n"
return text

@app.post("/summarize")
async def summarize(file: UploadFile = File(...)):
try:
# Read file based on extension
extension = file.filename.split('.')[-1].lower()
if extension == "pdf":
content = read_pdf(file.file)
elif extension == "docx":
content = read_docx(file.file)
elif extension == "txt":
content = (await file.read()).decode("utf-8")
else:
return {"error": "Unsupported file format"}

# Call OpenAI GPT for summarization
response = openai.Completion.create(
model="text-davinci-003",
prompt=f"Summarize the following text concisely:\n\n{content}",
max_tokens=300,
temperature=0.5
)

summary = response.choices[0].text.strip()
return {"summary": summary}

except Exception as e:
return {"error": str(e)}