James Wade commited on
Commit
b9f7af9
·
1 Parent(s): e58f0cc

tweak shiny deployment

Browse files
Files changed (1) hide show
  1. app.py +54 -20
app.py CHANGED
@@ -3,62 +3,96 @@ from chemcrow.agents import ChemCrow
3
  import asyncio
4
  import shinyswatch
5
 
 
 
 
6
  app_ui = ui.page_fluid(
7
  shinyswatch.theme.slate(),
8
  ui.panel_title("ChemCrow UI"),
9
  ui.p("An experiment with Shiny for Python and ChemCrow"),
10
  ui.br(),
11
  ui.row(
12
- ui.column(9, ui.input_text("prompt", label=None, placeholder="E.g., What is the molecular weight of tylenol?", width="100%")),
13
- ui.column(3, ui.input_action_button("chat", "Chat", class_="btn btn-primary btn-lg btn-block", width="100%"),)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  ),
15
- ui.help_text("Example 1: Propose a novel organicatalyst for enhancing carbon dioxide conversion in carbon capture and utilization processes."),
16
  ui.br(),
17
- ui.help_text("Example 2: What are the products of the reaction between 2-bromo-2-methylpropane and 4-(4-hydroxyphenyl)butan-2-one. Can this reaction run without problems?"),
 
 
18
  ui.output_text("txt"),
19
  ui.output_ui("prompt_ui"),
20
  ui.output_ui("result"),
21
  ui.hr(),
22
  ui.div(
23
- {"style": "align-items: center; display: flex; flex-direction: column; justify-content: center;"},
24
- ui.img(src="https://github.com/ur-whitelab/chemcrow-public/raw/main/assets/chemcrow_dark_thin.png", width="400px")
 
 
 
 
 
25
  ),
26
  ui.br(),
27
- ui.markdown(f'ChemCrow was [introduced](https://arxiv.org/abs/2304.05376) by Bran, Andres M., et al. "ChemCrow: Augmenting large-language models with chemistry tools." arXiv preprint arXiv:2304.05376 (2023). This tool is an extension of that work that puts the code into an interactive web app created by [James Wade](https://jameshwade.com) using [Shiny for Python](https://shiny.posit.co/py/). Find the code for the app [here](https://github.com/jameshwade/chemcrow) and the original code [here](https://github.com/ur-whitelab/chemcrow-public).')
 
 
28
  )
29
 
 
30
  def server(input, output, session):
31
  @reactive.Effect()
32
  def _():
33
  if input.chat():
34
  ui.update_text("prompt", value="")
35
-
36
  @output
37
  @render.ui
38
  @reactive.event(input.chat)
39
  def prompt_ui():
40
- list_ui = [ui.strong("Prompt"),
41
- ui.markdown(input.prompt())]
42
  return list_ui
43
 
44
  @output
45
  @render.ui
46
- @reactive.event(input.chat) # triggered when the "Chat" button is clicked
47
  async def result():
48
  ui.notification_show("Chatting with ChemCrow...", type="message")
49
  try:
50
- chem_model = ChemCrow(model="gpt-4-0613", temp=0.1, verbose=True)
51
- response = await asyncio.to_thread(chem_model.run, input.prompt())
52
- list_ui = [ui.strong("Thoughts"),
53
- ui.markdown(response[0]),
54
- ui.strong("Reasoning"),
55
- ui.markdown(response[1]),
56
- ui.strong("Answer"),
57
- ui.markdown(response[2])]
 
 
 
 
 
 
 
58
  return list_ui
59
  except TypeError:
60
  async def error_coro():
61
  return "An error occurred while processing your request."
62
  return await error_coro()
63
 
64
- app = App(app_ui, server, debug=True)
 
3
  import asyncio
4
  import shinyswatch
5
 
6
+ # initialize the model
7
+ chem_model = ChemCrow(model="gpt-4-0613", tools_model="gpt-4-0613", temp=0.1, verbose=True)
8
+
9
  app_ui = ui.page_fluid(
10
  shinyswatch.theme.slate(),
11
  ui.panel_title("ChemCrow UI"),
12
  ui.p("An experiment with Shiny for Python and ChemCrow"),
13
  ui.br(),
14
  ui.row(
15
+ ui.column(
16
+ 9,
17
+ ui.input_text(
18
+ "prompt",
19
+ label=None,
20
+ placeholder="E.g., What is the molecular weight of tylenol?",
21
+ width="100%",
22
+ ),
23
+ ),
24
+ ui.column(
25
+ 3,
26
+ ui.input_action_button(
27
+ "chat", "Chat", class_="btn btn-primary btn-lg btn-block", width="100%"
28
+ ),
29
+ ),
30
+ ),
31
+ ui.help_text(
32
+ "Example 1: Propose a novel organicatalyst for enhancing carbon dioxide conversion in carbon capture and utilization processes."
33
  ),
 
34
  ui.br(),
35
+ ui.help_text(
36
+ "Example 2: What are the products of the reaction between 2-bromo-2-methylpropane and 4-(4-hydroxyphenyl)butan-2-one. Can this reaction run without problems?"
37
+ ),
38
  ui.output_text("txt"),
39
  ui.output_ui("prompt_ui"),
40
  ui.output_ui("result"),
41
  ui.hr(),
42
  ui.div(
43
+ {
44
+ "style": "align-items: center; display: flex; flex-direction: column; justify-content: center;"
45
+ },
46
+ ui.img(
47
+ src="https://github.com/ur-whitelab/chemcrow-public/raw/main/assets/chemcrow_dark_thin.png",
48
+ width="400px",
49
+ ),
50
  ),
51
  ui.br(),
52
+ ui.markdown(
53
+ f'ChemCrow was [introduced](https://arxiv.org/abs/2304.05376) by Bran, Andres M., et al. "ChemCrow: Augmenting large-language models with chemistry tools." arXiv preprint arXiv:2304.05376 (2023). This tool is an extension of that work that puts the code into an interactive web app created by [James Wade](https://jameshwade.com) using [Shiny for Python](https://shiny.posit.co/py/). Find the code for the app [here](https://github.com/jameshwade/chemcrow) and the original code [here](https://github.com/ur-whitelab/chemcrow-public).'
54
+ ),
55
  )
56
 
57
+
58
  def server(input, output, session):
59
  @reactive.Effect()
60
  def _():
61
  if input.chat():
62
  ui.update_text("prompt", value="")
63
+
64
  @output
65
  @render.ui
66
  @reactive.event(input.chat)
67
  def prompt_ui():
68
+ list_ui = [ui.strong("Prompt"), ui.markdown(input.prompt())]
 
69
  return list_ui
70
 
71
  @output
72
  @render.ui
73
+ @reactive.event(input.chat) # triggered when the "Chat" button is clicked
74
  async def result():
75
  ui.notification_show("Chatting with ChemCrow...", type="message")
76
  try:
77
+
78
+ list_ui = []
79
+ # run the model and handle output as it's being produced
80
+ async for response in chem_model.run(input.prompt()):
81
+ if isinstance(response, str):
82
+ list_ui.append(ui.markdown(response))
83
+ else:
84
+ list_ui.extend([
85
+ ui.strong("Thoughts"),
86
+ ui.markdown(response[0]),
87
+ ui.strong("Reasoning"),
88
+ ui.markdown(response[1]),
89
+ ui.strong("Answer"),
90
+ ui.markdown(response[2]),
91
+ ])
92
  return list_ui
93
  except TypeError:
94
  async def error_coro():
95
  return "An error occurred while processing your request."
96
  return await error_coro()
97
 
98
+ app = App(app_ui, server)