-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3e1ef31
commit f75291b
Showing
8 changed files
with
111 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# flyctl launch added from .gitignore | ||
**/.env | ||
**/.vscode | ||
**/.telethon | ||
**/__pycache__ | ||
**/venv | ||
fly.toml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# See https://fly.io/docs/app-guides/continuous-deployment-with-github-actions/ | ||
|
||
name: Fly Deploy | ||
on: | ||
push: | ||
branches: | ||
- main | ||
jobs: | ||
deploy: | ||
name: Deploy app | ||
runs-on: ubuntu-latest | ||
concurrency: deploy-group # optional: ensure only one action runs at a time | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: superfly/flyctl-actions/setup-flyctl@master | ||
- run: flyctl deploy --remote-only | ||
env: | ||
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# fly.toml app configuration file generated for lightsoff-scheduler on 2024-09-06T09:35:39+03:00 | ||
# | ||
# See https://fly.io/docs/reference/configuration/ for information about how to use this file. | ||
# | ||
|
||
app = 'lightsoff-scheduler' | ||
primary_region = 'waw' | ||
|
||
[build] | ||
|
||
[[vm]] | ||
size = 'shared-cpu-1x' | ||
|
||
[[env]] | ||
TELEGRAM_CHANNEL = "@lvivoblenergo" | ||
LOG_LEVEL = "INFO" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,42 @@ | ||
import anthropic | ||
import logging | ||
import json | ||
from datetime import datetime | ||
from datetime import datetime, timedelta | ||
import csv | ||
import io | ||
|
||
|
||
# TODO maybe will move this to Google Calendar API instead of emailing .ics file | ||
def ocr_image_to_calendar_api(image_b64, group="3.2"): | ||
def ocr_image_to_calendar_api_multi(image_b64): | ||
text = _ocr_image( | ||
image_b64, f"I've uploaded an image of a power outage schedule table. Please analyze it and create Google Calendar API request to update calendar for the power outages for group {group}. The image shows hours of the day across the top and group numbers in the first column. Orange cells indicate 'no energy' periods. The date is highlighted at the top. Combine consecutive outage periods into single events. Add a 10-minute notification before each event. Only respond with Google API json requests as an array add a separator '->' and date in format '%Y-%m-%d'.") | ||
parts = text.split("->") | ||
return parts[0], parts[1] | ||
|
||
|
||
def ocr_image_to_calendar_api_multi(image_b64, group="3.2"): | ||
text = _ocr_image( | ||
image_b64, f"Analyze image of a power outage schedule table which shows hours of the day across the top and group numbers in 1st column. Orange cells indicate 'no energy' periods. The date is highlighted at the top. Combine consecutive outage periods into single events. Only respond with a minified json object key - group number, value - array of pairs [start datetime, end], then add a separator '->' and date in format '%Y-%m-%d'.") | ||
image_b64, 'I uploaded an image of a power outage schedule table which shows hours of the day across the top and group numbers in 1st column. The image may be split into 2 12-hour sub-tables. Orange cells indicate outage periods. The date is highlighted at the top. Combine consecutive outage periods into single events. Only respond with a valid minified json of format {"group": [[full datetime start, full datetime end],[full datetime start, full datetime end]]}, then add a separator "->" and date in format "%Y-%m-%d".') | ||
logging.debug("Received text from OCR: %s", text) | ||
parts = text.split("->") | ||
return json.loads(parts[0].strip()), datetime.strptime(parts[1].strip(), "%Y-%m-%d") | ||
|
||
|
||
def ocr_image_to_timeframes(image_b64): | ||
text = _ocr_image( | ||
image_b64, 'Don\'t interpret the image text. Turn the large green-orange table into CSV, use 0 for orange, 1 for green, add headers, no prompt, add separator "FOR_DATE:" and date %Y-%m-%d from the top of the image.') | ||
logging.info("Received text from OCR: %s", text) | ||
parts = text.split("FOR_DATE:") | ||
day = datetime.strptime(parts[1].strip(), "%Y-%m-%d") | ||
csv_string = parts[0].strip() | ||
csv_reader = csv.reader(io.StringIO(csv_string)) | ||
|
||
def ocr_image_to_ics(image_b64, group="3.2"): | ||
return _ocr_image(image_b64, f"I've uploaded an image of a power outage schedule table. Please analyze it and create an .ics file for the power outages for group {group}. The image shows hours of the day across the top and group numbers in the first column. Orange cells indicate 'no energy' periods. Combine consecutive outage periods into single events. Add a 10-minute notification before each event. Provide the .ics file content in a format I can easily copy and save. The date for these events is shown at the top of the image - please use that date when creating the events. Only send the .ics file content, no additional text.") | ||
result = {} | ||
next(csv_reader, None) # skip the headers | ||
for row in csv_reader: | ||
group = row[0] | ||
result[group] = [] | ||
for i, cell in enumerate(row[1:]): | ||
if cell == "0": | ||
start = day + timedelta(hours=i) | ||
end = day + timedelta(hours=i + 1) | ||
if i != 0 and len(result[group]) > 0 and result[group][-1] is not None and result[group][-1][1] == start: | ||
result[group][-1][1] = end | ||
else: | ||
result[group].append([start, end]) | ||
return result, day | ||
|
||
|
||
def mock_ocr_to_calendar_api_multi(): | ||
|
@@ -30,92 +45,6 @@ def mock_ocr_to_calendar_api_multi(): | |
return json.loads(parts[0].strip()), datetime.strptime(parts[1].strip(), "%Y-%m-%d") | ||
|
||
|
||
def mock_ocr_to_calendar_api(): | ||
text = """ | ||
[ | ||
{ | ||
"summary": "Power Outage", | ||
"description": "Scheduled power outage for group 3.2", | ||
"start": { | ||
"dateTime": "2024-09-05T03:00:00+03:00", | ||
"timeZone": "Europe/Kiev" | ||
}, | ||
"end": { | ||
"dateTime": "2024-09-05T05:00:00+03:00", | ||
"timeZone": "Europe/Kiev" | ||
}, | ||
"reminders": { | ||
"useDefault": false, | ||
"overrides": [ | ||
{ | ||
"method": "popup", | ||
"minutes": 10 | ||
} | ||
] | ||
} | ||
}, | ||
{ | ||
"summary": "Power Outage", | ||
"description": "Scheduled power outage for group 3.2", | ||
"start": { | ||
"dateTime": "2024-09-05T15:00:00+03:00", | ||
"timeZone": "Europe/Kiev" | ||
}, | ||
"end": { | ||
"dateTime": "2024-09-05T17:00:00+03:00", | ||
"timeZone": "Europe/Kiev" | ||
}, | ||
"reminders": { | ||
"useDefault": false, | ||
"overrides": [ | ||
{ | ||
"method": "popup", | ||
"minutes": 10 | ||
} | ||
] | ||
} | ||
} | ||
] | ||
->2024-09-05 | ||
""" | ||
parts = text.split("->") | ||
return parts[0].strip(), parts[1].strip() | ||
|
||
|
||
def mock_ocr_to_ics(): | ||
return """BEGIN:VCALENDAR | ||
VERSION:2.0 | ||
PRODID:-//Your Organization//EN | ||
BEGIN:VEVENT | ||
SUMMARY:Power Outage (Group 3.2) | ||
DTSTART:20240905T030000 | ||
DTEND:20240905T050000 | ||
DTSTAMP:20240905T000000Z | ||
UID:[email protected] | ||
DESCRIPTION:Scheduled power outage for Group 3.2 | ||
BEGIN:VALARM | ||
ACTION:DISPLAY | ||
DESCRIPTION:Power outage starting in 10 minutes | ||
TRIGGER:-PT10M | ||
END:VALARM | ||
END:VEVENT | ||
BEGIN:VEVENT | ||
SUMMARY:Power Outage (Group 3.2) | ||
DTSTART:20240905T170000 | ||
DTEND:20240905T190000 | ||
DTSTAMP:20240905T000000Z | ||
UID:[email protected] | ||
DESCRIPTION:Scheduled power outage for Group 3.2 | ||
BEGIN:VALARM | ||
ACTION:DISPLAY | ||
DESCRIPTION:Power outage starting in 10 minutes | ||
TRIGGER:-PT10M | ||
END:VALARM | ||
END:VEVENT | ||
END:VCALENDAR | ||
""" | ||
|
||
|
||
def _ocr_image(image_b64, prompt): | ||
client = anthropic.Anthropic() # defaults to os.environ.get("ANTHROPIC_API_KEY) | ||
logging.info("Sending image to Anthropics API for OCR...") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters