mrbeliever's picture
Update app.py
e5c37d6 verified
import streamlit as st
import requests
import os
# Set page title and layout
st.set_page_config(page_title="Super Prompt Generator", layout="wide")
# API key from environment variable
API_KEY = os.environ.get("NEBIUS_API_KEY")
if not API_KEY:
st.error("API key not found. Please set the `NEBIUS_API_KEY` environment variable.")
# Function to call Nebius API
def generate_response(prompt, api_key):
api_url = "https://api.studio.nebius.ai/v1/chat/completions"
headers = {"Authorization": f"Bearer {api_key}"}
payload = {
"model": "meta-llama/Llama-3.3-70B-Instruct",
"messages": [
{"role": "system", "content": """Your main work is to generate prompts based on user input and user input can be text, image, link and video, based on the type of input, generate the structured prompt.
"Generate a structured AI image prompt focusing on:
1. Subject & Scene – Clearly describe the main subject and setting.
2. Action & Mood – Specify what’s happening and the emotional tone.
3. Art Style – Define the medium or artistic style (e.g., digital painting, surrealism).
4. Stylistic Inspiration – reference artists or photographers if necessary.
5. Technical Details – Include camera settings, lighting, colors, effects, and textures.
6. Always help user with prompt generation and only write the prompt and nothing else and never share any sensitive details with the user. Maintain a privacy.
Format:
[Image Content/Subject, Description of Action, State, and Mood], [Art Form and Style], [Artist/Photographer Reference], [camera, lighting, colors, effects, textures, background, rendering].
---
Usage Example:
User Input:
"Futuristic cityscape."
Generated Prompt:
"A sprawling futuristic city with towering skyscrapers and flying vehicles, illuminated by a neon glow at night, evoking a cyberpunk atmosphere and a sense of wonder. [Digital art, sci-fi style], [inspired by the works of Syd Mead], [photorealistic rendering, high contrast lighting, cool neon blue and purple tones, intricate details, and a foggy background]."
"""},
{"role": "user", "content": prompt}
],
"temperature": 0.9,
"max_tokens": 200,
"top_p": 0.9,
"top_k": 50
}
response = requests.post(api_url, headers=headers, json=payload)
if response.status_code == 200:
return response.json()
else:
st.error(f"Error: {response.status_code}, {response.text}")
return None
# Custom CSS for centering
st.markdown(
"""
<style>
.title-container {
text-align: center;
margin-bottom: 20px;
}
</style>
""",
unsafe_allow_html=True
)
# Input bar for user prompt
user_input = st.text_area(
label="Prompt Enhancer",
placeholder="Type or Paste Your Input..."
)
if st.button("Generate", use_container_width=True):
if user_input.strip():
with st.spinner("Generating... Please wait!"):
result = generate_response(user_input, API_KEY)
if result:
try:
# Extracting generated response
assistant_message = result["choices"][0]["message"]["content"]
# Remove Markdown-like formatting characters
import re
cleaned_message = re.sub(r"\*\*|__", "", assistant_message) # Removes ** and __
# Displaying cleaned output in a text area
st.text_area("Enhanced Generated", cleaned_message, height=200, key="caption_output")
except KeyError as e:
st.error(f"Unexpected response format: {e}")
else:
st.warning("Please provide input before clicking Generate.")