AvocadoMuffin commited on
Commit
2203d96
·
verified ·
1 Parent(s): 8d5c4f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +170 -0
app.py CHANGED
@@ -0,0 +1,170 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ import google.generativeai as genai
4
+ from PIL import Image
5
+ from dotenv import load_dotenv
6
+ from modules.model_loader import load_gemini_model, generate_caption_with_gemini, generate_detailed_description
7
+
8
+ # Load environment variables
9
+ load_dotenv()
10
+
11
+ # Fetch API key securely
12
+ api_key = os.getenv("GEMINI_API_KEY")
13
+ # Configure Gemini with your API Key
14
+ genai.configure(api_key=api_key)
15
+
16
+ # Setup a session state counter
17
+ if "queries_done" not in st.session_state:
18
+ st.session_state.queries_done = 0
19
+
20
+ if "user_api_key" not in st.session_state:
21
+ st.session_state.user_api_key = None
22
+
23
+ # Add a state variable for the dynamic prompt
24
+ if "dynamic_prompt" not in st.session_state:
25
+ st.session_state.dynamic_prompt = ""
26
+
27
+ # Function to reset the dynamic prompt
28
+ def reset_dynamic_prompt():
29
+ st.session_state.dynamic_prompt = ""
30
+
31
+ # Streamlit App
32
+ st.title("🖼️ Dynamic Image Caption Generator (Gemini Pro Vision)")
33
+ st.write("Upload an image and generate creative descriptions based on your needs.")
34
+
35
+ # If more than 2 queries, ask for user's API key
36
+ if st.session_state.queries_done >= 2 and not st.session_state.user_api_key:
37
+ user_api_key = st.text_input("Enter your Gemini API Key to continue:", type="password")
38
+ if user_api_key:
39
+ st.session_state.user_api_key = user_api_key
40
+ genai.configure(api_key=user_api_key)
41
+ else:
42
+ st.warning("Please enter your Gemini API Key to continue using the app.")
43
+ st.stop()
44
+ else:
45
+ # For first two requests, use your own key
46
+ if st.session_state.queries_done < 2:
47
+ genai.configure(api_key=api_key)
48
+ elif st.session_state.user_api_key:
49
+ genai.configure(api_key=st.session_state.user_api_key)
50
+
51
+ # Upload image
52
+ uploaded_image = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])
53
+
54
+ if uploaded_image is not None:
55
+ image = Image.open(uploaded_image).convert("RGB")
56
+ # Use use_container_width instead of use_column_width
57
+ st.image(image, caption="Uploaded Image", use_container_width=True)
58
+
59
+ # Create tabs for different ways to interact
60
+ tab1, tab2 = st.tabs(["Quick Presets", "Ask Anything"])
61
+
62
+ with tab1:
63
+ # Prompt selection options
64
+ prompt_options = {
65
+ "basic": "Basic Description",
66
+ "chain_of_thought": "Detailed Analysis",
67
+ "story": "Creative Story",
68
+ "emotional": "Emotional Analysis",
69
+ "object": "Object Detection",
70
+ "context": "Contextual Description",
71
+ "action": "Action Description"
72
+ }
73
+
74
+ # Prompt selection
75
+ prompt_type = st.selectbox(
76
+ "Select description type",
77
+ options=list(prompt_options.keys()),
78
+ format_func=lambda x: prompt_options[x]
79
+ )
80
+
81
+ if st.button("Generate Description", key="preset_button"):
82
+ with st.spinner("Processing image..."):
83
+ # Increment the counter BEFORE processing
84
+ st.session_state.queries_done += 1
85
+
86
+ # Load model
87
+ gemini_model = load_gemini_model()
88
+
89
+ # Generate caption
90
+ with st.spinner("Generating basic caption..."):
91
+ caption = generate_caption_with_gemini(image, gemini_model)
92
+
93
+ # Show caption
94
+ st.subheader("✨ Basic Caption")
95
+ st.write(caption)
96
+
97
+ # Generate detailed description based on selected prompt type
98
+ with st.spinner(f"Generating {prompt_options[prompt_type]}..."):
99
+ detailed_description = generate_detailed_description(
100
+ image,
101
+ gemini_model,
102
+ prompt_type
103
+ )
104
+
105
+ # Show the result with appropriate header
106
+ st.subheader(f"✨ {prompt_options[prompt_type]}")
107
+ st.write(detailed_description)
108
+
109
+ # Add download button for the generated text
110
+ st.download_button(
111
+ label="Download Description",
112
+ data=detailed_description,
113
+ file_name="image_description.txt",
114
+ mime="text/plain",
115
+ key="download1"
116
+ )
117
+
118
+ # Show usage count
119
+ st.info(f"You have used {st.session_state.queries_done} out of 2 free queries.")
120
+
121
+ with tab2:
122
+ # Dynamic prompt box for custom questions
123
+ st.subheader("Ask anything about this image")
124
+ dynamic_prompt = st.text_area(
125
+ "Enter your question or what you'd like to know about the image",
126
+ value=st.session_state.dynamic_prompt,
127
+ placeholder="Examples:\n- Write a poem about this image\n- Explain what's happening in this scene\n- What emotion does this image evoke?\n- Describe this as if you were a detective",
128
+ key="dynamic_prompt_input"
129
+ )
130
+
131
+ if st.button("Get Answer", key="dynamic_button"):
132
+ if dynamic_prompt:
133
+ # Save the current prompt before processing
134
+ current_prompt = dynamic_prompt
135
+
136
+ # Clear the prompt for next use
137
+ reset_dynamic_prompt()
138
+
139
+ # Increment the counter BEFORE processing
140
+ st.session_state.queries_done += 1
141
+
142
+ with st.spinner("Processing your request..."):
143
+ # Load model
144
+ gemini_model = load_gemini_model()
145
+
146
+ # Generate response directly from the image and custom prompt
147
+ response = generate_detailed_description(
148
+ image,
149
+ gemini_model,
150
+ "custom",
151
+ current_prompt
152
+ )
153
+
154
+ # Display the response
155
+ st.subheader("✨ Response")
156
+ st.write(response)
157
+
158
+ # Add download button
159
+ st.download_button(
160
+ label="Download Response",
161
+ data=response,
162
+ file_name="image_response.txt",
163
+ mime="text/plain",
164
+ key="download2"
165
+ )
166
+
167
+ # Show usage count
168
+ st.info(f"You have used {st.session_state.queries_done} out of 2 free queries.")
169
+ else:
170
+ st.warning("Please enter a question or prompt first")