Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
@@ -1,166 +1,260 @@
|
|
1 |
-
|
2 |
-
|
|
|
|
|
|
|
|
|
3 |
import os
|
4 |
-
from
|
5 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
from anthropic.types import TextBlock
|
7 |
from anthropic.types.beta import BetaMessage, BetaTextBlock, BetaToolUseBlock
|
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 |
-
if not
|
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 |
-
def
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
if
|
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 |
-
response = raw_response.parse()
|
110 |
-
scale_x = image.width // 1512
|
111 |
-
scale_y = image.height // 982
|
112 |
-
for r in response.content:
|
113 |
-
if hasattr(r, 'text'):
|
114 |
-
chat_history = chat_history + [[input_text, r.text]]
|
115 |
-
|
116 |
-
if hasattr(r, 'input') and 'coordinate' in r.input:
|
117 |
-
coordinate = r.input['coordinate']
|
118 |
-
new_image = draw_circle_on_image(image, (coordinate[0] * scale_x, coordinate[1] * scale_y))
|
119 |
-
|
120 |
-
# Save the image or encode it as a base64 string if needed
|
121 |
-
image_url = save_image_or_get_url(
|
122 |
-
new_image) # Define this function to save or generate the URL for the image
|
123 |
-
|
124 |
-
# Include the image as part of the chat history
|
125 |
-
image_html = f'<img src="{image_url}" alt="Processed Image" style="max-width: 100%; max-height: 200px;">'
|
126 |
-
chat_history = chat_history + [[None, (image_url,)]]
|
127 |
-
return chat_history
|
128 |
-
|
129 |
-
# Read the image and encode it in base64
|
130 |
-
|
131 |
-
|
132 |
|
|
|
|
|
|
|
133 |
|
134 |
|
135 |
-
|
136 |
-
|
137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
|
139 |
|
140 |
-
# Create the Gradio interface
|
141 |
with gr.Blocks() as demo:
|
142 |
-
|
143 |
-
with gr.Column():
|
144 |
-
image_input = gr.Image(label="Upload Image", type="pil", interactive=True)
|
145 |
-
with gr.Column():
|
146 |
-
chatbot = gr.Chatbot(label="Chatbot Interaction", height=400)
|
147 |
|
148 |
-
|
149 |
-
user_input = gr.Textbox(label="Type your message here", placeholder="Enter your message...")
|
150 |
-
key_input = gr.Textbox(label="API Key", placeholder="Enter your key...", type="password")
|
151 |
-
|
152 |
-
# Button to submit
|
153 |
-
submit_button = gr.Button("Submit")
|
154 |
|
155 |
-
|
156 |
-
|
157 |
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Entrypoint for Gradio, see https://gradio.app/
|
3 |
+
"""
|
4 |
+
|
5 |
+
import asyncio
|
6 |
+
import base64
|
7 |
import os
|
8 |
+
from datetime import datetime
|
9 |
+
from enum import StrEnum
|
10 |
+
from functools import partial
|
11 |
+
from pathlib import Path
|
12 |
+
from typing import cast, Dict
|
13 |
+
|
14 |
+
import gradio as gr
|
15 |
+
from anthropic import APIResponse
|
16 |
from anthropic.types import TextBlock
|
17 |
from anthropic.types.beta import BetaMessage, BetaTextBlock, BetaToolUseBlock
|
18 |
+
from anthropic.types.tool_use_block import ToolUseBlock
|
19 |
+
|
20 |
+
from computer_use_demo.loop import (
|
21 |
+
PROVIDER_TO_DEFAULT_MODEL_NAME,
|
22 |
+
APIProvider,
|
23 |
+
sampling_loop,
|
24 |
+
sampling_loop_sync,
|
25 |
+
)
|
26 |
+
|
27 |
+
from computer_use_demo.tools import ToolResult
|
28 |
+
|
29 |
+
|
30 |
+
CONFIG_DIR = Path("~/.anthropic").expanduser()
|
31 |
+
API_KEY_FILE = CONFIG_DIR / "api_key"
|
32 |
+
|
33 |
+
WARNING_TEXT = "⚠️ Security Alert: Never provide access to sensitive accounts or data, as malicious web content can hijack Claude's behavior"
|
34 |
+
|
35 |
+
|
36 |
+
class Sender(StrEnum):
|
37 |
+
USER = "user"
|
38 |
+
BOT = "assistant"
|
39 |
+
TOOL = "tool"
|
40 |
+
|
41 |
+
|
42 |
+
def setup_state(state):
|
43 |
+
if "messages" not in state:
|
44 |
+
state["messages"] = []
|
45 |
+
if "api_key" not in state:
|
46 |
+
# Try to load API key from file first, then environment
|
47 |
+
state["api_key"] = load_from_storage("api_key") or os.getenv("ANTHROPIC_API_KEY", "")
|
48 |
+
if not state["api_key"]:
|
49 |
+
print("API key not found. Please set it in the environment or storage.")
|
50 |
+
if "provider" not in state:
|
51 |
+
state["provider"] = os.getenv("API_PROVIDER", "anthropic") or APIProvider.ANTHROPIC
|
52 |
+
if "provider_radio" not in state:
|
53 |
+
state["provider_radio"] = state["provider"]
|
54 |
+
if "model" not in state:
|
55 |
+
_reset_model(state)
|
56 |
+
if "auth_validated" not in state:
|
57 |
+
state["auth_validated"] = False
|
58 |
+
if "responses" not in state:
|
59 |
+
state["responses"] = {}
|
60 |
+
if "tools" not in state:
|
61 |
+
state["tools"] = {}
|
62 |
+
if "only_n_most_recent_images" not in state:
|
63 |
+
state["only_n_most_recent_images"] = 10
|
64 |
+
if "custom_system_prompt" not in state:
|
65 |
+
state["custom_system_prompt"] = load_from_storage("system_prompt") or ""
|
66 |
+
# remove if want to use default system prompt
|
67 |
+
state["custom_system_prompt"] += "\n\nNote that you are operating on a Windows machine, so you should use double click to open a desktop application"
|
68 |
+
if "hide_images" not in state:
|
69 |
+
state["hide_images"] = False
|
70 |
+
|
71 |
+
|
72 |
+
def _reset_model(state):
|
73 |
+
state["model"] = PROVIDER_TO_DEFAULT_MODEL_NAME[cast(APIProvider, state["provider"])]
|
74 |
+
|
75 |
+
|
76 |
+
async def main(state):
|
77 |
+
"""Render loop for Gradio"""
|
78 |
+
setup_state(state)
|
79 |
+
return "Setup completed"
|
80 |
+
|
81 |
+
|
82 |
+
def validate_auth(provider: APIProvider, api_key: str | None):
|
83 |
+
if provider == APIProvider.ANTHROPIC:
|
84 |
+
if not api_key:
|
85 |
+
return "Enter your Anthropic API key to continue."
|
86 |
+
if provider == APIProvider.BEDROCK:
|
87 |
+
import boto3
|
88 |
+
|
89 |
+
if not boto3.Session().get_credentials():
|
90 |
+
return "You must have AWS credentials set up to use the Bedrock API."
|
91 |
+
if provider == APIProvider.VERTEX:
|
92 |
+
import google.auth
|
93 |
+
from google.auth.exceptions import DefaultCredentialsError
|
94 |
+
|
95 |
+
if not os.environ.get("CLOUD_ML_REGION"):
|
96 |
+
return "Set the CLOUD_ML_REGION environment variable to use the Vertex API."
|
97 |
+
try:
|
98 |
+
google.auth.default(scopes=["https://www.googleapis.com/auth/cloud-platform"])
|
99 |
+
except DefaultCredentialsError:
|
100 |
+
return "Your google cloud credentials are not set up correctly."
|
101 |
+
|
102 |
+
|
103 |
+
def load_from_storage(filename: str) -> str | None:
|
104 |
+
"""Load data from a file in the storage directory."""
|
105 |
+
try:
|
106 |
+
file_path = CONFIG_DIR / filename
|
107 |
+
if file_path.exists():
|
108 |
+
data = file_path.read_text().strip()
|
109 |
+
if data:
|
110 |
+
return data
|
111 |
+
except Exception as e:
|
112 |
+
print(f"Debug: Error loading {filename}: {e}")
|
113 |
+
return None
|
114 |
+
|
115 |
+
|
116 |
+
def save_to_storage(filename: str, data: str) -> None:
|
117 |
+
"""Save data to a file in the storage directory."""
|
118 |
+
try:
|
119 |
+
CONFIG_DIR.mkdir(parents=True, exist_ok=True)
|
120 |
+
file_path = CONFIG_DIR / filename
|
121 |
+
file_path.write_text(data)
|
122 |
+
# Ensure only user can read/write the file
|
123 |
+
file_path.chmod(0o600)
|
124 |
+
except Exception as e:
|
125 |
+
print(f"Debug: Error saving {filename}: {e}")
|
126 |
+
|
127 |
+
|
128 |
+
def _api_response_callback(response: APIResponse[BetaMessage], response_state: dict):
|
129 |
+
response_id = datetime.now().isoformat()
|
130 |
+
response_state[response_id] = response
|
131 |
+
|
132 |
+
|
133 |
+
def _tool_output_callback(tool_output: ToolResult, tool_id: str, tool_state: dict):
|
134 |
+
tool_state[tool_id] = tool_output
|
135 |
+
|
136 |
+
|
137 |
+
def _render_message(sender: Sender, message: str | BetaTextBlock | BetaToolUseBlock | ToolResult, state):
|
138 |
+
is_tool_result = not isinstance(message, str) and (
|
139 |
+
isinstance(message, ToolResult)
|
140 |
+
or message.__class__.__name__ == "ToolResult"
|
141 |
+
or message.__class__.__name__ == "CLIResult"
|
142 |
+
)
|
143 |
+
if not message or (
|
144 |
+
is_tool_result
|
145 |
+
and state["hide_images"]
|
146 |
+
and not hasattr(message, "error")
|
147 |
+
and not hasattr(message, "output")
|
148 |
+
):
|
149 |
+
return
|
150 |
+
if is_tool_result:
|
151 |
+
message = cast(ToolResult, message)
|
152 |
+
if message.output:
|
153 |
+
return message.output
|
154 |
+
if message.error:
|
155 |
+
return f"Error: {message.error}"
|
156 |
+
if message.base64_image and not state["hide_images"]:
|
157 |
+
return base64.b64decode(message.base64_image)
|
158 |
+
elif isinstance(message, BetaTextBlock) or isinstance(message, TextBlock):
|
159 |
+
return message.text
|
160 |
+
elif isinstance(message, BetaToolUseBlock) or isinstance(message, ToolUseBlock):
|
161 |
+
return f"Tool Use: {message.name}\nInput: {message.input}"
|
162 |
+
else:
|
163 |
+
return message
|
164 |
+
# open new tab, open google sheets inside, then create a new blank spreadsheet
|
165 |
+
|
166 |
+
def process_input(user_input, state):
|
167 |
+
# Ensure the state is properly initialized
|
168 |
+
setup_state(state)
|
169 |
+
|
170 |
+
# Append the user input to the messages in the state
|
171 |
+
state["messages"].append(
|
172 |
+
{
|
173 |
+
"role": Sender.USER,
|
174 |
+
"content": [TextBlock(type="text", text=user_input)],
|
175 |
+
}
|
176 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
177 |
|
178 |
+
# Run the sampling loop synchronously and yield messages
|
179 |
+
for message in sampling_loop(state):
|
180 |
+
yield message
|
181 |
|
182 |
|
183 |
+
def accumulate_messages(*args, **kwargs):
|
184 |
+
"""
|
185 |
+
Wrapper function to accumulate messages from sampling_loop_sync.
|
186 |
+
"""
|
187 |
+
accumulated_messages = []
|
188 |
+
|
189 |
+
for message in sampling_loop_sync(*args, **kwargs):
|
190 |
+
# Check if the message is already in the accumulated messages
|
191 |
+
if message not in accumulated_messages:
|
192 |
+
accumulated_messages.append(message)
|
193 |
+
# Yield the accumulated messages as a list
|
194 |
+
yield accumulated_messages
|
195 |
+
|
196 |
+
|
197 |
+
def sampling_loop(state):
|
198 |
+
# Ensure the API key is present
|
199 |
+
if not state.get("api_key"):
|
200 |
+
raise ValueError("API key is missing. Please set it in the environment or storage.")
|
201 |
+
|
202 |
+
# Call the sampling loop and yield messages
|
203 |
+
for message in accumulate_messages(
|
204 |
+
system_prompt_suffix=state["custom_system_prompt"],
|
205 |
+
model=state["model"],
|
206 |
+
provider=state["provider"],
|
207 |
+
messages=state["messages"],
|
208 |
+
output_callback=partial(_render_message, Sender.BOT, state=state),
|
209 |
+
tool_output_callback=partial(_tool_output_callback, tool_state=state["tools"]),
|
210 |
+
api_response_callback=partial(_api_response_callback, response_state=state["responses"]),
|
211 |
+
api_key=state["api_key"],
|
212 |
+
only_n_most_recent_images=state["only_n_most_recent_images"],
|
213 |
+
):
|
214 |
+
yield message
|
215 |
|
216 |
|
|
|
217 |
with gr.Blocks() as demo:
|
218 |
+
state = gr.State({}) # Use Gradio's state management
|
|
|
|
|
|
|
|
|
219 |
|
220 |
+
gr.Markdown("# Claude Computer Use Demo")
|
|
|
|
|
|
|
|
|
|
|
221 |
|
222 |
+
if not os.getenv("HIDE_WARNING", False):
|
223 |
+
gr.Markdown(WARNING_TEXT)
|
224 |
|
225 |
+
with gr.Row():
|
226 |
+
provider = gr.Dropdown(
|
227 |
+
label="API Provider",
|
228 |
+
choices=[option.value for option in APIProvider],
|
229 |
+
value="anthropic",
|
230 |
+
interactive=True,
|
231 |
+
)
|
232 |
+
model = gr.Textbox(label="Model", value="claude-3-5-sonnet-20241022")
|
233 |
+
api_key = gr.Textbox(
|
234 |
+
label="Anthropic API Key",
|
235 |
+
type="password",
|
236 |
+
value="",
|
237 |
+
interactive=True,
|
238 |
+
)
|
239 |
+
only_n_images = gr.Slider(
|
240 |
+
label="Only send N most recent images",
|
241 |
+
minimum=0,
|
242 |
+
value=10,
|
243 |
+
interactive=True,
|
244 |
+
)
|
245 |
+
custom_prompt = gr.Textbox(
|
246 |
+
label="Custom System Prompt Suffix",
|
247 |
+
value="",
|
248 |
+
interactive=True,
|
249 |
+
)
|
250 |
+
hide_images = gr.Checkbox(label="Hide screenshots", value=False)
|
251 |
+
|
252 |
+
api_key.change(fn=lambda key: save_to_storage(API_KEY_FILE, key), inputs=api_key)
|
253 |
+
chat_input = gr.Textbox(label="Type a message to send to Claude...")
|
254 |
+
# chat_output = gr.Textbox(label="Chat Output", interactive=False)
|
255 |
+
chatbot = gr.Chatbot(label="Chatbot History")
|
256 |
+
|
257 |
+
# Pass state as an input to the function
|
258 |
+
chat_input.submit(process_input, [chat_input, state], chatbot)
|
259 |
+
|
260 |
+
demo.launch(share=True)
|