forked from srush/MiniChain
-
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
Sasha Rush
committed
Apr 13, 2023
1 parent
94c77ce
commit 7bf9a82
Showing
9 changed files
with
375 additions
and
67 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,52 @@ | ||
# + tags=["hide_inp"] | ||
|
||
desc = """ | ||
### Gradio Tool | ||
Chain that ask for a command-line question and then runs the bash command. [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/srush/MiniChain/blob/master/examples/bash.ipynb) | ||
(Adapted from LangChain [BashChain](https://langchain.readthedocs.io/en/latest/modules/chains/examples/llm_bash.html)) | ||
""" | ||
# - | ||
|
||
# $ | ||
|
||
from minichain import show, prompt, OpenAI, OpenAIStream | ||
import gradio as gr | ||
from gradio_tools.tools import StableDiffusionTool, ImageCaptioningTool | ||
|
||
@prompt(OpenAIStream(), stream=True) | ||
def picture(model, query): | ||
out = "" | ||
for r in model.stream(query): | ||
out += r | ||
yield out | ||
|
||
@prompt(StableDiffusionTool(), stream=True, block_input=lambda: gr.Textbox(label="")) | ||
def gen(model, query): | ||
for r in model.stream(query): | ||
yield "https://htmlcolorcodes.com/assets/images/colors/baby-blue-color-solid-background-1920x1080.png" | ||
yield r | ||
|
||
@prompt(ImageCaptioningTool(), block_output=lambda: gr.Textbox(label="")) | ||
def caption(model, img_src): | ||
return model(img_src) | ||
|
||
def gradio_example(query): | ||
return caption(gen(picture(query))) | ||
|
||
|
||
# $ | ||
|
||
gradio = show(gradio_example, | ||
subprompts=[picture, gen, caption], | ||
examples=['Describe a one-sentence fantasy scene.', | ||
'Describe a one-sentence scene happening on the moon.'], | ||
out_type="markdown", | ||
description=desc, | ||
css="#advanced {display: none}" | ||
|
||
) | ||
if __name__ == "__main__": | ||
gradio.queue().launch() | ||
|
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,31 @@ | ||
You are a utility built to extract structured information from documents. You are returning a TSV table. Here are the headers . | ||
|
||
---- | ||
{{type}} {% for k in player_keys %}{{k[0]}}{{"\t" if not loop.last}}{% endfor %} | ||
---- | ||
|
||
Return the rest of the table in TSV format. Here are some examples | ||
|
||
{% for example in examples %} | ||
Example | ||
--- | ||
{{example.input}} | ||
--- | ||
|
||
Output | ||
--- | ||
{{example.output}} | ||
--- | ||
{% endfor %} | ||
|
||
Article: | ||
---- | ||
{{passage}} | ||
---- | ||
|
||
All other values should be numbers or _. | ||
Only include numbers that appear explicitly in the passage below. | ||
If you cannot find the value in the table, output _. Most cells will be _. | ||
|
||
Ok, here is the correctly valid TSV with headers and nothing else. Remember only include values that are directly written in the article. Do not guess or combine rows. | ||
|
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,81 @@ | ||
# + tags=["hide_inp"] | ||
desc = """ | ||
### Table | ||
""" | ||
# - | ||
|
||
# $ | ||
import pandas as pd | ||
from minichain import prompt, Mock, show, OpenAIStream | ||
import minichain | ||
import json | ||
import gradio as gr | ||
|
||
rotowire = json.load(open("data.json")) | ||
names = { | ||
'3-pointer percentage': 'FG3_PCT', | ||
'3-pointers attempted': 'FG3A', | ||
'3-pointers made': 'FG3M', | ||
'Assists': 'AST', | ||
'Blocks': 'BLK', | ||
'Field goal percentage': 'FG_PCT', | ||
'Field goals attempted': 'FGA', | ||
'Field goals made': 'FGM', | ||
'Free throw percentage': 'FT_PCT', | ||
'Free throws attempted': 'FTA', | ||
'Free throws made': 'FTM', | ||
'Minutes played': 'MIN', | ||
'Personal fouls': 'PF', | ||
'Points': 'PTS', | ||
'Rebounds': 'REB', | ||
'Rebounds (Defensive)': 'DREB', | ||
'Rebounds (Offensive)': 'OREB', | ||
'Steals': 'STL', | ||
'Turnovers': 'TO' | ||
} | ||
# Convert an example to dataframe | ||
def to_df(d): | ||
players = {player for v in d.values() if v is not None for player, _ in v.items()} | ||
lookup = {k: {a: b for a, b in v.items()} for k,v in d.items()} | ||
rows = [{"player": p} | {k: "_" if p not in lookup.get(k, []) else lookup[k][p] for k in names.keys()} | ||
for p in players] | ||
return pd.DataFrame.from_dict(rows).astype("str").sort_values(axis=0, by="player", ignore_index=True).transpose() | ||
|
||
|
||
# Make few shot examples | ||
few_shot_examples = 2 | ||
examples = [] | ||
for i in range(few_shot_examples): | ||
examples.append({"input": rotowire[i][1], | ||
"output": to_df(rotowire[i][0][1]).transpose().set_index("player").to_csv(sep="\t")}) | ||
|
||
@prompt(OpenAIStream(), | ||
template_file="table.pmpt.txt", | ||
block_output=gr.HTML, | ||
stream=True) | ||
def extract(model, passage, typ): | ||
state = [] | ||
out = "" | ||
for token in model.stream(dict(player_keys=names.items(), examples=examples, passage=passage, type=typ)): | ||
out += token | ||
html = "<table><tr><td>" + out.replace("\t", "</td><td>").replace("\n", "</td></tr><tr><td>") + "</td></td></table>" | ||
yield html | ||
yield html | ||
|
||
|
||
|
||
def run(query): | ||
return extract(query, "Player") | ||
|
||
# $ | ||
|
||
gradio = show(run, | ||
examples = [rotowire[i][1] for i in range(50, 55)], | ||
subprompts=[extract], | ||
code=open("table.py", "r").read().split("$")[1].strip().strip("#").strip(), | ||
out_type="markdown" | ||
) | ||
|
||
if __name__ == "__main__": | ||
gradio.queue().launch() |
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 |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
Mock, | ||
OpenAI, | ||
OpenAIEmbed, | ||
OpenAIStream, | ||
Python, | ||
Request, | ||
set_minichain_log, | ||
|
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
Oops, something went wrong.