-
Notifications
You must be signed in to change notification settings - Fork 0
/
illustrate.py
executable file
·76 lines (57 loc) · 1.79 KB
/
illustrate.py
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
#!python3
from pathlib import Path
import sys
import asyncio
from langchain.schema.output_parser import StrOutputParser
from langchain_core import messages
import requests
import typer
from langchain.prompts import ChatPromptTemplate
from loguru import logger
from rich import print
from rich.console import Console
import langchain_helper
import html2text
def prompt_illustrate(content):
instructions = """
Help me create an image to represent this blog post, by making some prompts for GPT. They should be fun whimsical and if they feature someone that person is a 3d render of a cartoon racoon in the pixar style, the raccon can be in his mid 30s and wear glasses
"""
return ChatPromptTemplate.from_messages(
[
messages.SystemMessage(content=instructions),
messages.HumanMessage(content=content),
]
)
def get_text(path):
if not path: # read from stdin
return "".join(sys.stdin.readlines())
# check if path is URL
if path.startswith("http"):
request = requests.get(path)
out = html2text.html2text(request.text)
return out
if path:
# try to open the file, using pathlib
return Path(path).read_text()
# read stdin
return str(sys.stdin.readlines())
async def a_draw(path: str):
llm = langchain_helper.get_model(claude=True)
user_text = get_text(path)
ret = (prompt_illustrate(user_text) | llm | StrOutputParser()).invoke({})
print(ret)
console = Console()
app = typer.Typer()
@app.command()
def draw(
trace: bool = False,
path: str = typer.Argument(None),
):
langchain_helper.langsmith_trace_if_requested(
trace, lambda: asyncio.run(a_draw(path))
)
@logger.catch()
def app_wrap_loguru():
app()
if __name__ == "__main__":
app_wrap_loguru()