Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,18 @@
|
|
| 1 |
import os
|
| 2 |
import shutil
|
| 3 |
import subprocess
|
|
|
|
|
|
|
| 4 |
|
|
|
|
|
|
|
| 5 |
subprocess.run(["git", "clone", "https://huggingface.co/irotem98/edge_vlm"])
|
|
|
|
| 6 |
subprocess.run(["pip", "install", "-r", "edge_vlm/requirements.txt"])
|
| 7 |
subprocess.run(["pip", "install", "sentencepiece"])
|
| 8 |
|
|
|
|
|
|
|
| 9 |
source_dir = "edge_vlm"
|
| 10 |
destination_dir = "."
|
| 11 |
|
|
@@ -13,44 +20,51 @@ for item in os.listdir(source_dir):
|
|
| 13 |
source_item = os.path.join(source_dir, item)
|
| 14 |
destination_item = os.path.join(destination_dir, item)
|
| 15 |
|
| 16 |
-
# If it's a directory, copy it recursively
|
| 17 |
if os.path.isdir(source_item):
|
| 18 |
if os.path.exists(destination_item):
|
| 19 |
-
shutil.rmtree(destination_item)
|
| 20 |
shutil.copytree(source_item, destination_item)
|
| 21 |
else:
|
| 22 |
-
# If it's a file, copy it
|
| 23 |
shutil.copy(source_item, destination_item)
|
| 24 |
|
|
|
|
|
|
|
| 25 |
# Now import the model from the copied files
|
| 26 |
from model import MoondreamModel
|
| 27 |
-
import torch
|
| 28 |
-
import gradio as gr
|
| 29 |
|
| 30 |
# Load the model and tokenizer
|
|
|
|
| 31 |
model = MoondreamModel.load_model()
|
|
|
|
|
|
|
| 32 |
tokenizer = MoondreamModel.load_tokenizer()
|
|
|
|
| 33 |
|
| 34 |
# Define the default question
|
| 35 |
default_question = "Describe the image."
|
| 36 |
|
| 37 |
# Function to handle image and return generated caption
|
| 38 |
def generate_caption_with_default(image):
|
| 39 |
-
|
| 40 |
preprocessed_image = MoondreamModel.preprocess_image(image)
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
caption = MoondreamModel.generate_caption(model, preprocessed_image, tokenizer)
|
|
|
|
| 44 |
|
| 45 |
return caption
|
| 46 |
|
|
|
|
|
|
|
| 47 |
interface = gr.Interface(
|
| 48 |
fn=generate_caption_with_default,
|
| 49 |
-
inputs=gr.Image(type="pil", label="Upload an Image"),
|
| 50 |
outputs="text",
|
| 51 |
title="Image Caption Generator",
|
| 52 |
description=f"The default question is: '{default_question}'. Upload an image to generate a description."
|
| 53 |
)
|
| 54 |
|
| 55 |
# Launch the interface
|
| 56 |
-
interface
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import shutil
|
| 3 |
import subprocess
|
| 4 |
+
import torch
|
| 5 |
+
import gradio as gr
|
| 6 |
|
| 7 |
+
# Clone and install dependencies
|
| 8 |
+
print("Cloning the repository...")
|
| 9 |
subprocess.run(["git", "clone", "https://huggingface.co/irotem98/edge_vlm"])
|
| 10 |
+
print("Installing dependencies...")
|
| 11 |
subprocess.run(["pip", "install", "-r", "edge_vlm/requirements.txt"])
|
| 12 |
subprocess.run(["pip", "install", "sentencepiece"])
|
| 13 |
|
| 14 |
+
# Copy all files from edge_vlm to current directory
|
| 15 |
+
print("Copying files...")
|
| 16 |
source_dir = "edge_vlm"
|
| 17 |
destination_dir = "."
|
| 18 |
|
|
|
|
| 20 |
source_item = os.path.join(source_dir, item)
|
| 21 |
destination_item = os.path.join(destination_dir, item)
|
| 22 |
|
|
|
|
| 23 |
if os.path.isdir(source_item):
|
| 24 |
if os.path.exists(destination_item):
|
| 25 |
+
shutil.rmtree(destination_item)
|
| 26 |
shutil.copytree(source_item, destination_item)
|
| 27 |
else:
|
|
|
|
| 28 |
shutil.copy(source_item, destination_item)
|
| 29 |
|
| 30 |
+
print("Files copied successfully.")
|
| 31 |
+
|
| 32 |
# Now import the model from the copied files
|
| 33 |
from model import MoondreamModel
|
|
|
|
|
|
|
| 34 |
|
| 35 |
# Load the model and tokenizer
|
| 36 |
+
print("Loading model...")
|
| 37 |
model = MoondreamModel.load_model()
|
| 38 |
+
print("Model loaded.")
|
| 39 |
+
print("Loading tokenizer...")
|
| 40 |
tokenizer = MoondreamModel.load_tokenizer()
|
| 41 |
+
print("Tokenizer loaded.")
|
| 42 |
|
| 43 |
# Define the default question
|
| 44 |
default_question = "Describe the image."
|
| 45 |
|
| 46 |
# Function to handle image and return generated caption
|
| 47 |
def generate_caption_with_default(image):
|
| 48 |
+
print("Preprocessing image...")
|
| 49 |
preprocessed_image = MoondreamModel.preprocess_image(image)
|
| 50 |
+
print("Image preprocessed.")
|
| 51 |
|
| 52 |
+
print("Generating caption...")
|
| 53 |
caption = MoondreamModel.generate_caption(model, preprocessed_image, tokenizer)
|
| 54 |
+
print("Caption generated.")
|
| 55 |
|
| 56 |
return caption
|
| 57 |
|
| 58 |
+
# Create Gradio interface
|
| 59 |
+
print("Setting up Gradio interface...")
|
| 60 |
interface = gr.Interface(
|
| 61 |
fn=generate_caption_with_default,
|
| 62 |
+
inputs=gr.Image(type="pil", label="Upload an Image"),
|
| 63 |
outputs="text",
|
| 64 |
title="Image Caption Generator",
|
| 65 |
description=f"The default question is: '{default_question}'. Upload an image to generate a description."
|
| 66 |
)
|
| 67 |
|
| 68 |
# Launch the interface
|
| 69 |
+
print("Launching interface...")
|
| 70 |
+
interface.launch()
|