Spaces:
Sleeping
Sleeping
File size: 4,889 Bytes
67d6f5b 0d42798 67d6f5b 0d42798 87c26ed 67d6f5b 8578c0e 67d6f5b 0d42798 67d6f5b 0d42798 67d6f5b 0d42798 67d6f5b 0d42798 67d6f5b 0d42798 8578c0e 67d6f5b 0d42798 87c26ed 0d42798 f2f1ffa 0d42798 67d6f5b 0d42798 67d6f5b 0d42798 8678e61 0d42798 67d6f5b 0d42798 67d6f5b 0d42798 |
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
import base64
import logging
from typing import List, Optional
from fastapi import Depends, FastAPI, File, Form, HTTPException, UploadFile, status
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from pydantic import AnyHttpUrl, BaseModel, UrlConstraints
from contextlib import asynccontextmanager
from PIL import Image
from config import get_settings
import uvicorn
from utils.audio_utils import AudioUtils
from utils.caption_utils import ImageCaptioning
from utils.image_utils import UrlTest
from utils.topic_generation import TopicGenerator
# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Pydantic models for request/response
class TopicResponse(BaseModel):
topics: List[str]
caption: Optional[str]
class AudioResponse(BaseModel):
audio_base64: str
class TranscriptionResponse(BaseModel):
audio_transcription: str
# Context manager for startup and shutdown events
@asynccontextmanager
async def lifespan(app: FastAPI):
# Startup
app.state.topic_generator = TopicGenerator()
app.state.img_caption = ImageCaptioning()
app.state.audio_utils = AudioUtils()
app.state.url_utils = UrlTest()
logger.info("Application startup complete")
yield
# Shutdown
logger.info("Application shutdown")
app = FastAPI(
title="Rediones API",
lifespan=lifespan,
)
# CORS
@app.on_event("startup")
async def startup_event():
settings = get_settings()
if settings.ALLOWED_ORIGINS:
app.add_middleware(
CORSMiddleware,
allow_origins=settings.ALLOWED_ORIGINS,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
async def root():
return {"message": "Welcome To Rediones API"}
@app.get("/health")
async def health():
return {"status": "OK"}
@app.post("/topicgen", response_model=TopicResponse)
async def generate_topic(
img: UploadFile = File(None),
text: Optional[str] = Form(None),
img_url: Optional[AnyHttpUrl] = Form(None)
):
try:
if img_url and img:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Only one of image_url or img can be accepted"
)
if text and not (img or img_url):
generated_topics = app.state.topic_generator.generate_topics(text)
return TopicResponse(topics=generated_topics, caption=None)
if img or img_url:
img_file_object = None
if img:
if not img.filename.lower().endswith((".jpg", ".png", ".jpeg")):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Image file must be ended with .jpg, .png, .jpeg"
)
img_file_object = Image.open(img.file)
elif img_url:
img_file_object = app.state.url_utils.load_image(img_url)
capt = app.state.img_caption.combo_model(img_file_object, text)
print(capt)
return TopicResponse(topics=capt["topics"], caption=capt["caption"])
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Enter text or image. Image URL and image file are mutually exclusive."
)
except Exception as e:
logger.error(f"Error in generate_topic: {str(e)}")
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="An unexpected error occurred")
@app.post("/audioverse", response_model=AudioResponse)
async def generate_audio(text: str):
try:
audio_bytes = app.state.audio_utils.speak(text)
audio_base64 = base64.b64encode(audio_bytes).decode("utf-8")
return AudioResponse(audio_base64=audio_base64)
except Exception as e:
logger.error(f"Error in generate_audio: {str(e)}")
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="An unexpected error occurred")
@app.post("/transcribe", response_model=TranscriptionResponse)
async def transcribe_audio(
audio: UploadFile = File(..., description="Audio file to be transcribed.")
):
try:
audio_transcribe = app.state.audio_utils.improved_transcribe(0.8, audio_file=audio.file)
return TranscriptionResponse(audio_transcription=audio_transcribe)
except Exception as e:
logger.error(f"Error in transcribe_audio: {str(e)}")
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="An unexpected error occurred")
if __name__ == "__main__":
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True) |