Spaces:
Sleeping
Sleeping
Add stupid claude retry
Browse files
app.py
CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
|
|
2 |
from playwright.async_api import async_playwright, Page
|
3 |
from PIL import Image
|
4 |
from io import BytesIO
|
5 |
-
from anthropic import Anthropic
|
6 |
from dotenv import load_dotenv
|
7 |
import os
|
8 |
from typing import Literal
|
@@ -61,18 +61,39 @@ def messages_text_to_web(prompt):
|
|
61 |
]
|
62 |
|
63 |
|
|
|
|
|
|
|
|
|
64 |
# returns the full text of the response each time
|
65 |
-
def stream_claude(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
text = ""
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
|
78 |
def format_image(image: bytes, media_type: Literal["image/png", "image/jpeg"]):
|
@@ -125,7 +146,7 @@ def visual_feedback_messages(prompt, history: list[tuple[str, bytes]]):
|
|
125 |
|
126 |
def match_image_messages(image_bytes: bytes, history: list[tuple[bytes, bytes]]):
|
127 |
improve_prompt = """
|
128 |
-
Given the current draft of the webpage you generated for me as HTML and the original screenshot, improve the HTML to match closer to the original screenshot.
|
129 |
"""
|
130 |
|
131 |
return [
|
|
|
2 |
from playwright.async_api import async_playwright, Page
|
3 |
from PIL import Image
|
4 |
from io import BytesIO
|
5 |
+
from anthropic import Anthropic, APIStatusError
|
6 |
from dotenv import load_dotenv
|
7 |
import os
|
8 |
from typing import Literal
|
|
|
61 |
]
|
62 |
|
63 |
|
64 |
+
class MaxRetriesExceeded(Exception):
|
65 |
+
pass
|
66 |
+
|
67 |
+
|
68 |
# returns the full text of the response each time
|
69 |
+
def stream_claude(
|
70 |
+
messages,
|
71 |
+
system=system_prompt,
|
72 |
+
max_tokens=2000,
|
73 |
+
max_retries=3,
|
74 |
+
wait_seconds=1,
|
75 |
+
wait_exponential=1.5,
|
76 |
+
):
|
77 |
text = ""
|
78 |
+
for _ in range(max_retries):
|
79 |
+
try:
|
80 |
+
with anthropic.messages.stream(
|
81 |
+
model=model,
|
82 |
+
max_tokens=max_tokens,
|
83 |
+
system=system,
|
84 |
+
messages=messages,
|
85 |
+
) as stream:
|
86 |
+
for chunk in stream.text_stream:
|
87 |
+
text += chunk
|
88 |
+
yield text
|
89 |
+
return
|
90 |
+
except APIStatusError as e:
|
91 |
+
# anthropic.APIStatusError: {'type': 'error', 'error': {'details': None, 'type': 'overloaded_error', 'message': 'Overloaded'}}
|
92 |
+
print(
|
93 |
+
f"Error from Claude: {e}, will wait {wait_seconds} seconds before retrying"
|
94 |
+
)
|
95 |
+
wait_seconds *= wait_exponential
|
96 |
+
raise MaxRetriesExceeded
|
97 |
|
98 |
|
99 |
def format_image(image: bytes, media_type: Literal["image/png", "image/jpeg"]):
|
|
|
146 |
|
147 |
def match_image_messages(image_bytes: bytes, history: list[tuple[bytes, bytes]]):
|
148 |
improve_prompt = """
|
149 |
+
Given the current draft of the webpage you generated for me as HTML and the original screenshot, improve the HTML to match closer to the original screenshot. Remember not to output any commentary, just the HTML.
|
150 |
"""
|
151 |
|
152 |
return [
|