Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- Dockerfile +26 -0
- app.py +36 -0
- requirements.txt +6 -0
Dockerfile
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Gunakan image base Python
|
| 2 |
+
FROM python:3.11
|
| 3 |
+
|
| 4 |
+
# Set working directory
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Install build tools dan clean apt cache agar image tetap ringan
|
| 8 |
+
RUN apt-get update && apt-get install -y \
|
| 9 |
+
build-essential \
|
| 10 |
+
git \
|
| 11 |
+
curl \
|
| 12 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 13 |
+
|
| 14 |
+
# Copy requirements dan install
|
| 15 |
+
COPY requirements.txt .
|
| 16 |
+
RUN pip install --upgrade pip
|
| 17 |
+
RUN pip install --no-cache-dir -r requirements.txt -f https://download.pytorch.org/whl/torch_stable.html
|
| 18 |
+
|
| 19 |
+
# Copy seluruh kode aplikasi
|
| 20 |
+
COPY . .
|
| 21 |
+
|
| 22 |
+
# Expose port yang digunakan (sesuaikan dengan port API)
|
| 23 |
+
EXPOSE 8000
|
| 24 |
+
|
| 25 |
+
# Jalankan aplikasi
|
| 26 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
|
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# Inisialisasi FastAPI
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
# Deteksi device (GPU jika tersedia)
|
| 10 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 11 |
+
|
| 12 |
+
# Load model dan tokenizer
|
| 13 |
+
model_name = "hanifahputri/Capstone-Model-SumAI"
|
| 14 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 15 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name).to(device)
|
| 16 |
+
|
| 17 |
+
# Schema untuk input menggunakan Pydantic
|
| 18 |
+
class SummarizationRequest(BaseModel):
|
| 19 |
+
text: str
|
| 20 |
+
|
| 21 |
+
# Endpoint untuk summarization
|
| 22 |
+
@app.post("/summarize")
|
| 23 |
+
def summarize(request: SummarizationRequest):
|
| 24 |
+
text = request.text
|
| 25 |
+
inputs = tokenizer.encode("summarize: " + text, return_tensors="pt", max_length=512, truncation=True).to(device)
|
| 26 |
+
outputs = model.generate(
|
| 27 |
+
inputs,
|
| 28 |
+
max_length=150,
|
| 29 |
+
min_length=30,
|
| 30 |
+
length_penalty=2.0,
|
| 31 |
+
num_beams=4,
|
| 32 |
+
early_stopping=True
|
| 33 |
+
)
|
| 34 |
+
summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 35 |
+
return {"summary": summary}
|
| 36 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.115.12
|
| 2 |
+
uvicorn==0.34.2
|
| 3 |
+
torch==2.3.0+cpu
|
| 4 |
+
transformers==4.51.3
|
| 5 |
+
sentencepiece==0.2.0
|
| 6 |
+
protobuf==4.25.3
|