Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +87 -0
- requirements.txt +9 -0
app.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dotenv import load_dotenv
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import os
|
| 4 |
+
import google.generativeai as genai
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
load_dotenv()
|
| 8 |
+
|
| 9 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 10 |
+
|
| 11 |
+
# Function to get response from Gemini model
|
| 12 |
+
def get_gemini_response(input_prompt, image_data, genre, length, language, mood):
|
| 13 |
+
model = genai.GenerativeModel('gemini-pro-vision')
|
| 14 |
+
if image_data:
|
| 15 |
+
full_prompt = f"""
|
| 16 |
+
You are a 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.
|
| 17 |
+
"""
|
| 18 |
+
response = model.generate_content([full_prompt, image_data[0]])
|
| 19 |
+
else:
|
| 20 |
+
full_prompt = f"""
|
| 21 |
+
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:
|
| 22 |
+
|
| 23 |
+
"{input_prompt}"
|
| 24 |
+
|
| 25 |
+
Make sure it contains realistic and emotional elements.
|
| 26 |
+
"""
|
| 27 |
+
response = model.generate_content([full_prompt])
|
| 28 |
+
|
| 29 |
+
# Check if response is valid and return text
|
| 30 |
+
if response and response.parts:
|
| 31 |
+
return response.parts[0].text
|
| 32 |
+
else:
|
| 33 |
+
raise ValueError("Sorry, But please try a different combination of inputs.")
|
| 34 |
+
|
| 35 |
+
# Function to setup uploaded image
|
| 36 |
+
def input_image_setup(uploaded_file):
|
| 37 |
+
if uploaded_file is not None:
|
| 38 |
+
bytes_data = uploaded_file.getvalue()
|
| 39 |
+
image_parts = [
|
| 40 |
+
{
|
| 41 |
+
"mime_type": uploaded_file.type,
|
| 42 |
+
"data": bytes_data
|
| 43 |
+
}
|
| 44 |
+
]
|
| 45 |
+
return image_parts
|
| 46 |
+
else:
|
| 47 |
+
return None
|
| 48 |
+
|
| 49 |
+
# Initialize Streamlit app
|
| 50 |
+
st.set_page_config(page_title="Poetic Vision", page_icon=":pencil:")
|
| 51 |
+
|
| 52 |
+
# Main UI components
|
| 53 |
+
st.title("Story Maker App :pencil:")
|
| 54 |
+
st.markdown("### Upload your image or provide a prompt to create a Vision around that: ")
|
| 55 |
+
|
| 56 |
+
input_prompt = st.text_input("Input Prompt (optional):", placeholder="Enter your prompt here...")
|
| 57 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 58 |
+
|
| 59 |
+
genre = st.selectbox("Select Genre:", ["story", "shayari", "sher", "poem", "quote"])
|
| 60 |
+
length = st.selectbox("Select Length:", ["short", "long"])
|
| 61 |
+
language = st.selectbox("Select Language:", ["English", "Hindi"])
|
| 62 |
+
mood = st.selectbox("Select Mood:", ["emotional", "sad", "happy", "horror", "comedy", "romantic"])
|
| 63 |
+
|
| 64 |
+
image = None
|
| 65 |
+
|
| 66 |
+
if uploaded_file is not None:
|
| 67 |
+
image = Image.open(uploaded_file)
|
| 68 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 69 |
+
|
| 70 |
+
submit = st.button("Generate")
|
| 71 |
+
|
| 72 |
+
# Handle submit action
|
| 73 |
+
if submit:
|
| 74 |
+
if uploaded_file or input_prompt:
|
| 75 |
+
try:
|
| 76 |
+
image_data = input_image_setup(uploaded_file)
|
| 77 |
+
response = get_gemini_response(input_prompt, image_data, genre, length, language, mood)
|
| 78 |
+
st.subheader("Generated Content:")
|
| 79 |
+
st.write(response)
|
| 80 |
+
except ValueError as e:
|
| 81 |
+
st.error(f"Error: {str(e)}")
|
| 82 |
+
else:
|
| 83 |
+
st.error("Please upload an image or provide a prompt.")
|
| 84 |
+
|
| 85 |
+
st.sidebar.title("About")
|
| 86 |
+
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. "
|
| 87 |
+
"Upload an image or enter a prompt, and select the genre, length, language, and mood to generate your content.")
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
google-generativeai
|
| 3 |
+
python-dotenv
|
| 4 |
+
langchain
|
| 5 |
+
PyPDF2
|
| 6 |
+
chromadb
|
| 7 |
+
pdf2image
|
| 8 |
+
faiss-cpu
|
| 9 |
+
langchain_google_genai
|