shukdevdatta123 commited on
Commit
7a90624
·
verified ·
1 Parent(s): 5acf918

Create v1.txt

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