gemini_clone / app.py
adil9858's picture
Update app.py
37bd679 verified
import streamlit as st
import google.generativeai as genai
import os
# Configure the API key
api_key = 'AIzaSyC13hxiLDZQagfXybLKCklbTlBDsO4vHwQ'
genai.configure(api_key=api_key)
# Function to upload and process the image
def process_image(image_path, user_question=None):
try:
# Upload the image to Google Generative AI
sample_file = genai.upload_file(path=image_path, display_name="Sample drawing")
# Use the Generative AI model to analyze the image
model = genai.GenerativeModel(model_name="models/gemini-2.0-flash-exp")
# Prepare the prompt
prompt = user_question if user_question else "Explain the scene in the image in detail."
response = model.generate_content([prompt, sample_file])
# Display the response
st.write("**VisioX Analysis:**")
st.write(response.text)
# Delete the uploaded file
genai.delete_file(sample_file.name)
except Exception as e:
st.error(f"An error occurred: {e}")
# Custom CSS for cyberpunk styling
def set_cyberpunk_style():
st.markdown(
"""
<style>
@import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@900&family=Roboto+Mono:wght@300&display=swap');
html, body, [class*="css"] {
font-family: 'Roboto Mono', monospace;
color: #00ff00;
background-color: #0a0a0a;
}
h1, h2, h3, h4, h5, h6 {
font-family: 'Orbitron', sans-serif;
color: #ff00ff !important;
}
.stButton button {
background-color: #000000 !important;
color: #00ff00 !important;
border: 2px solid #00ff00 !important;
font-family: 'Orbitron', sans-serif;
font-weight: bold;
transition: all 0.3s ease;
}
.stButton button:hover {
background-color: #00ff00 !important;
color: #000000 !important;
border: 2px solid #000000 !important;
}
.stTextInput input {
background-color: #0a0a0a;
color: #00ff00;
border: 2px solid #ff00ff;
}
.stCameraInput {
border: 2px solid #ff00ff;
border-radius: 10px;
}
.stImage img {
border: 2px solid #00ff00;
border-radius: 10px;
}
.stSpinner div {
color: #ff00ff !important;
}
.stMarkdown {
color: #00ff00 !important;
}
</style>
""",
unsafe_allow_html=True,
)
# Streamlit app
def main():
# Set cyberpunk style
set_cyberpunk_style()
# App title and description
st.title("VisioX : SXR")
st.markdown(
"""
<div style="color: #00ff00;">
Welcome to <span style="color: #ff00ff;">VisioX</span>, the ultimate AI-powered vision analyzer.
Capture an image, and let VisioX explain the scene or answer your questions.
</div>
""",
unsafe_allow_html=True,
)
# Access the camera and capture an image
st.markdown("### Capture an Image")
picture = st.camera_input("Take a picture")
if picture:
# Save the captured image to a temporary file
image_path = "captured_image.png"
with open(image_path, "wb") as f:
f.write(picture.getbuffer())
# Display the captured image
st.markdown("### Captured Image")
st.image(image_path, caption="Your Moment")
# Add a text input for user questions
st.markdown("### Ask a Question")
user_question = st.text_input(
"Ask a question about the image (optional):",
placeholder="e.g., What is in the background?",
key="question_input",
)
# Process the image
if st.button("Analyze with VisioX"):
with st.spinner("VisioX is analyzing your image..."):
process_image(image_path, user_question)
# Clean up the temporary file
os.remove(image_path)
# Run the app
if __name__ == "__main__":
main()