Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -4,12 +4,21 @@ import streamlit as st | |
| 4 | 
             
            import google.generativeai as genai
         | 
| 5 | 
             
            from PIL import Image
         | 
| 6 | 
             
            import io
         | 
|  | |
| 7 |  | 
| 8 | 
             
            st.set_page_config(page_title="Gemini AI Chat", layout="wide")
         | 
| 9 |  | 
| 10 | 
             
            st.title("π€ Gemini AI Chat Interface by Volkan Sah")
         | 
| 11 | 
             
            st.markdown("Chat with Google's Gemini AI models. Supports both text and image inputs. Follow me on Github@volkansah for more cool stuff!")
         | 
| 12 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 13 | 
             
            # Sidebar for settings
         | 
| 14 | 
             
            with st.sidebar:
         | 
| 15 | 
             
                api_key = st.text_input("Enter Google AI API Key", type="password")
         | 
| @@ -41,7 +50,7 @@ uploaded_file = st.file_uploader("Upload an image (optional)", type=["jpg", "jpe | |
| 41 | 
             
            uploaded_image = None
         | 
| 42 | 
             
            if uploaded_file is not None:
         | 
| 43 | 
             
                uploaded_image = Image.open(uploaded_file).convert('RGB')
         | 
| 44 | 
            -
                st.image(uploaded_image, caption="Uploaded Image", use_container_width=True) | 
| 45 |  | 
| 46 | 
             
            # Chat input
         | 
| 47 | 
             
            user_input = st.chat_input("Type your message here...")
         | 
| @@ -59,9 +68,21 @@ if user_input and api_key: | |
| 59 | 
             
                    # Prepare the model and content
         | 
| 60 | 
             
                    model_instance = genai.GenerativeModel(model_name=model)
         | 
| 61 |  | 
| 62 | 
            -
                    content = [ | 
| 63 | 
             
                    if uploaded_image:
         | 
| 64 | 
            -
                         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 65 |  | 
| 66 | 
             
                    # Generate response
         | 
| 67 | 
             
                    response = model_instance.generate_content(
         | 
| @@ -81,6 +102,7 @@ if user_input and api_key: | |
| 81 |  | 
| 82 | 
             
                except Exception as e:
         | 
| 83 | 
             
                    st.error(f"Error: {str(e)}")
         | 
|  | |
| 84 |  | 
| 85 | 
             
            elif not api_key and user_input:
         | 
| 86 | 
             
                st.warning("Please enter your API key in the sidebar first.")
         | 
| @@ -90,7 +112,7 @@ with st.sidebar: | |
| 90 | 
             
                st.markdown("""
         | 
| 91 | 
             
                ## π Instructions:
         | 
| 92 | 
             
                1. Enter your Google AI API key
         | 
| 93 | 
            -
                2. Select a model
         | 
| 94 | 
             
                3. Adjust temperature and max tokens if needed
         | 
| 95 | 
             
                4. Optional: Set a system prompt
         | 
| 96 | 
             
                5. Upload an image (optional)
         | 
|  | |
| 4 | 
             
            import google.generativeai as genai
         | 
| 5 | 
             
            from PIL import Image
         | 
| 6 | 
             
            import io
         | 
| 7 | 
            +
            import base64
         | 
| 8 |  | 
| 9 | 
             
            st.set_page_config(page_title="Gemini AI Chat", layout="wide")
         | 
| 10 |  | 
| 11 | 
             
            st.title("π€ Gemini AI Chat Interface by Volkan Sah")
         | 
| 12 | 
             
            st.markdown("Chat with Google's Gemini AI models. Supports both text and image inputs. Follow me on Github@volkansah for more cool stuff!")
         | 
| 13 |  | 
| 14 | 
            +
            def encode_image(image):
         | 
| 15 | 
            +
                """Convert PIL Image to base64 string"""
         | 
| 16 | 
            +
                buffered = io.BytesIO()
         | 
| 17 | 
            +
                image.save(buffered, format="JPEG")
         | 
| 18 | 
            +
                image_bytes = buffered.getvalue()
         | 
| 19 | 
            +
                encoded_image = base64.b64encode(image_bytes).decode('utf-8')
         | 
| 20 | 
            +
                return encoded_image
         | 
| 21 | 
            +
             | 
| 22 | 
             
            # Sidebar for settings
         | 
| 23 | 
             
            with st.sidebar:
         | 
| 24 | 
             
                api_key = st.text_input("Enter Google AI API Key", type="password")
         | 
|  | |
| 50 | 
             
            uploaded_image = None
         | 
| 51 | 
             
            if uploaded_file is not None:
         | 
| 52 | 
             
                uploaded_image = Image.open(uploaded_file).convert('RGB')
         | 
| 53 | 
            +
                st.image(uploaded_image, caption="Uploaded Image", use_container_width=True)
         | 
| 54 |  | 
| 55 | 
             
            # Chat input
         | 
| 56 | 
             
            user_input = st.chat_input("Type your message here...")
         | 
|  | |
| 68 | 
             
                    # Prepare the model and content
         | 
| 69 | 
             
                    model_instance = genai.GenerativeModel(model_name=model)
         | 
| 70 |  | 
| 71 | 
            +
                    content = []
         | 
| 72 | 
             
                    if uploaded_image:
         | 
| 73 | 
            +
                        # Convert image to base64
         | 
| 74 | 
            +
                        encoded_image = encode_image(uploaded_image)
         | 
| 75 | 
            +
                        content = [
         | 
| 76 | 
            +
                            {"text": user_input},
         | 
| 77 | 
            +
                            {
         | 
| 78 | 
            +
                                "inline_data": {
         | 
| 79 | 
            +
                                    "mime_type": "image/jpeg",
         | 
| 80 | 
            +
                                    "data": encoded_image
         | 
| 81 | 
            +
                                }
         | 
| 82 | 
            +
                            }
         | 
| 83 | 
            +
                        ]
         | 
| 84 | 
            +
                    else:
         | 
| 85 | 
            +
                        content = [{"text": user_input}]
         | 
| 86 |  | 
| 87 | 
             
                    # Generate response
         | 
| 88 | 
             
                    response = model_instance.generate_content(
         | 
|  | |
| 102 |  | 
| 103 | 
             
                except Exception as e:
         | 
| 104 | 
             
                    st.error(f"Error: {str(e)}")
         | 
| 105 | 
            +
                    st.error("If using an image, make sure to select a vision-enabled model (ones with 'vision' in the name)")
         | 
| 106 |  | 
| 107 | 
             
            elif not api_key and user_input:
         | 
| 108 | 
             
                st.warning("Please enter your API key in the sidebar first.")
         | 
|  | |
| 112 | 
             
                st.markdown("""
         | 
| 113 | 
             
                ## π Instructions:
         | 
| 114 | 
             
                1. Enter your Google AI API key
         | 
| 115 | 
            +
                2. Select a model (use vision models for image analysis)
         | 
| 116 | 
             
                3. Adjust temperature and max tokens if needed
         | 
| 117 | 
             
                4. Optional: Set a system prompt
         | 
| 118 | 
             
                5. Upload an image (optional)
         | 

