Im-prmpt / app.py
mrbeliever's picture
Update app.py
c7bca57 verified
raw
history blame
2.87 kB
import streamlit as st
import requests
import os
import json
# Set the Nebius API key
API_KEY = os.environ.get("NEBIUS_API_KEY")
API_URL = "https://api.studio.nebius.ai/v1/chat/completions"
# Streamlit app configuration
st.set_page_config(page_title="Image to Prompt Converter", layout="centered", page_icon="🖼️")
# Apply custom styles
st.markdown(
"""
<style>
body {
background: linear-gradient(135deg, #1e3c72, #2a5298);
color: #FFFFFF;
font-family: 'Arial', sans-serif;
}
.stApp {
align-items: center;
justify-content: center;
}
img {
border-radius: 10px;
margin-bottom: 20px;
max-width: 100%;
height: auto;
}
h1, h2, h3 {
text-align: center;
}
</style>
""",
unsafe_allow_html=True
)
# App title
st.title("Image to Prompt Converter")
st.markdown("**Upload an image and generate a detailed prompt.**")
# Image upload
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
if uploaded_file is not None:
# Display the uploaded image
st.image(uploaded_file, caption="Uploaded Image", use_container_width=True)
# Generate button
if st.button("Generate Prompt"):
# Prepare the API payload
files = {"file": uploaded_file.getvalue()}
headers = {"Authorization": f"Bearer {API_KEY}"}
data = {
"model": "Qwen/Qwen2-VL-7B-Instruct",
"messages": [
{
"role": "system",
"content": """You are an image to prompt converter. Your work is to observe each and every detail of the image and craft a detailed prompt under 75 words in this format: [image content/subject, description of action, state, and mood], [art form, style], [artist/photographer reference if needed], [additional settings such as camera and lens settings, lighting, colors, effects, texture, background, rendering]."""
}
],
"temperature": 1
}
# Call the Nebius API
response = requests.post(API_URL, headers=headers, data=json.dumps(data))
if response.status_code == 200:
# Extract the generated prompt
result = response.json()
generated_prompt = result.get("choices", [{}])[0].get("message", {}).get("content", "No prompt generated.")
# Display the generated prompt
st.subheader("Generated Prompt")
st.text_area("", generated_prompt, height=200)
# Copy button
if st.button("Copy Prompt"):
st.write("Copy functionality is not supported in this environment. Please manually copy the text.")
else:
st.error(f"Failed to generate prompt: {response.status_code} - {response.text}")