File size: 940 Bytes
297dd8a
 
 
0000b07
5e640af
0000b07
5e640af
 
297dd8a
 
 
 
 
5e640af
297dd8a
 
5e640af
297dd8a
5e640af
297dd8a
 
 
 
5e640af
297dd8a
 
 
 
 
 
 
 
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
# appImage.py
from transformers import pipeline
import tempfile, os
from PIL import Image
from gtts import gTTS

captioner = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")

async def caption_image(file):
    contents = await file.read()
    with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp:
        tmp.write(contents)
        image_path = tmp.name

    captions = captioner(image_path)
    caption = captions[0]['generated_text'] if captions else "No caption generated."

    audio_path = text_to_speech(caption)

    result = {"caption": caption}
    if audio_path:
        result["audioUrl"] = f"/files/{os.path.basename(audio_path)}"
    return result

def text_to_speech(text: str):
    try:
        tts = gTTS(text)
        temp_audio = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
        tts.save(temp_audio.name)
        return temp_audio.name
    except:
        return ""