Spaces:
Running
Running
File size: 1,639 Bytes
059d3f3 9a30172 059d3f3 428d9b9 059d3f3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# app/main.py
from fastapi import FastAPI, File, UploadFile, status
from PIL import Image
import io
from .ai_core import get_image_caption, generate_story_from_caption
app = FastAPI(
title="Visionary Verse API",
description="An AI service that generates fantasy stories inspired by an image."
)
@app.get("/health", status_code=status.HTTP_200_OK)
def perform_health_check():
"""
Performs a health check on the application.
It returns a 200 OK status if the application is running.
This endpoint is used by the hosting platform to verify service health.
"""
return {"status": "ok"}
@app.get("/")
def read_root():
"""A simple root endpoint to check if the API is running."""
return {"message": "Welcome to the Visionary Verse API! Go to /docs to test."}
@app.post("/generate")
async def generate_text_from_image(file: UploadFile = File(...)):
"""
Generates a story from an uploaded image file.
- **file**: The image file to upload (e.g., jpg, png).
"""
contents = await file.read()
try:
uploaded_image = Image.open(io.BytesIO(contents)).convert("RGB")
except Exception:
return {"error": "Invalid image file. Please upload a file in jpg or png format."}
image_caption = get_image_caption(uploaded_image)
if "Could not generate" in image_caption:
return {"error": "Could not understand the content of the image."}
generated_story = generate_story_from_caption(image_caption)
return {
"input_image_filename": file.filename,
"generated_caption": image_caption,
"generated_story": generated_story
} |