Update main.py
Browse files
main.py
CHANGED
|
@@ -20,7 +20,7 @@ PORT = int(os.getenv("PORT", 7860))
|
|
| 20 |
app = FastAPI(
|
| 21 |
title = "AI-Powered Web-App API",
|
| 22 |
description = "Backend for summarisation, captioning & QA",
|
| 23 |
-
version = "1.2.
|
| 24 |
)
|
| 25 |
|
| 26 |
app.add_middleware(
|
|
@@ -47,12 +47,15 @@ summary_client = InferenceClient(
|
|
| 47 |
timeout = 120,
|
| 48 |
)
|
| 49 |
|
| 50 |
-
# ➜ Upgraded QA model
|
| 51 |
qa_client = InferenceClient(
|
| 52 |
-
"
|
| 53 |
token = HUGGINGFACE_TOKEN,
|
| 54 |
-
timeout =
|
| 55 |
)
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
image_caption_client = InferenceClient(
|
| 58 |
"nlpconnect/vit-gpt2-image-captioning",
|
|
@@ -111,6 +114,7 @@ async def summarize_document(file: UploadFile = File(...)):
|
|
| 111 |
# -------------------- Image Caption ------------------------------------------
|
| 112 |
@app.post("/api/caption")
|
| 113 |
async def caption_image(image: UploadFile = File(...)):
|
|
|
|
| 114 |
try:
|
| 115 |
img_bytes = await image.read()
|
| 116 |
img = Image.open(io.BytesIO(img_bytes)).convert("RGB")
|
|
@@ -145,7 +149,7 @@ async def question_answering(file: UploadFile = File(...),
|
|
| 145 |
context = (res.get("generated_text") if isinstance(res, dict)
|
| 146 |
else str(res))
|
| 147 |
else:
|
| 148 |
-
context = process_uploaded_file(file)[:
|
| 149 |
|
| 150 |
if not context:
|
| 151 |
return {"result": "No context – cannot answer."}
|
|
|
|
| 20 |
app = FastAPI(
|
| 21 |
title = "AI-Powered Web-App API",
|
| 22 |
description = "Backend for summarisation, captioning & QA",
|
| 23 |
+
version = "1.2.3", # <-- bumped
|
| 24 |
)
|
| 25 |
|
| 26 |
app.add_middleware(
|
|
|
|
| 47 |
timeout = 120,
|
| 48 |
)
|
| 49 |
|
| 50 |
+
# ➜ Upgraded QA model (higher accuracy than roberta-base)
|
| 51 |
qa_client = InferenceClient(
|
| 52 |
+
"deepset/roberta-large-squad2",
|
| 53 |
token = HUGGINGFACE_TOKEN,
|
| 54 |
+
timeout = 120,
|
| 55 |
)
|
| 56 |
+
# If you need multilingual support, swap for:
|
| 57 |
+
# qa_client = InferenceClient("deepset/xlm-roberta-large-squad2",
|
| 58 |
+
# token=HUGGINGFACE_TOKEN, timeout=120)
|
| 59 |
|
| 60 |
image_caption_client = InferenceClient(
|
| 61 |
"nlpconnect/vit-gpt2-image-captioning",
|
|
|
|
| 114 |
# -------------------- Image Caption ------------------------------------------
|
| 115 |
@app.post("/api/caption")
|
| 116 |
async def caption_image(image: UploadFile = File(...)):
|
| 117 |
+
"""`image` field name matches frontend (was `file` before)."""
|
| 118 |
try:
|
| 119 |
img_bytes = await image.read()
|
| 120 |
img = Image.open(io.BytesIO(img_bytes)).convert("RGB")
|
|
|
|
| 149 |
context = (res.get("generated_text") if isinstance(res, dict)
|
| 150 |
else str(res))
|
| 151 |
else:
|
| 152 |
+
context = process_uploaded_file(file)[:3000]
|
| 153 |
|
| 154 |
if not context:
|
| 155 |
return {"result": "No context – cannot answer."}
|