lewtun HF staff commited on
Commit
5625528
1 Parent(s): c05fb53
Files changed (2) hide show
  1. app.ipynb +37 -36
  2. app.py +14 -7
app.ipynb CHANGED
@@ -2,7 +2,7 @@
2
  "cells": [
3
  {
4
  "cell_type": "code",
5
- "execution_count": 14,
6
  "metadata": {},
7
  "outputs": [],
8
  "source": [
@@ -18,10 +18,11 @@
18
  },
19
  {
20
  "cell_type": "code",
21
- "execution_count": null,
22
  "metadata": {},
23
  "outputs": [],
24
  "source": [
 
25
  "if Path(\".env\").is_file():\n",
26
  " load_dotenv(\".env\")\n",
27
  "\n",
@@ -30,7 +31,7 @@
30
  },
31
  {
32
  "cell_type": "code",
33
- "execution_count": 2,
34
  "metadata": {},
35
  "outputs": [],
36
  "source": [
@@ -42,7 +43,7 @@
42
  " top_p\n",
43
  "):\n",
44
  " API_URL = f\"https://api-inference.huggingface.co/models/{model_id}\"\n",
45
- " headers = {\"Authorization\": \"Bearer \", \"x-wait-for-model\": \"1\"}\n",
46
  "\n",
47
  " payload = {\n",
48
  " \"inputs\": inputs,\n",
@@ -64,16 +65,16 @@
64
  },
65
  {
66
  "cell_type": "code",
67
- "execution_count": 3,
68
  "metadata": {},
69
  "outputs": [
70
  {
71
  "data": {
72
  "text/plain": [
73
- "[{'generated_text': 'YES'}]"
74
  ]
75
  },
76
- "execution_count": 3,
77
  "metadata": {},
78
  "output_type": "execute_result"
79
  }
@@ -86,7 +87,7 @@
86
  },
87
  {
88
  "cell_type": "code",
89
- "execution_count": 4,
90
  "metadata": {},
91
  "outputs": [],
92
  "source": [
@@ -100,7 +101,7 @@
100
  },
101
  {
102
  "cell_type": "code",
103
- "execution_count": 5,
104
  "metadata": {},
105
  "outputs": [],
106
  "source": [
@@ -129,6 +130,14 @@
129
  " return {chatbot: chat, state: history}\n"
130
  ]
131
  },
 
 
 
 
 
 
 
 
132
  {
133
  "cell_type": "code",
134
  "execution_count": 6,
@@ -644,9 +653,17 @@
644
  " json.dump({\"prompt\": template}, f)"
645
  ]
646
  },
 
 
 
 
 
 
 
 
647
  {
648
  "cell_type": "code",
649
- "execution_count": 28,
650
  "metadata": {},
651
  "outputs": [],
652
  "source": [
@@ -678,14 +695,14 @@
678
  },
679
  {
680
  "cell_type": "code",
681
- "execution_count": 36,
682
  "metadata": {},
683
  "outputs": [
684
  {
685
  "name": "stdout",
686
  "output_type": "stream",
687
  "text": [
688
- "Running on local URL: http://127.0.0.1:7871\n",
689
  "\n",
690
  "To create a public link, set `share=True` in `launch()`.\n"
691
  ]
@@ -693,7 +710,7 @@
693
  {
694
  "data": {
695
  "text/html": [
696
- "<div><iframe src=\"http://127.0.0.1:7871/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
697
  ],
698
  "text/plain": [
699
  "<IPython.core.display.HTML object>"
@@ -706,25 +723,9 @@
706
  "data": {
707
  "text/plain": []
708
  },
709
- "execution_count": 36,
710
  "metadata": {},
711
  "output_type": "execute_result"
712
- },
713
- {
714
- "name": "stderr",
715
- "output_type": "stream",
716
- "text": [
717
- "Traceback (most recent call last):\n",
718
- " File \"/Users/lewtun/miniconda3/envs/hf/lib/python3.8/site-packages/gradio/routes.py\", line 337, in run_predict\n",
719
- " output = await app.get_blocks().process_api(\n",
720
- " File \"/Users/lewtun/miniconda3/envs/hf/lib/python3.8/site-packages/gradio/blocks.py\", line 1018, in process_api\n",
721
- " data = self.postprocess_data(fn_index, result[\"prediction\"], state)\n",
722
- " File \"/Users/lewtun/miniconda3/envs/hf/lib/python3.8/site-packages/gradio/blocks.py\", line 924, in postprocess_data\n",
723
- " predictions = convert_component_dict_to_list(\n",
724
- " File \"/Users/lewtun/miniconda3/envs/hf/lib/python3.8/site-packages/gradio/blocks.py\", line 397, in convert_component_dict_to_list\n",
725
- " raise ValueError(\n",
726
- "ValueError: Returned component chatbot not specified as output of function.\n"
727
- ]
728
  }
729
  ],
730
  "source": [
@@ -761,9 +762,9 @@
761
  " interactive=True,\n",
762
  " )\n",
763
  " temperature = gr.Slider(\n",
764
- " minimum=0.5,\n",
765
  " maximum=3.0,\n",
766
- " value=1.0,\n",
767
  " step=0.1,\n",
768
  " interactive=True,\n",
769
  " label=\"Temperature\",\n",
@@ -772,7 +773,7 @@
772
  " top_p = gr.Slider(\n",
773
  " minimum=-0,\n",
774
  " maximum=1.0,\n",
775
- " value=0.8,\n",
776
  " step=0.05,\n",
777
  " interactive=True,\n",
778
  " label=\"Top-p (nucleus sampling)\",\n",
@@ -828,14 +829,14 @@
828
  },
829
  {
830
  "cell_type": "code",
831
- "execution_count": 193,
832
  "metadata": {},
833
  "outputs": [
834
  {
835
  "name": "stdout",
836
  "output_type": "stream",
837
  "text": [
838
- "Closing server running on port: 7905\n"
839
  ]
840
  }
841
  ],
@@ -845,7 +846,7 @@
845
  },
846
  {
847
  "cell_type": "code",
848
- "execution_count": 38,
849
  "metadata": {},
850
  "outputs": [],
851
  "source": [
 
2
  "cells": [
3
  {
4
  "cell_type": "code",
5
+ "execution_count": 1,
6
  "metadata": {},
7
  "outputs": [],
8
  "source": [
 
18
  },
19
  {
20
  "cell_type": "code",
21
+ "execution_count": 11,
22
  "metadata": {},
23
  "outputs": [],
24
  "source": [
25
+ "# |export\n",
26
  "if Path(\".env\").is_file():\n",
27
  " load_dotenv(\".env\")\n",
28
  "\n",
 
31
  },
32
  {
33
  "cell_type": "code",
34
+ "execution_count": 3,
35
  "metadata": {},
36
  "outputs": [],
37
  "source": [
 
43
  " top_p\n",
44
  "):\n",
45
  " API_URL = f\"https://api-inference.huggingface.co/models/{model_id}\"\n",
46
+ " headers = {\"Authorization\": f\"Bearer {HF_TOKEN}\", \"x-wait-for-model\": \"1\"}\n",
47
  "\n",
48
  " payload = {\n",
49
  " \"inputs\": inputs,\n",
 
65
  },
66
  {
67
  "cell_type": "code",
68
+ "execution_count": 4,
69
  "metadata": {},
70
  "outputs": [
71
  {
72
  "data": {
73
  "text/plain": [
74
+ "[{'generated_text': 'love'}]"
75
  ]
76
  },
77
+ "execution_count": 4,
78
  "metadata": {},
79
  "output_type": "execute_result"
80
  }
 
87
  },
88
  {
89
  "cell_type": "code",
90
+ "execution_count": 5,
91
  "metadata": {},
92
  "outputs": [],
93
  "source": [
 
101
  },
102
  {
103
  "cell_type": "code",
104
+ "execution_count": 9,
105
  "metadata": {},
106
  "outputs": [],
107
  "source": [
 
130
  " return {chatbot: chat, state: history}\n"
131
  ]
132
  },
133
+ {
134
+ "attachments": {},
135
+ "cell_type": "markdown",
136
+ "metadata": {},
137
+ "source": [
138
+ "## Prompt templates"
139
+ ]
140
+ },
141
  {
142
  "cell_type": "code",
143
  "execution_count": 6,
 
653
  " json.dump({\"prompt\": template}, f)"
654
  ]
655
  },
656
+ {
657
+ "attachments": {},
658
+ "cell_type": "markdown",
659
+ "metadata": {},
660
+ "source": [
661
+ "## App"
662
+ ]
663
+ },
664
  {
665
  "cell_type": "code",
666
+ "execution_count": 5,
667
  "metadata": {},
668
  "outputs": [],
669
  "source": [
 
695
  },
696
  {
697
  "cell_type": "code",
698
+ "execution_count": 6,
699
  "metadata": {},
700
  "outputs": [
701
  {
702
  "name": "stdout",
703
  "output_type": "stream",
704
  "text": [
705
+ "Running on local URL: http://127.0.0.1:7860\n",
706
  "\n",
707
  "To create a public link, set `share=True` in `launch()`.\n"
708
  ]
 
710
  {
711
  "data": {
712
  "text/html": [
713
+ "<div><iframe src=\"http://127.0.0.1:7860/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
714
  ],
715
  "text/plain": [
716
  "<IPython.core.display.HTML object>"
 
723
  "data": {
724
  "text/plain": []
725
  },
726
+ "execution_count": 6,
727
  "metadata": {},
728
  "output_type": "execute_result"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
729
  }
730
  ],
731
  "source": [
 
762
  " interactive=True,\n",
763
  " )\n",
764
  " temperature = gr.Slider(\n",
765
+ " minimum=0.0,\n",
766
  " maximum=3.0,\n",
767
+ " value=0.5,\n",
768
  " step=0.1,\n",
769
  " interactive=True,\n",
770
  " label=\"Temperature\",\n",
 
773
  " top_p = gr.Slider(\n",
774
  " minimum=-0,\n",
775
  " maximum=1.0,\n",
776
+ " value=0.9,\n",
777
  " step=0.05,\n",
778
  " interactive=True,\n",
779
  " label=\"Top-p (nucleus sampling)\",\n",
 
829
  },
830
  {
831
  "cell_type": "code",
832
+ "execution_count": 7,
833
  "metadata": {},
834
  "outputs": [
835
  {
836
  "name": "stdout",
837
  "output_type": "stream",
838
  "text": [
839
+ "Closing server running on port: 7860\n"
840
  ]
841
  }
842
  ],
 
846
  },
847
  {
848
  "cell_type": "code",
849
+ "execution_count": 10,
850
  "metadata": {},
851
  "outputs": [],
852
  "source": [
app.py CHANGED
@@ -1,7 +1,7 @@
1
  # AUTOGENERATED! DO NOT EDIT! File to edit: app.ipynb.
2
 
3
  # %% auto 0
4
- __all__ = ['title', 'description', 'query_chat_api', 'inference_chat']
5
 
6
  # %% app.ipynb 0
7
  import gradio as gr
@@ -13,6 +13,13 @@ from pathlib import Path
13
  from dotenv import load_dotenv
14
 
15
 
 
 
 
 
 
 
 
16
  # %% app.ipynb 2
17
  def query_chat_api(
18
  model_id,
@@ -21,7 +28,7 @@ def query_chat_api(
21
  top_p
22
  ):
23
  API_URL = f"https://api-inference.huggingface.co/models/{model_id}"
24
- headers = {"Authorization": "Bearer ", "x-wait-for-model": "1"}
25
 
26
  payload = {
27
  "inputs": inputs,
@@ -66,7 +73,7 @@ def inference_chat(
66
  return {chatbot: chat, state: history}
67
 
68
 
69
- # %% app.ipynb 13
70
  title = """<h1 align="center">Chatty Language Models</h1>"""
71
  description = """Language models can be conditioned to act like dialogue agents through a conversational prompt that typically takes the form:
72
 
@@ -91,7 +98,7 @@ So far, the following prompts are available:
91
  As you can see, most of these prompts exceed the maximum context size of models like Flan-T5, so an error usually means the Inference API has timed out.
92
  """
93
 
94
- # %% app.ipynb 14
95
  with gr.Blocks(
96
  css="""
97
  .message.svelte-w6rprc.svelte-w6rprc.svelte-w6rprc {font-size: 20px; margin-top: 20px}
@@ -124,9 +131,9 @@ with gr.Blocks(
124
  interactive=True,
125
  )
126
  temperature = gr.Slider(
127
- minimum=0.5,
128
  maximum=3.0,
129
- value=1.0,
130
  step=0.1,
131
  interactive=True,
132
  label="Temperature",
@@ -135,7 +142,7 @@ with gr.Blocks(
135
  top_p = gr.Slider(
136
  minimum=-0,
137
  maximum=1.0,
138
- value=0.8,
139
  step=0.05,
140
  interactive=True,
141
  label="Top-p (nucleus sampling)",
 
1
  # AUTOGENERATED! DO NOT EDIT! File to edit: app.ipynb.
2
 
3
  # %% auto 0
4
+ __all__ = ['HF_TOKEN', 'title', 'description', 'query_chat_api', 'inference_chat']
5
 
6
  # %% app.ipynb 0
7
  import gradio as gr
 
13
  from dotenv import load_dotenv
14
 
15
 
16
+ # %% app.ipynb 1
17
+ if Path(".env").is_file():
18
+ load_dotenv(".env")
19
+
20
+ HF_TOKEN = os.getenv("HF_TOKEN")
21
+
22
+
23
  # %% app.ipynb 2
24
  def query_chat_api(
25
  model_id,
 
28
  top_p
29
  ):
30
  API_URL = f"https://api-inference.huggingface.co/models/{model_id}"
31
+ headers = {"Authorization": f"Bearer {HF_TOKEN}", "x-wait-for-model": "1"}
32
 
33
  payload = {
34
  "inputs": inputs,
 
73
  return {chatbot: chat, state: history}
74
 
75
 
76
+ # %% app.ipynb 15
77
  title = """<h1 align="center">Chatty Language Models</h1>"""
78
  description = """Language models can be conditioned to act like dialogue agents through a conversational prompt that typically takes the form:
79
 
 
98
  As you can see, most of these prompts exceed the maximum context size of models like Flan-T5, so an error usually means the Inference API has timed out.
99
  """
100
 
101
+ # %% app.ipynb 16
102
  with gr.Blocks(
103
  css="""
104
  .message.svelte-w6rprc.svelte-w6rprc.svelte-w6rprc {font-size: 20px; margin-top: 20px}
 
131
  interactive=True,
132
  )
133
  temperature = gr.Slider(
134
+ minimum=0.0,
135
  maximum=3.0,
136
+ value=0.5,
137
  step=0.1,
138
  interactive=True,
139
  label="Temperature",
 
142
  top_p = gr.Slider(
143
  minimum=-0,
144
  maximum=1.0,
145
+ value=0.9,
146
  step=0.05,
147
  interactive=True,
148
  label="Top-p (nucleus sampling)",