Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,39 @@
|
|
| 1 |
-
import
|
| 2 |
-
import requests
|
| 3 |
-
from PIL import Image
|
| 4 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
|
|
|
| 5 |
|
| 6 |
-
# Load
|
| 7 |
-
|
| 8 |
-
model = BlipForConditionalGeneration.from_pretrained("quadranttechnologies/Imageclassification")
|
| 9 |
|
| 10 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def generate_caption(image):
|
| 12 |
try:
|
| 13 |
-
# Convert the image into the required format for the model
|
| 14 |
inputs = processor(image, return_tensors="pt")
|
| 15 |
-
|
| 16 |
-
# Generate caption
|
| 17 |
outputs = model.generate(**inputs)
|
| 18 |
caption = processor.decode(outputs[0], skip_special_tokens=True)
|
| 19 |
return caption
|
| 20 |
except Exception as e:
|
| 21 |
return f"Error generating caption: {e}"
|
| 22 |
|
| 23 |
-
# Set up Gradio interface for image upload and caption generation
|
| 24 |
interface = gr.Interface(
|
| 25 |
fn=generate_caption,
|
| 26 |
-
inputs=gr.Image(type="pil"),
|
| 27 |
-
outputs="text",
|
| 28 |
title="Image Captioning Model",
|
| 29 |
description="Upload an image to receive a caption generated by the model."
|
| 30 |
)
|
| 31 |
|
| 32 |
-
# Launch the Gradio app
|
| 33 |
if __name__ == "__main__":
|
| 34 |
-
interface.launch(share=True)
|
|
|
|
| 35 |
|
|
|
|
| 1 |
+
import os
|
|
|
|
|
|
|
| 2 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 3 |
+
import gradio as gr
|
| 4 |
|
| 5 |
+
# Load the token from the environment
|
| 6 |
+
HUGGINGFACE_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
|
|
|
|
| 7 |
|
| 8 |
+
# Load the model and processor with the token
|
| 9 |
+
processor = BlipProcessor.from_pretrained(
|
| 10 |
+
"quadranttechnologies/Imageclassification",
|
| 11 |
+
use_auth_token=HUGGINGFACE_TOKEN
|
| 12 |
+
)
|
| 13 |
+
model = BlipForConditionalGeneration.from_pretrained(
|
| 14 |
+
"quadranttechnologies/Imageclassification",
|
| 15 |
+
use_auth_token=HUGGINGFACE_TOKEN
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
# Define your Gradio interface and logic as before
|
| 19 |
def generate_caption(image):
|
| 20 |
try:
|
|
|
|
| 21 |
inputs = processor(image, return_tensors="pt")
|
|
|
|
|
|
|
| 22 |
outputs = model.generate(**inputs)
|
| 23 |
caption = processor.decode(outputs[0], skip_special_tokens=True)
|
| 24 |
return caption
|
| 25 |
except Exception as e:
|
| 26 |
return f"Error generating caption: {e}"
|
| 27 |
|
|
|
|
| 28 |
interface = gr.Interface(
|
| 29 |
fn=generate_caption,
|
| 30 |
+
inputs=gr.Image(type="pil"),
|
| 31 |
+
outputs="text",
|
| 32 |
title="Image Captioning Model",
|
| 33 |
description="Upload an image to receive a caption generated by the model."
|
| 34 |
)
|
| 35 |
|
|
|
|
| 36 |
if __name__ == "__main__":
|
| 37 |
+
interface.launch(share=True)
|
| 38 |
+
|
| 39 |
|