Spaces:
Running
Running
Felix Konrad
commited on
Commit
·
e5e6c04
1
Parent(s):
115893a
First draft of basic layout + model loading.
Browse files
app.py
CHANGED
|
@@ -1,7 +1,57 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 7 |
demo.launch()
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
+
from transformers import AutoModel, AutoImageProcessor
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import torch
|
| 6 |
|
| 7 |
+
# Global state to store loaded model + processor
|
| 8 |
+
state = {
|
| 9 |
+
"model": None,
|
| 10 |
+
"processor": None,
|
| 11 |
+
"repo_id": None,
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
def load_model(repo_id: str, revision: str = None):
|
| 15 |
+
"""
|
| 16 |
+
Load a Hugging Face model and processor from a repo ID.
|
| 17 |
+
"""
|
| 18 |
+
try:
|
| 19 |
+
model = AutoModel.from_pretrained(repo_id, revision=revision, trust_remote_code=False)
|
| 20 |
+
processor = AutoImageProcessor.from_pretrained(repo_id, revision=revision, trust_remote_code=False)
|
| 21 |
+
# Move model to CPU/GPU if needed
|
| 22 |
+
if torch.cuda.is_available():
|
| 23 |
+
model.to("cuda")
|
| 24 |
+
else:
|
| 25 |
+
model.to("cpu")
|
| 26 |
+
# Store in global state
|
| 27 |
+
state["model"] = model
|
| 28 |
+
state["processor"] = processor
|
| 29 |
+
state["repo_id"] = repo_id
|
| 30 |
+
return f"Successfully loaded model '{repo_id}'"
|
| 31 |
+
except Exception as e:
|
| 32 |
+
return f"Error loading model: {e}"
|
| 33 |
+
|
| 34 |
+
def display_image(image: Image):
|
| 35 |
+
"""
|
| 36 |
+
Simply returns the uploaded image (you can process it later).
|
| 37 |
+
"""
|
| 38 |
+
return image
|
| 39 |
+
|
| 40 |
+
# Build the Gradio interface
|
| 41 |
+
with gr.Blocks() as demo:
|
| 42 |
+
gr.Markdown("# Dynamic ViT Loader Template")
|
| 43 |
+
|
| 44 |
+
with gr.Row():
|
| 45 |
+
repo_input = gr.Textbox(label="Hugging Face model repo ID", placeholder="e.g. google/vit-base-patch16-224")
|
| 46 |
+
revision_input = gr.Textbox(label="Revision (optional)", placeholder="branch, tag, or commit hash")
|
| 47 |
+
load_btn = gr.Button("Load Model")
|
| 48 |
+
load_status = gr.Textbox(label="Model Status", interactive=False)
|
| 49 |
+
|
| 50 |
+
image_input = gr.Image(type="pil", label="Upload Image")
|
| 51 |
+
image_output = gr.Image(label="Displayed Image")
|
| 52 |
+
|
| 53 |
+
# Button clicks / image upload handlers
|
| 54 |
+
load_btn.click(fn=load_model, inputs=[repo_input, revision_input], outputs=load_status)
|
| 55 |
+
image_input.change(fn=display_image, inputs=image_input, outputs=image_output)
|
| 56 |
|
|
|
|
| 57 |
demo.launch()
|