Dileep7729
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -2,38 +2,44 @@ 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("
|
7 |
|
8 |
-
# Load the
|
9 |
processor = BlipProcessor.from_pretrained(
|
10 |
-
"quadranttechnologies/
|
11 |
use_auth_token=HUGGINGFACE_TOKEN
|
12 |
)
|
13 |
model = BlipForConditionalGeneration.from_pretrained(
|
14 |
-
"quadranttechnologies/
|
15 |
use_auth_token=HUGGINGFACE_TOKEN
|
16 |
)
|
17 |
|
18 |
-
#
|
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
|
34 |
)
|
35 |
|
|
|
36 |
if __name__ == "__main__":
|
37 |
interface.launch(share=True)
|
38 |
|
39 |
|
|
|
|
2 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
3 |
import gradio as gr
|
4 |
|
5 |
+
# Load the Hugging Face token from the environment using the secret name
|
6 |
+
HUGGINGFACE_TOKEN = os.getenv("Image_classification")
|
7 |
|
8 |
+
# Load the processor and model with the token
|
9 |
processor = BlipProcessor.from_pretrained(
|
10 |
+
"quadranttechnologies/qhub-blip-image-captioning-finetuned",
|
11 |
use_auth_token=HUGGINGFACE_TOKEN
|
12 |
)
|
13 |
model = BlipForConditionalGeneration.from_pretrained(
|
14 |
+
"quadranttechnologies/qhub-blip-image-captioning-finetuned",
|
15 |
use_auth_token=HUGGINGFACE_TOKEN
|
16 |
)
|
17 |
|
18 |
+
# Function to generate captions for uploaded images
|
19 |
def generate_caption(image):
|
20 |
try:
|
21 |
+
# Prepare the image inputs for the model
|
22 |
inputs = processor(image, return_tensors="pt")
|
23 |
+
|
24 |
+
# Generate the caption
|
25 |
outputs = model.generate(**inputs)
|
26 |
caption = processor.decode(outputs[0], skip_special_tokens=True)
|
27 |
return caption
|
28 |
except Exception as e:
|
29 |
return f"Error generating caption: {e}"
|
30 |
|
31 |
+
# Set up the Gradio interface
|
32 |
interface = gr.Interface(
|
33 |
fn=generate_caption,
|
34 |
+
inputs=gr.Image(type="pil"), # Accepts image uploads
|
35 |
+
outputs="text", # Displays generated captions as text
|
36 |
title="Image Captioning Model",
|
37 |
+
description="Upload an image to generate a caption using the fine-tuned BLIP model."
|
38 |
)
|
39 |
|
40 |
+
# Launch the Gradio app
|
41 |
if __name__ == "__main__":
|
42 |
interface.launch(share=True)
|
43 |
|
44 |
|
45 |
+
|