shukdevdatta123 commited on
Commit
5acf918
·
verified ·
1 Parent(s): 874dd0f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +230 -0
app.py ADDED
@@ -0,0 +1,230 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ from openai import OpenAI
4
+ import json
5
+ import requests
6
+ from PIL import Image
7
+ import io
8
+ import base64
9
+
10
+ # Constants
11
+ DEFAULT_MODEL = "opengvlab/internvl3-14b:free"
12
+ # No need for placeholder text as we'll use password type input
13
+ DEFAULT_SITE_URL = "https://dynamic-nature-trail-guide.app"
14
+ DEFAULT_SITE_NAME = "Dynamic Nature Trail Guide"
15
+
16
+ # Initialize system prompt for better nature guide responses
17
+ SYSTEM_PROMPT = """
18
+ You are the Dynamic Nature Trail Guide, an expert in identifying and explaining natural elements
19
+ found on nature trails. For any image sent, please:
20
+
21
+ 1. Identify all visible plants, animals, geological features, and ecosystems
22
+ 2. Provide educational information about identified elements
23
+ 3. Mention any seasonal characteristics visible in the image
24
+ 4. Note any ecological significance or conservation considerations
25
+ 5. Offer suggestions for what to observe or learn about next on the trail
26
+
27
+ Keep explanations informative yet accessible to people of all ages and backgrounds.
28
+ """
29
+
30
+ def encode_image_to_base64(image_path):
31
+ """Convert an image file to base64 encoding"""
32
+ with open(image_path, "rb") as image_file:
33
+ return base64.b64encode(image_file.read()).decode('utf-8')
34
+
35
+ def analyze_image(api_key, image, prompt="What can you identify in this nature trail image? Provide detailed educational information.", site_url=DEFAULT_SITE_URL, site_name=DEFAULT_SITE_NAME, model=DEFAULT_MODEL):
36
+ """Analyze the uploaded image using the InternVL3 model via OpenRouter"""
37
+ # Remove the placeholder text check
38
+ if not api_key:
39
+ return "Please provide an OpenRouter API key."
40
+
41
+ if image is None:
42
+ return "Please upload an image to analyze."
43
+
44
+ # Save the image temporarily
45
+ temp_image_path = "temp_image.jpg"
46
+ image.save(temp_image_path)
47
+
48
+ try:
49
+ # Convert image to base64
50
+ base64_image = encode_image_to_base64(temp_image_path)
51
+
52
+ # Initialize OpenAI client
53
+ client = OpenAI(
54
+ base_url="https://openrouter.ai/api/v1",
55
+ api_key=api_key,
56
+ )
57
+
58
+ # Create message with image and text
59
+ response = client.chat.completions.create(
60
+ extra_headers={
61
+ "HTTP-Referer": site_url,
62
+ "X-Title": site_name,
63
+ },
64
+ model=model,
65
+ messages=[
66
+ {"role": "system", "content": SYSTEM_PROMPT},
67
+ {
68
+ "role": "user",
69
+ "content": [
70
+ {
71
+ "type": "text",
72
+ "text": prompt
73
+ },
74
+ {
75
+ "type": "image_url",
76
+ "image_url": {
77
+ "url": f"data:image/jpeg;base64,{base64_image}"
78
+ }
79
+ }
80
+ ]
81
+ }
82
+ ]
83
+ )
84
+
85
+ analysis_result = response.choices[0].message.content
86
+ return analysis_result
87
+
88
+ except Exception as e:
89
+ return f"Error analyzing image: {str(e)}"
90
+
91
+ finally:
92
+ # Clean up the temporary file
93
+ if os.path.exists(temp_image_path):
94
+ os.remove(temp_image_path)
95
+
96
+ def build_custom_prompt(identification=True, education=True, seasonal=True, conservation=True, suggestions=True, additional_prompt=""):
97
+ """Build a custom prompt based on user preferences"""
98
+ prompt_parts = []
99
+
100
+ if identification:
101
+ prompt_parts.append("Identify all visible plants, animals, geological features, and ecosystems")
102
+
103
+ if education:
104
+ prompt_parts.append("Provide educational information about identified elements")
105
+
106
+ if seasonal:
107
+ prompt_parts.append("Mention any seasonal characteristics visible in the image")
108
+
109
+ if conservation:
110
+ prompt_parts.append("Note any ecological significance or conservation considerations")
111
+
112
+ if suggestions:
113
+ prompt_parts.append("Offer suggestions for what to observe or learn next on the trail")
114
+
115
+ if additional_prompt:
116
+ prompt_parts.append(additional_prompt)
117
+
118
+ if not prompt_parts:
119
+ return "What can you identify in this nature trail image?"
120
+
121
+ numbered_prompt = "\n".join([f"{i+1}. {part}" for i, part in enumerate(prompt_parts)])
122
+ return f"For this nature trail image, please: \n{numbered_prompt}"
123
+
124
+ def create_interface():
125
+ """Create the Gradio interface for the Dynamic Nature Trail Guide"""
126
+ with gr.Blocks(title="Dynamic Nature Trail Guide", theme=gr.themes.Soft()) as app:
127
+ gr.Markdown("""
128
+ # 🌿 Dynamic Nature Trail Guide: Accessible Outdoor Education 🌿
129
+
130
+ Upload an image from your nature walk to identify plants, animals, geological features, and learn about the ecosystem.
131
+ This application uses the advanced InternVL3 14B multimodal model for nature identification and education.
132
+ """)
133
+
134
+ with gr.Row():
135
+ with gr.Column(scale=1):
136
+ api_key_input = gr.Textbox(
137
+ label="OpenRouter API Key",
138
+ placeholder="Enter your OpenRouter API key here...",
139
+ type="password"
140
+ )
141
+ image_input = gr.Image(label="Upload Nature Image", type="pil")
142
+
143
+ with gr.Accordion("Advanced Settings", open=False):
144
+ site_url = gr.Textbox(
145
+ label="Site URL (for OpenRouter)",
146
+ value=DEFAULT_SITE_URL
147
+ )
148
+ site_name = gr.Textbox(
149
+ label="Site Name (for OpenRouter)",
150
+ value=DEFAULT_SITE_NAME
151
+ )
152
+ model_selection = gr.Dropdown(
153
+ label="Model",
154
+ choices=[DEFAULT_MODEL],
155
+ value=DEFAULT_MODEL
156
+ )
157
+
158
+ with gr.Accordion("Customize Analysis", open=False):
159
+ gr.Markdown("Select what information you want to receive about the image:")
160
+ identification_checkbox = gr.Checkbox(label="Species & Feature Identification", value=True)
161
+ education_checkbox = gr.Checkbox(label="Educational Information", value=True)
162
+ seasonal_checkbox = gr.Checkbox(label="Seasonal Characteristics", value=True)
163
+ conservation_checkbox = gr.Checkbox(label="Conservation Considerations", value=True)
164
+ suggestions_checkbox = gr.Checkbox(label="Trail Suggestions", value=True)
165
+ additional_prompt = gr.Textbox(label="Additional Instructions (Optional)")
166
+
167
+ analyze_button = gr.Button("Analyze Nature Image", variant="primary")
168
+
169
+ with gr.Column(scale=1):
170
+ output_text = gr.Markdown(label="Analysis Results")
171
+
172
+ # Set up the click event for the analyze button
173
+ analyze_button.click(
174
+ fn=lambda api_key, image, id_check, edu_check, season_check, conserve_check, suggest_check, add_prompt, site_url, site_name, model:
175
+ analyze_image(
176
+ api_key,
177
+ image,
178
+ build_custom_prompt(id_check, edu_check, season_check, conserve_check, suggest_check, add_prompt),
179
+ site_url,
180
+ site_name,
181
+ model
182
+ ),
183
+ inputs=[
184
+ api_key_input,
185
+ image_input,
186
+ identification_checkbox,
187
+ education_checkbox,
188
+ seasonal_checkbox,
189
+ conservation_checkbox,
190
+ suggestions_checkbox,
191
+ additional_prompt,
192
+ site_url,
193
+ site_name,
194
+ model_selection
195
+ ],
196
+ outputs=output_text
197
+ )
198
+
199
+ # Example gallery
200
+ with gr.Accordion("Example Images", open=False):
201
+ gr.Markdown("Click on an example image to analyze it:")
202
+ example_images = gr.Examples(
203
+ examples=[
204
+ "examples/forest_trail.jpg",
205
+ "examples/wetland_boardwalk.jpg",
206
+ "examples/mountain_vista.jpg",
207
+ "examples/coastal_trail.jpg",
208
+ ],
209
+ inputs=image_input,
210
+ label="Nature Trail Examples"
211
+ )
212
+
213
+ gr.Markdown("""
214
+ ## How to Use This App
215
+
216
+ 1. Enter your OpenRouter API key (sign up at [openrouter.ai](https://openrouter.ai) if needed)
217
+ 2. Upload an image from your nature walk
218
+ 3. Customize what kind of information you want (optional)
219
+ 4. Click "Analyze Nature Image"
220
+ 5. Explore the detailed educational content about what you're seeing
221
+
222
+ This application is designed to make nature more accessible and educational for everyone!
223
+ """)
224
+
225
+ return app
226
+
227
+ # Create and launch the app
228
+ if __name__ == "__main__":
229
+ app = create_interface()
230
+ app.launch()