OtakuOracle / app.py
iSathyam03's picture
Create app.py
1bd030e verified
from dotenv import load_dotenv
import streamlit as st
import os
import google.generativeai as genai
from PIL import Image
# Configure page
st.set_page_config(page_title="OtakuOracle ๐ŸŒŸ", layout="wide")
# Custom CSS for styling
st.markdown("""
<style>
.stApp {
background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
color: #ffffff;
}
.stButton button {
background-color: #ff6b6b;
color: white;
border-radius: 10px;
transition: all 0.3s ease;
}
.stButton button:hover {
background-color: #ff8787;
transform: translateY(-2px);
box-shadow: 0 5px 15px rgba(255, 107, 107, 0.4);
}
.sidebar .sidebar-content {
background: rgba(26, 26, 46, 0.9);
}
.stTextInput input {
border-radius: 8px;
border: 2px solid #4a90e2;
}
.css-1d391kg {
background-color: rgba(255, 255, 255, 0.05);
border-radius: 15px;
padding: 20px;
margin: 10px 0;
}
</style>
""", unsafe_allow_html=True)
# API Key input in sidebar
api_key = st.sidebar.text_input("Enter Google API Key ๐Ÿ”‘", type="password")
if not api_key:
st.warning("โš ๏ธ Please enter your Google API Key to use the app")
st.stop()
# Configure Gemini with provided API key
genai.configure(api_key=api_key)
def get_anime_recommendations(anime_title):
query = f"Give me 5 anime recommendations similar to {anime_title} with the following details: Anime, Genre, Main Protagonists, Summary."
response = chat.send_message(query, stream=True)
full_response = ""
for chunk in response:
full_response += chunk.text
return full_response
def format_anime_recommendation(text):
recommendations = text.split('\n\n')
formatted_recommendations = []
for rec in recommendations:
lines = rec.split('\n')
if len(lines) >= 4:
anime = lines[0].replace("Anime:", "").strip()
genre = lines[1].replace("Genre:", "").strip()
protagonists = lines[2].replace("Main Protagonists:", "").strip()
summary = lines[3].replace("Summary:", "").strip()
formatted_recommendations.append({
"Anime": anime,
"Genre": genre,
"Main Protagonists": protagonists,
"Summary": summary
})
return formatted_recommendations
def get_anime_character_response(character, question):
query = f"Respond to the following question in the style of the anime character {character}: {question}"
response = chat.send_message(query, stream=True)
full_response = ""
for chunk in response:
full_response += chunk.text
return full_response
def get_gemini_response(input, image, prompt):
model = genai.GenerativeModel('gemini-1.5-flash')
response = model.generate_content([input, image[0], prompt])
return response.text
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:
raise FileNotFoundError("No file uploaded")
def load_animation():
with st.spinner('๐ŸŒŸ Processing...'):
st.balloons()
# Initialize the Gemini Pro model
chat = genai.GenerativeModel("gemini-1.5-flash-002")
chat = chat.start_chat(history=[])
# Sidebar navigation
st.sidebar.markdown("## Navigation Menu ๐ŸŽฎ")
page = st.sidebar.selectbox("Select Service",
["Anime Recommender ๐ŸŒŸ", "Talk to Anime Character ๐Ÿ—ฃ๏ธ", "Anime Face Detect ๐Ÿ‘€"])
# Anime Recommender Page
if page == "Anime Recommender ๐ŸŒŸ":
st.markdown("# ๐ŸŽฌ Anime Recommender System")
st.markdown("---")
input_anime = st.text_input("Input Anime Title: ",
key="input_anime",
help="Enter the title of an anime you like!")
if st.button("Get Recommendations ๐Ÿš€"):
if input_anime:
load_animation()
response = get_anime_recommendations(input_anime)
recommendations = format_anime_recommendation(response)
st.subheader("Anime Recommendations ๐Ÿ“œ")
for rec in recommendations:
with st.container():
st.markdown(f"""
**Anime:** {rec['Anime']} ๐Ÿ“บ
**Genre:** {rec['Genre']} ๐ŸŽญ
**Main Protagonists:** {rec['Main Protagonists']} ๐Ÿง‘โ€๐Ÿคโ€๐Ÿง‘
**Summary:** {rec['Summary']} ๐Ÿ“
---
""")
# Anime Character Chat Page
elif page == "Talk to Anime Character ๐Ÿ—ฃ๏ธ":
st.markdown("# ๐Ÿ’ฌ Chat with an Anime Character")
st.markdown("---")
character = st.text_input("Anime Character Name:",
help="Enter character name!")
question = st.text_input("Your Question:",
help="Ask your question!")
if st.button("Ask Character ๐Ÿ—จ๏ธ"):
if character and question:
load_animation()
response = get_anime_character_response(character, question)
st.markdown(f"### Response from {character} ๐ŸŽญ")
st.write(response)
# Anime Face Detect Page
elif page == "Anime Face Detect ๐Ÿ‘€":
st.markdown("# ๐Ÿ•ต๏ธ Anime Character Detector")
st.markdown("---")
input_prompt = """
You are an expert in the world of Anime where you need to see characters from the image and provide the details of every character in this below format:
1. Name of the Character(s):
2. Anime:
3. Role:
4. Favourite Habit:
If there are multiple characters then mention them separately in the above format
Note: If you don't find the character related to any Anime then kindly tell the user to provide an Anime character picture in this below format:
'Kindly provide a character from Anime'
"""
input_text = st.text_input("Input Prompt:",
help="Enter custom prompt!")
uploaded_file = st.file_uploader("Choose an image... ๐Ÿ“ท",
type=["jpg", "jpeg", "png"])
if uploaded_file:
image = Image.open(uploaded_file)
st.image(image, caption="Uploaded Image ๐Ÿ–ผ๏ธ", use_column_width=True)
if st.button("Analyze Character ๐Ÿ”"):
load_animation()
image_data = input_image_setup(uploaded_file)
response = get_gemini_response(input_text, image_data, input_prompt)
st.markdown("### Analysis Results ๐Ÿ“")
st.write(response)