Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
4 |
+
import torch
|
5 |
+
|
6 |
+
# Load model and processor
|
7 |
+
@st.cache_resource
|
8 |
+
def load_model():
|
9 |
+
model_name = "your-huggingface-username/your-model-name"
|
10 |
+
processor = BlipProcessor.from_pretrained(model_name)
|
11 |
+
model = BlipForConditionalGeneration.from_pretrained(model_name)
|
12 |
+
return processor, model
|
13 |
+
|
14 |
+
processor, model = load_model()
|
15 |
+
|
16 |
+
# Streamlit UI
|
17 |
+
st.title("Cartoon Caption Generator 🖼️📜")
|
18 |
+
st.write("Upload a cartoon image and get a funny caption!")
|
19 |
+
|
20 |
+
uploaded_file = st.file_uploader("Upload a Cartoon Image", type=["jpg", "png", "jpeg"])
|
21 |
+
|
22 |
+
if uploaded_file is not None:
|
23 |
+
image = Image.open(uploaded_file).convert("RGB")
|
24 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
25 |
+
|
26 |
+
# Preprocess and generate caption
|
27 |
+
inputs = processor(images=image, return_tensors="pt")
|
28 |
+
with torch.no_grad():
|
29 |
+
generated_ids = model.generate(**inputs)
|
30 |
+
caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
31 |
+
|
32 |
+
st.subheader("Generated Caption:")
|
33 |
+
st.write(f"💬 {caption}")
|