Spaces:
Runtime error
Runtime error
from dotenv import load_dotenv | |
import streamlit as st | |
import os | |
import google.generativeai as genai | |
from PIL import Image | |
load_dotenv() | |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY")) | |
# Function to get response from Gemini model | |
def get_gemini_response(input_prompt, image_data, genre, length, language, mood): | |
model = genai.GenerativeModel('gemini-1.5-flash') | |
if image_data: | |
full_prompt = f""" | |
You are a famous creative writer. Look at the image provided and create a {length} {genre} in {language}. The {genre} should be {mood}. The {genre} should be based on the image and contain realistic and emotional elements. | |
""" | |
response = model.generate_content([full_prompt, image_data[0]]) | |
else: | |
full_prompt = f""" | |
You are a creative writer. Create a {length} {genre} in {language}. The {genre} should be {mood}. The {genre} should be based on the following prompt: | |
"{input_prompt}" | |
Make sure it contains realistic and emotional elements. | |
""" | |
response = model.generate_content([full_prompt]) | |
# Check if response is valid and return text | |
if response and response.parts: | |
return response.parts[0].text | |
else: | |
raise ValueError("Sorry, But please try a different combination of inputs.") | |
# Function to setup uploaded image | |
def input_image_setup(uploaded_file): | |
if uploaded_file is not None: | |
bytes_data = uploaded_file.getvalue() | |
image_parts = [ | |
{ | |
"mime_type": uploaded_file.type, | |
"data": bytes_data | |
} | |
] | |
return image_parts | |
else: | |
return None | |
# Initialize Streamlit app | |
st.set_page_config(page_title="Poetic Vision", page_icon=":pencil:") | |
# Main UI components | |
st.title("Story Maker App :pencil:") | |
st.markdown("### Upload your image or provide a prompt to create a Vision around that: ") | |
input_prompt = st.text_input("Input Prompt (optional):", placeholder="Enter your prompt here...") | |
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"]) | |
genre = st.selectbox("Select Genre:", ["story", "shayari", "sher", "poem", "quote"]) | |
length = st.selectbox("Select Length:", ["short", "long"]) | |
language = st.selectbox("Select Language:", ["English", "Hindi"]) | |
mood = st.selectbox("Select Mood:", ["emotional", "sad", "happy", "horror", "comedy", "romantic"]) | |
image = None | |
if uploaded_file is not None: | |
image = Image.open(uploaded_file) | |
st.image(image, caption="Uploaded Image", use_column_width=True) | |
submit = st.button("Generate") | |
# Handle submit action | |
if submit: | |
if uploaded_file or input_prompt: | |
try: | |
image_data = input_image_setup(uploaded_file) | |
response = get_gemini_response(input_prompt, image_data, genre, length, language, mood) | |
st.subheader("Generated Content:") | |
st.write(response) | |
except ValueError as e: | |
st.error(f"Error: {str(e)}") | |
else: | |
st.error("Please upload an image or provide a prompt.") | |
st.sidebar.title("About") | |
st.sidebar.info("This app uses AI to create stories, shayari, sher, ghazal, poems, or quotes based on an uploaded image or a provided prompt. " | |
"Upload an image or enter a prompt, and select the genre, length, language, and mood to generate your content.") | |