Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,22 @@
|
|
| 1 |
-
import
|
| 2 |
from PIL import Image
|
| 3 |
-
|
| 4 |
-
import torch as T
|
| 5 |
-
import transformers
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
tokenizer = transformers.AutoTokenizer.from_pretrained(PATH_LLAVA)
|
| 10 |
-
model = transformers.AutoModelForCausalLM.from_pretrained(PATH_LLAVA).cuda()
|
| 11 |
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
# Example: Generate a simple instruction based on the image
|
| 27 |
-
# This is a placeholder. You would replace this with your own method
|
| 28 |
-
# to analyze the image and generate a textual description or instruction.
|
| 29 |
-
instruction = "Describe what to do with this image."
|
| 30 |
-
|
| 31 |
-
# Simplify and generate expressive instruction
|
| 32 |
-
expressive_instruction = remove_alter(instruction)
|
| 33 |
-
print("Expressive Instruction:", expressive_instruction)
|
| 34 |
-
|
| 35 |
-
# Example usage
|
| 36 |
-
image_path = './path/to/your/image.jpg' # Update this path to your image
|
| 37 |
-
load_image_and_generate_instruction(image_path)
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
from PIL import Image
|
| 3 |
+
from transformers import pipeline
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# Initialize the pipeline with the image captioning model
|
| 6 |
+
caption_pipeline = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
def generate_caption(image):
|
| 9 |
+
# Convert the PIL Image to the format expected by the model
|
| 10 |
+
image = Image.open(image).convert("RGB")
|
| 11 |
+
|
| 12 |
+
# Use the pipeline to generate a caption
|
| 13 |
+
result = caption_pipeline(image)
|
| 14 |
+
caption = result[0]["generated_text"]
|
| 15 |
+
|
| 16 |
+
return caption
|
| 17 |
|
| 18 |
+
# Setup the Gradio interface
|
| 19 |
+
interface = gr.Interface(fn=generate_caption,
|
| 20 |
+
inputs=gr.inputs.Image(type="pil", label="Upload an Image"),
|
| 21 |
+
outputs=gr.outputs.Textbox(label="Generated Caption"))
|
| 22 |
+
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|