Spaces:
Running
Running
Update app.py
Browse filesupdate tampilan
app.py
CHANGED
@@ -1,33 +1,71 @@
|
|
1 |
import streamlit as st
|
2 |
import os
|
3 |
from PIL import Image
|
4 |
-
|
5 |
import google.generativeai as genai
|
|
|
|
|
6 |
secret_key = os.getenv("SECRET_KEY")
|
7 |
genai.configure(api_key=secret_key)
|
8 |
|
|
|
9 |
def get_gemini_response(image):
|
10 |
-
model = genai.GenerativeModel('gemini-
|
11 |
-
input='''You are a prompt generator.You will get an image.
|
12 |
Your work is to write a prompt such that an image generator model would create most identical picture
|
13 |
as the image given to you'''
|
14 |
-
response = model.generate_content([input,image])
|
15 |
return response.text
|
16 |
|
17 |
-
|
18 |
-
st.
|
|
|
|
|
19 |
|
20 |
-
|
|
|
21 |
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
if uploaded_file is not None:
|
25 |
-
|
26 |
-
|
|
|
|
|
27 |
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import os
|
3 |
from PIL import Image
|
|
|
4 |
import google.generativeai as genai
|
5 |
+
|
6 |
+
# Konfigurasi API Gemini
|
7 |
secret_key = os.getenv("SECRET_KEY")
|
8 |
genai.configure(api_key=secret_key)
|
9 |
|
10 |
+
# Fungsi untuk menghasilkan prompt dari gambar
|
11 |
def get_gemini_response(image):
|
12 |
+
model = genai.GenerativeModel('gemini-2.0-flash')
|
13 |
+
input = '''You are a prompt generator. You will get an image.
|
14 |
Your work is to write a prompt such that an image generator model would create most identical picture
|
15 |
as the image given to you'''
|
16 |
+
response = model.generate_content([input, image])
|
17 |
return response.text
|
18 |
|
19 |
+
# Konfigurasi halaman
|
20 |
+
st.set_page_config(page_title="Prompt Generation from Image", layout="wide")
|
21 |
+
|
22 |
+
st.title("🖼️ Prompt Generator from Image")
|
23 |
|
24 |
+
# Membuat dua kolom
|
25 |
+
col1, col2 = st.columns(2)
|
26 |
|
27 |
+
with col1:
|
28 |
+
st.markdown("### 📤 Upload Your Image")
|
29 |
+
uploaded_file = st.file_uploader(
|
30 |
+
"Drag and drop or click to upload an image...",
|
31 |
+
type=["jpg", "jpeg", "png", "webp"],
|
32 |
+
label_visibility="collapsed"
|
33 |
+
)
|
34 |
|
35 |
+
if uploaded_file is not None:
|
36 |
+
image = Image.open(uploaded_file)
|
37 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
38 |
+
else:
|
39 |
+
image = None
|
40 |
|
41 |
+
with col2:
|
42 |
+
st.markdown("### 🎯 Generated Prompt")
|
43 |
+
if uploaded_file is not None:
|
44 |
+
if st.button("✨ Generate Prompt"):
|
45 |
+
with st.spinner("Generating prompt..."):
|
46 |
+
prompt = get_gemini_response(image)
|
47 |
+
st.code(prompt, language="markdown")
|
48 |
|
49 |
+
# Tombol copy dengan interaksi
|
50 |
+
st.markdown(f"""
|
51 |
+
<button id="copyButton"
|
52 |
+
onclick="copyToClipboard()"
|
53 |
+
style="background-color:#4CAF50;color:white;padding:8px 12px;
|
54 |
+
border:none;border-radius:4px;cursor:pointer;margin-top:10px;">
|
55 |
+
📋 Copy Prompt
|
56 |
+
</button>
|
57 |
+
<script>
|
58 |
+
function copyToClipboard() {{
|
59 |
+
const text = `{prompt}`;
|
60 |
+
navigator.clipboard.writeText(text).then(function() {{
|
61 |
+
const btn = document.getElementById("copyButton");
|
62 |
+
btn.innerHTML = "✅ Copied!";
|
63 |
+
setTimeout(() => {{
|
64 |
+
btn.innerHTML = "📋 Copy Prompt";
|
65 |
+
}}, 2000);
|
66 |
+
}});
|
67 |
+
}}
|
68 |
+
</script>
|
69 |
+
""", unsafe_allow_html=True)
|
70 |
+
else:
|
71 |
+
st.info("Please upload an image to generate a prompt.")
|