adil9858 commited on
Commit
a6c1838
·
verified ·
1 Parent(s): 33cebba

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +171 -0
app.py ADDED
@@ -0,0 +1,171 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import base64
3
+ from openai import OpenAI
4
+ from PIL import Image
5
+ import io
6
+ import cv2
7
+ import numpy as np
8
+
9
+ # Configure app
10
+ st.set_page_config(
11
+ page_title="AI Vision Assistant",
12
+ page_icon="🔍",
13
+ layout="wide",
14
+ initial_sidebar_state="expanded"
15
+ )
16
+
17
+ # Custom CSS (keep your existing CSS here)
18
+ st.markdown("""
19
+ <style>
20
+ /* Your existing CSS styles */
21
+ </style>
22
+ """, unsafe_allow_html=True)
23
+
24
+ # App title and description
25
+ st.title("🔍 Optimus Alpha | Live Vision Assistant")
26
+
27
+ # Initialize OpenAI client (keep your existing cached function)
28
+ @st.cache_resource
29
+ def get_client():
30
+ return OpenAI(
31
+ base_url="https://openrouter.ai/api/v1",
32
+ api_key='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'
33
+ )
34
+
35
+ # ===== New Live Camera Section =====
36
+ st.subheader("Live Camera Feed")
37
+ run_camera = st.checkbox("Enable Camera", value=False)
38
+
39
+ FRAME_WINDOW = st.empty()
40
+ captured_image = None
41
+
42
+ if run_camera:
43
+ cap = cv2.VideoCapture(0)
44
+
45
+ capture_button = st.button("Capture Image")
46
+ stop_button = st.button("Stop Camera")
47
+
48
+ if stop_button:
49
+ run_camera = False
50
+ cap.release()
51
+ st.experimental_rerun()
52
+
53
+ while run_camera:
54
+ ret, frame = cap.read()
55
+ if not ret:
56
+ st.error("Failed to access camera")
57
+ break
58
+
59
+ # Display the live feed
60
+ frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
61
+ FRAME_WINDOW.image(frame)
62
+
63
+ if capture_button:
64
+ captured_image = frame
65
+ run_camera = False
66
+ cap.release()
67
+ break
68
+ else:
69
+ FRAME_WINDOW.info("Camera is currently off")
70
+
71
+ # ===== Image Processing Section =====
72
+ col1, col2 = st.columns([1, 2])
73
+
74
+ with col1:
75
+ st.subheader("Image Source")
76
+
77
+ # Option to use captured image or upload
78
+ if captured_image is not None:
79
+ st.image(captured_image, caption="Captured Image", width=300)
80
+ use_captured = True
81
+ else:
82
+ use_captured = False
83
+
84
+ uploaded_file = st.file_uploader(
85
+ "Or upload an image",
86
+ type=["jpg", "jpeg", "png"],
87
+ disabled=use_captured
88
+ )
89
+
90
+ # Determine which image to use
91
+ if use_captured:
92
+ image = Image.fromarray(captured_image)
93
+ elif uploaded_file:
94
+ image = Image.open(uploaded_file)
95
+ else:
96
+ image = None
97
+
98
+ with col2:
99
+ st.subheader("AI Analysis")
100
+
101
+ user_prompt = st.text_input(
102
+ "Your question about the image:",
103
+ placeholder="e.g. 'What objects do you see?' or 'Explain this diagram'",
104
+ key="user_prompt"
105
+ )
106
+
107
+ if st.button("Analyze", type="primary") and image:
108
+ try:
109
+ # Convert image to base64
110
+ buffered = io.BytesIO()
111
+ image.save(buffered, format="JPEG")
112
+ image_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
113
+
114
+ # Prepare messages
115
+ messages = [
116
+ {
117
+ "role": "system",
118
+ "content": """You are a real-time vision assistant. Analyze the current camera feed or uploaded image and:
119
+ 1. Identify objects, people, text clearly
120
+ 2. Answer follow-up questions precisely
121
+ 3. Format responses with bullet points
122
+ 4. Highlight urgent/important findings"""
123
+ },
124
+ {
125
+ "role": "user",
126
+ "content": [
127
+ {
128
+ "type": "text",
129
+ "text": user_prompt if user_prompt else "Describe what you see in detail"
130
+ },
131
+ {
132
+ "type": "image_url",
133
+ "image_url": {
134
+ "url": f"data:image/jpeg;base64,{image_base64}"
135
+ }
136
+ }
137
+ ]
138
+ }
139
+ ]
140
+
141
+ # Stream the response
142
+ response_container = st.empty()
143
+ full_response = ""
144
+
145
+ client = get_client()
146
+ stream = client.chat.completions.create(
147
+ model="openrouter/optimus-alpha",
148
+ messages=messages,
149
+ stream=True
150
+ )
151
+
152
+ for chunk in stream:
153
+ if chunk.choices[0].delta.content is not None:
154
+ full_response += chunk.choices[0].delta.content
155
+ response_container.markdown(f"""
156
+ <div class="markdown-text">
157
+ {full_response}
158
+ </div>
159
+ """, unsafe_allow_html=True)
160
+
161
+ except Exception as e:
162
+ st.error(f"Error: {str(e)}")
163
+
164
+ # Sidebar (keep your existing sidebar)
165
+ with st.sidebar:
166
+ st.image("blob.png", width=200)
167
+ st.markdown("""
168
+ *Powered by OpenRouter*
169
+ """)
170
+ st.markdown("---")
171
+ st.markdown("Made with ❤️ by Koshur AI")