-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComputerVisionLLM.py
More file actions
170 lines (138 loc) · 5.77 KB
/
ComputerVisionLLM.py
File metadata and controls
170 lines (138 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#%%
import streamlit as st
import requests
import json
import logging
from typing import Optional
from ultralytics import YOLO
from ultralytics import YOLOWorld
from PIL import Image
#%%
# Constants
BASE_API_URL = "http://68.183.143.199:7860"
FLOW_ID = "060365f6-e9af-47b0-a022-f1d7ea853132"
ENDPOINT = "" # You can set a specific endpoint name in the flow settings
TWEAKS = {
"ChatOutput-Tfdxu": {},
"Prompt-Pt2Wc": {},
"OpenAIModel-BmzJ5": {},
"TextInput-M45by": {},
"Memory-RAwe2": {},
"Prompt-XxBlc": {},
"ChatInput-ne99J": {}
}
# Initialize logging
logging.basicConfig(level=logging.INFO)
# Function to run the flow
def run_flow(message: str,
endpoint: str = FLOW_ID,
output_type: str = "chat",
input_type: str = "chat",
tweaks: Optional[dict] = None,
api_key: Optional[str] = None) -> dict:
"""
Run a flow with a given message and optional tweaks.
:param message: The message to send to the flow
:param endpoint: The ID or the endpoint name of the flow
:param tweaks: Optional tweaks to customize the flow
:return: The JSON response from the flow
"""
api_url = f"{BASE_API_URL}/api/v1/run/{endpoint}"
payload = {
"input_value": message,
"output_type": output_type,
"input_type": input_type,
}
if tweaks:
payload["tweaks"] = tweaks
headers = {"x-api-key": api_key} if api_key else None
response = requests.post(api_url, json=payload, headers=headers)
# Log the response for debugging
logging.info(f"Response Status Code: {response.status_code}")
logging.info(f"Response Text: {response.text}")
try:
return response.json()
except json.JSONDecodeError:
logging.error("Failed to decode JSON from the server response.")
return {}
# Function to extract the assistant's message from the response
def extract_message(response: dict) -> str:
try:
# Extract the response message
return response['outputs'][0]['outputs'][0]['results']['message']['text']
except (KeyError, IndexError):
logging.error("No valid message found in response.")
return "No valid message found in response."
# Function to run the flow
def main():
st.title("Computer Vision + LLM 🤖")
st.write("🢀Please capture an image first before you start asking question.")
with st.sidebar:
# File uploader for image
#uploaded_file = st.file_uploader("Upload an image (JPG, PNG)", type=["jpg", "jpeg", "png"])
enable = st.checkbox("Enable camera")
picture = st.camera_input("Take a picture", disabled=not enable)
json_full = []
if picture is not None:
# Convert the uploaded file to an image
image = Image.open(picture)
st.image(image, caption="Uploaded Image", use_column_width=True)
# Perform inference on the uploaded image
model= YOLO("yolov8m-worldv2")
results = []
if results is not None:
results = model(image) # Use the uploaded image for inference
json_full = results[0].to_json()
# Iterate over the results
detected_classes = []
for result in results:
detections = result.boxes
for box in detections:
class_id = int(box.cls[0]) # Class index
confidence = box.conf[0] # Confidence score
class_name = model.names[class_id] # Class name
detected_classes.append(class_name)
print(f"Detected: {class_name} with confidence: {confidence:.2f}")
#text_input = st.text_input(detect)
#st.write(json_full) #cross check detection result
TWEAKS["TextInput-M45by"]["input_value"] = json_full
# Initialize session state for chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# Display previous messages with avatars
for message in st.session_state.messages:
with st.chat_message(message["role"], avatar=message["avatar"]):
st.write(message["content"])
# Input box for user message
if query := st.chat_input("Ask me anything..."):
# Add user message to session state
st.session_state.messages.append(
{
"role": "user",
"content": query,
"avatar": "💬", # Emoji for user
}
)
with st.chat_message("user", avatar="💬"): # Display user message
st.write(query)
# Call the Langflow API and get the assistant's response
with st.chat_message("assistant", avatar="🤖"): # Emoji for assistant
message_placeholder = st.empty() # Placeholder for assistant response
with st.spinner("Thinking..."):
# Fetch response from Langflow with updated TWEAKS and using `query`
assistant_response = extract_message(run_flow(query, tweaks=TWEAKS))
message_placeholder.write(assistant_response)
# Add assistant response to session state
st.session_state.messages.append(
{
"role": "assistant",
"content": assistant_response,
"avatar": "🤖", # Emoji for assistant
}
)
if picture is not None:
#[Optional] Show detection result
st.image(results[0].plot())
if __name__ == "__main__":
main()
# %%