Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import requests
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import pyttsx3 # Text-to-speech (optional)
|
| 7 |
+
|
| 8 |
+
# --- Load hosted model ---
|
| 9 |
+
captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
| 10 |
+
|
| 11 |
+
# --- Caption & TTS function ---
|
| 12 |
+
def generate_caption_tts(image):
|
| 13 |
+
# If user uploads a URL
|
| 14 |
+
if isinstance(image, str):
|
| 15 |
+
image = Image.open(BytesIO(requests.get(image).content))
|
| 16 |
+
caption = captioner(image)[0]['generated_text']
|
| 17 |
+
|
| 18 |
+
# TTS (optional)
|
| 19 |
+
tts = pyttsx3.init()
|
| 20 |
+
tts.say(caption)
|
| 21 |
+
tts.runAndWait()
|
| 22 |
+
|
| 23 |
+
return caption
|
| 24 |
+
|
| 25 |
+
# --- Gradio interface ---
|
| 26 |
+
iface = gr.Interface(
|
| 27 |
+
fn=generate_caption_tts,
|
| 28 |
+
inputs=gr.Image(type="pil"),
|
| 29 |
+
outputs="text",
|
| 30 |
+
title="Image Captioning for Visually Impaired",
|
| 31 |
+
description="Upload any image and get a descriptive caption."
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
iface.launch()
|