Spaces:
Running
Running
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +64 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,66 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
""
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
"
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
|
| 6 |
+
# --- Config ---
|
| 7 |
+
st.set_page_config(page_title="Image → Prompt", page_icon="🖼️", layout="centered")
|
| 8 |
+
|
| 9 |
+
# Laad model (gecached)
|
| 10 |
+
@st.cache_resource
|
| 11 |
+
def load_model():
|
| 12 |
+
return pipeline(
|
| 13 |
+
"image-to-text",
|
| 14 |
+
model="llava-hf/llava-v1.6-34b-hf",
|
| 15 |
+
torch_dtype=torch.float16,
|
| 16 |
+
device_map="auto"
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
pipe = load_model()
|
| 20 |
+
|
| 21 |
+
# Stijlen
|
| 22 |
+
STIJLEN = {
|
| 23 |
+
"Realistisch": ", fotorealistisch, 8k, scherp, natuurlijk licht",
|
| 24 |
+
"Anime": ", anime stijl, gedetailleerde ogen, Studio Ghibli, 4k",
|
| 25 |
+
"Olieverf": ", olieverf op doek, impressionistisch, Van Gogh stijl",
|
| 26 |
+
"Cyberpunk": ", cyberpunk, neon lichten, regenachtige straten, Blade Runner",
|
| 27 |
+
"Geen stijl": ""
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
# --- UI ---
|
| 31 |
+
st.title("🖼️ Image → Prompt Generator")
|
| 32 |
+
st.markdown("*Upload een foto, kies een stijl, krijg een perfecte AI-prompt.*")
|
| 33 |
+
|
| 34 |
+
col1, col2 = st.columns([1, 1])
|
| 35 |
+
with col1:
|
| 36 |
+
uploaded_file = st.file_uploader("Kies een afbeelding", type=["png", "jpg", "jpeg"])
|
| 37 |
+
with col2:
|
| 38 |
+
stijl = st.selectbox("Kies een stijl", options=list(STIJLEN.keys()))
|
| 39 |
+
|
| 40 |
+
if uploaded_file:
|
| 41 |
+
image = Image.open(uploaded_file).convert("RGB")
|
| 42 |
+
st.image(image, caption="Jouw afbeelding", use_column_width=True)
|
| 43 |
+
|
| 44 |
+
with st.spinner("Prompt genereren..."):
|
| 45 |
+
result = pipe(image)
|
| 46 |
+
raw_prompt = result[0]["generated_text"]
|
| 47 |
+
|
| 48 |
+
# Optimaliseer
|
| 49 |
+
final_prompt = (
|
| 50 |
+
raw_prompt
|
| 51 |
+
.replace("a photo of", "")
|
| 52 |
+
.replace("an image of", "")
|
| 53 |
+
.strip()
|
| 54 |
+
.capitalize()
|
| 55 |
+
)
|
| 56 |
+
final_prompt += STIJLEN[stijl]
|
| 57 |
+
|
| 58 |
+
st.success("**Prompt:**")
|
| 59 |
+
st.code(final_prompt, language=None)
|
| 60 |
+
|
| 61 |
+
st.download_button(
|
| 62 |
+
"📥 Download prompt",
|
| 63 |
+
final_prompt,
|
| 64 |
+
file_name="prompt.txt",
|
| 65 |
+
mime="text/plain"
|
| 66 |
+
)
|