Spaces:
Sleeping
Sleeping
Anwar11234
commited on
Commit
•
3204b21
1
Parent(s):
879ec11
initial commit
Browse files- Dockerfile +10 -0
- app.py +54 -0
- requirements.txt +5 -0
Dockerfile
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.10.9
|
2 |
+
WORKDIR /
|
3 |
+
|
4 |
+
RUN pip install --no-cache-dir --upgrade -r /requirements.txt
|
5 |
+
RUN mkdir -p /.cache/huggingface/hub \
|
6 |
+
&& chmod -R 777 /.cache/huggingface \
|
7 |
+
|
8 |
+
COPY . .
|
9 |
+
|
10 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, Body, Depends
|
2 |
+
from typing import Dict
|
3 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
4 |
+
from peft import PeftModel, PeftConfig
|
5 |
+
from fastapi.middleware.cors import CORSMiddleware
|
6 |
+
import os
|
7 |
+
from huggingface_hub import HfApi
|
8 |
+
|
9 |
+
hf_api = HfApi() # Create a Hugging Face API client
|
10 |
+
access_token = os.environ.get("HF_TOKEN")
|
11 |
+
hf_api.set_access_token(access_token)
|
12 |
+
|
13 |
+
app = FastAPI()
|
14 |
+
|
15 |
+
app.add_middleware(
|
16 |
+
CORSMiddleware,
|
17 |
+
allow_origins=['*'],
|
18 |
+
allow_credentials=True,
|
19 |
+
allow_methods=["*"],
|
20 |
+
allow_headers=["*"]
|
21 |
+
)
|
22 |
+
|
23 |
+
def load_model():
|
24 |
+
peft_model_id = "ANWAR101/lora-bart-base-youtube-cnn"
|
25 |
+
config = PeftConfig.from_pretrained(peft_model_id , use_auth_token=access_token)
|
26 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(config.base_model_name_or_path)
|
27 |
+
tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
|
28 |
+
model = PeftModel.from_pretrained(model, peft_model_id)
|
29 |
+
return model , tokenizer
|
30 |
+
|
31 |
+
|
32 |
+
@app.post("/summarize")
|
33 |
+
async def summarize(data: Dict[str, str] = Body(...)):
|
34 |
+
"""Summarize a text using the loaded Peft model."""
|
35 |
+
model , tokenizer = load_model()
|
36 |
+
|
37 |
+
text = data.get("text")
|
38 |
+
|
39 |
+
# Check for missing text
|
40 |
+
if not text:
|
41 |
+
return {"error": "Missing text in request body"}, 400
|
42 |
+
|
43 |
+
# Preprocess the text
|
44 |
+
inputs = tokenizer(text, truncation=True, return_tensors="pt")
|
45 |
+
|
46 |
+
# Generate summary using the model
|
47 |
+
outputs = model.generate(
|
48 |
+
**inputs, max_length=300, min_length=50, do_sample=True, num_beams=3,
|
49 |
+
no_repeat_ngram_size=2, temperature=0.6, length_penalty=1.0
|
50 |
+
)
|
51 |
+
|
52 |
+
summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
53 |
+
response = {"summary": summary}
|
54 |
+
return response
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers==4.38.2
|
2 |
+
peft==0.3.0
|
3 |
+
fastapi
|
4 |
+
uvicorn
|
5 |
+
huggingface_hub
|