|
3 | 3 | import json |
4 | 4 | import os |
5 | 5 | import ssl |
| 6 | +import traceback |
6 | 7 |
|
7 | 8 | import board |
8 | 9 | import displayio |
9 | 10 | import digitalio |
10 | 11 | import keypad |
11 | 12 | import socketpool |
| 13 | +import supervisor |
12 | 14 | from wifi import radio |
13 | 15 |
|
14 | 16 | import adafruit_requests |
|
26 | 28 | # MY_PROMPT="Give me an idea for a gluten free, keto dinner. Write one sentence" |
27 | 29 | # PLEASE_WAIT="Cooking something up just for you" |
28 | 30 | # |
29 | | -# Experiementation is best to figure out what works. Usually you'll want to ask |
| 31 | +# Experimentation is best to figure out what works. Usually you'll want to ask |
30 | 32 | # for just one sentence or paragraph, since the 128x32 pixel screen can't hold |
31 | 33 | # much text! |
32 | 34 |
|
@@ -116,7 +118,7 @@ def add_text(self, new_text): |
116 | 118 | self.scroll_to_end() |
117 | 119 |
|
118 | 120 | def set_text(self, text): |
119 | | - print("\033[H\033[2J", end=text) |
| 121 | + print("\n\n", end=text) |
120 | 122 | self.text = text |
121 | 123 | self.lines = wrap_text_to_pixels(text, display.width, nice_font) |
122 | 124 | self.offset = 0 |
@@ -161,6 +163,7 @@ def refresh(self): |
161 | 163 |
|
162 | 164 | def wait_button_scroll_text(): |
163 | 165 | led.switch_to_output(True) |
| 166 | + keys.events.clear() |
164 | 167 | deadline = ticks_add(ticks_ms(), |
165 | 168 | 5000 if wrapped_text.on_last_line() else 1000) |
166 | 169 | while True: |
@@ -194,32 +197,45 @@ def iter_lines(resp): |
194 | 197 | ] |
195 | 198 |
|
196 | 199 | keys = keypad.Keys((board.GP14,), value_when_pressed=False) |
197 | | -led = digitalio.DigitalInOut(board.LED) |
| 200 | +led = digitalio.DigitalInOut(board.GP10) |
| 201 | +led.switch_to_output(False) |
198 | 202 |
|
199 | | -while True: |
200 | | - wrapped_text.show(please_wait) |
201 | | - |
202 | | - with requests.post("https://api.openai.com/v1/chat/completions", |
203 | | - json={"model": "gpt-3.5-turbo", "messages": full_prompt, "stream": True}, |
204 | | - headers={ |
205 | | - "Authorization": f"Bearer {openai_api_key}", |
206 | | - }, |
207 | | - ) as response: |
208 | | - |
209 | | - wrapped_text.set_text("") |
210 | | - if response.status_code != 200: |
211 | | - wrapped_text.show(f"Uh oh! {response.status_code}: {response.reason}") |
212 | | - else: |
213 | | - wrapped_text.show("") |
214 | | - for line in iter_lines(response): |
215 | | - if line.startswith("data: [DONE]"): |
216 | | - break |
217 | | - if line.startswith("data:"): |
218 | | - content = json.loads(line[5:]) |
219 | | - try: |
220 | | - token = content['choices'][0]['delta'].get('content', '') |
221 | | - except (KeyError, IndexError) as e: |
222 | | - token = None |
223 | | - if token: |
224 | | - wrapped_text.add_show(token) |
225 | | - wait_button_scroll_text() |
| 203 | +try: |
| 204 | + while True: |
| 205 | + wrapped_text.show(please_wait) |
| 206 | + |
| 207 | + with requests.post("https://api.openai.com/v1/chat/completions", |
| 208 | + json={"model": "gpt-3.5-turbo", "messages": full_prompt, "stream": True}, |
| 209 | + headers={ |
| 210 | + "Authorization": f"Bearer {openai_api_key}", |
| 211 | + }, |
| 212 | + ) as response: |
| 213 | + |
| 214 | + wrapped_text.set_text("") |
| 215 | + if response.status_code != 200: |
| 216 | + wrapped_text.show(f"Uh oh! {response.status_code}: {response.reason}") |
| 217 | + else: |
| 218 | + wrapped_text.show("") |
| 219 | + for line in iter_lines(response): |
| 220 | + led.switch_to_output(True) |
| 221 | + if line.startswith("data: [DONE]"): |
| 222 | + break |
| 223 | + if line.startswith("data:"): |
| 224 | + content = json.loads(line[5:]) |
| 225 | + try: |
| 226 | + token = content['choices'][0]['delta'].get('content', '') |
| 227 | + except (KeyError, IndexError) as e: |
| 228 | + token = None |
| 229 | + led.value = False |
| 230 | + if token: |
| 231 | + wrapped_text.add_show(token) |
| 232 | + wait_button_scroll_text() |
| 233 | +except Exception as e: # pylint: disable=broad-except |
| 234 | + traceback.print_exception(e) # pylint: disable=no-value-for-parameter |
| 235 | + print(end="\n\n\nAn error occurred\n\nPress button\nto reload") |
| 236 | + display.root_group = displayio.CIRCUITPYTHON_TERMINAL |
| 237 | + display.auto_refresh = True |
| 238 | + while True: |
| 239 | + if (event1 := keys.events.get()) and event1.pressed: |
| 240 | + break |
| 241 | + supervisor.reload() |
0 commit comments