Spaces:
Runtime error
Runtime error
Commit
·
22cc962
1
Parent(s):
2654324
Add tabs for different types of feedback
Browse files
app.py
CHANGED
|
@@ -13,29 +13,79 @@ from diff_match_patch import diff_match_patch
|
|
| 13 |
import collections
|
| 14 |
from itertools import islice
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
api_key = openai.api_key if api_key == "" or api_key.isspace() else api_key
|
| 18 |
if api_key:
|
| 19 |
llm = PromptLayerChatOpenAI(temperature=0, model_name="gpt-3.5-turbo", openai_api_key=api_key, pl_tags=["grammar"])
|
| 20 |
prompt1 = PromptTemplate(
|
| 21 |
input_variables=["content"],
|
| 22 |
-
template=
|
| 23 |
)
|
| 24 |
chain = LLMChain(llm=llm,
|
| 25 |
prompt=prompt1,
|
| 26 |
memory=ConversationBufferMemory())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
return chain, llm
|
| 29 |
|
| 30 |
|
| 31 |
-
def
|
|
|
|
| 32 |
edited = "\n".join([(chain.run(x) if should_check else x) for x, should_check in split_paragraphs(content)])
|
| 33 |
return diff_words(content, edited) + (edited,)
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
def split_paragraphs(text):
|
| 36 |
-
return [(x, x != "" and not x.startswith("#") and not x.isspace()) for x in text.split("\n")]
|
|
|
|
| 37 |
|
| 38 |
-
def sliding_window(iterable, n):
|
| 39 |
# sliding_window('ABCDEFG', 4) --> ABCD BCDE CDEF DEFG
|
| 40 |
it = iter(iterable)
|
| 41 |
window = collections.deque(islice(it, n), maxlen=n)
|
|
@@ -113,7 +163,14 @@ def select_diff(evt: gr.SelectData, changes):
|
|
| 113 |
# original, edited = text.split("→")
|
| 114 |
return f"Why is it better to change [{original}] to [{edited}]?"
|
| 115 |
|
| 116 |
-
block = gr.Blocks(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
with block:
|
| 118 |
api_key = gr.Textbox(
|
| 119 |
placeholder="Paste your OpenAPI API key here (sk-...)",
|
|
@@ -121,56 +178,97 @@ with block:
|
|
| 121 |
lines=1,
|
| 122 |
type="password"
|
| 123 |
)
|
| 124 |
-
|
| 125 |
-
label="Prompt",
|
| 126 |
-
value="Proofread for grammar and spelling:\n{content}"
|
| 127 |
-
)
|
| 128 |
content = gr.Textbox(
|
| 129 |
-
label="
|
| 130 |
)
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
)
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
}
|
| 150 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
changes = gr.State()
|
| 163 |
edited = gr.State()
|
| 164 |
chain = gr.State()
|
| 165 |
llm = gr.State()
|
|
|
|
|
|
|
| 166 |
|
| 167 |
-
api_key.change(load_chain, [api_key
|
| 168 |
-
prompt.change(load_chain, [api_key, prompt], [chain, llm])
|
| 169 |
|
| 170 |
inputs = [content, chain]
|
| 171 |
outputs = [output_before, output_after, changes, edited]
|
| 172 |
-
content.submit(
|
| 173 |
-
submit.click(
|
| 174 |
|
| 175 |
output_before.select(select_diff, changes, followup_question)
|
| 176 |
output_after.select(select_diff, changes, followup_question)
|
|
@@ -180,4 +278,8 @@ with block:
|
|
| 180 |
followup_question.submit(run2, inputs2, outputs2)
|
| 181 |
followup_submit.click(run2, inputs2, outputs2)
|
| 182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
block.launch(debug=True)
|
|
|
|
| 13 |
import collections
|
| 14 |
from itertools import islice
|
| 15 |
|
| 16 |
+
GRAMMAR_PROMPT = "Proofread for grammar and spelling without adding new paragraphs:\n{content}"
|
| 17 |
+
|
| 18 |
+
INTRO_PROMPT = """These are the parts of a good introductory paragraph:
|
| 19 |
+
1. Introduction
|
| 20 |
+
2. The stage of human development of the main character
|
| 21 |
+
3. Summary of story
|
| 22 |
+
4. Thesis statement and outline of topics covered in essay
|
| 23 |
+
For each part, say how confident you are (percentage) that the following paragraph has it, and put a quote of the sentence(s) that fulfil that part.
|
| 24 |
+
{content}"""
|
| 25 |
+
BODY_PROMPT1 = """Complete the following tasks for the following essay paragraph:
|
| 26 |
+
1. Identify the topic sentence and determine whether it introduces an argument
|
| 27 |
+
2. Outline a bullet list of key points
|
| 28 |
+
3. Give a bullet list of parts of the paragraph that use quotes or other textual evidence from the book
|
| 29 |
+
|
| 30 |
+
{content}"""
|
| 31 |
+
BODY_PROMPT2 = """4. Give advice on how the topic sentence could be made stronger or clearer
|
| 32 |
+
5. In a bullet list, state how each key point supports the topic (or if any doesn't support it)
|
| 33 |
+
6. In a bullet list for each textual evidence, state whether the evidence is based on the book or uses outside sources
|
| 34 |
+
7. Group the textual evidence from the book into different bullet lists based on which key point they support"""
|
| 35 |
+
|
| 36 |
+
def load_chain(api_key):
|
| 37 |
api_key = openai.api_key if api_key == "" or api_key.isspace() else api_key
|
| 38 |
if api_key:
|
| 39 |
llm = PromptLayerChatOpenAI(temperature=0, model_name="gpt-3.5-turbo", openai_api_key=api_key, pl_tags=["grammar"])
|
| 40 |
prompt1 = PromptTemplate(
|
| 41 |
input_variables=["content"],
|
| 42 |
+
template=GRAMMAR_PROMPT
|
| 43 |
)
|
| 44 |
chain = LLMChain(llm=llm,
|
| 45 |
prompt=prompt1,
|
| 46 |
memory=ConversationBufferMemory())
|
| 47 |
+
chain_intro = LLMChain(llm=llm,
|
| 48 |
+
prompt=PromptTemplate(
|
| 49 |
+
input_variables=["content"],
|
| 50 |
+
template=INTRO_PROMPT
|
| 51 |
+
),
|
| 52 |
+
memory=ConversationBufferMemory())
|
| 53 |
+
chain_body1 = LLMChain(llm=llm,
|
| 54 |
+
prompt=PromptTemplate(
|
| 55 |
+
input_variables=["content"],
|
| 56 |
+
template=BODY_PROMPT1
|
| 57 |
+
),
|
| 58 |
+
memory=ConversationBufferMemory())
|
| 59 |
|
| 60 |
+
return chain, llm, chain_intro, chain_body1
|
| 61 |
|
| 62 |
|
| 63 |
+
def run_diff(content, chain):
|
| 64 |
+
chain.memory.clear()
|
| 65 |
edited = "\n".join([(chain.run(x) if should_check else x) for x, should_check in split_paragraphs(content)])
|
| 66 |
return diff_words(content, edited) + (edited,)
|
| 67 |
|
| 68 |
+
def run(content, chain):
|
| 69 |
+
chain.memory.clear()
|
| 70 |
+
return chain.run(content)
|
| 71 |
+
|
| 72 |
+
def run_body(content, chain, llm):
|
| 73 |
+
output1 = run(content, chain)
|
| 74 |
+
output2 = run2(BODY_PROMPT2, chain, llm)
|
| 75 |
+
return output1 + "\n\n" + output2
|
| 76 |
+
|
| 77 |
+
def run_custom(content, llm, prompt):
|
| 78 |
+
return LLMChain(llm=llm,
|
| 79 |
+
prompt=PromptTemplate(
|
| 80 |
+
input_variables=["content"],
|
| 81 |
+
template=prompt
|
| 82 |
+
)).run(content)
|
| 83 |
+
|
| 84 |
def split_paragraphs(text):
|
| 85 |
+
# return [(x, x != "" and not x.startswith("#") and not x.isspace()) for x in text.split("\n")]
|
| 86 |
+
return [(text, True)]
|
| 87 |
|
| 88 |
+
def sliding_window(iterable, n):
|
| 89 |
# sliding_window('ABCDEFG', 4) --> ABCD BCDE CDEF DEFG
|
| 90 |
it = iter(iterable)
|
| 91 |
window = collections.deque(islice(it, n), maxlen=n)
|
|
|
|
| 163 |
# original, edited = text.split("→")
|
| 164 |
return f"Why is it better to change [{original}] to [{edited}]?"
|
| 165 |
|
| 166 |
+
block = gr.Blocks(css="""
|
| 167 |
+
.diff-component {
|
| 168 |
+
white-space: pre-wrap;
|
| 169 |
+
}
|
| 170 |
+
.diff-component .textspan.hl {
|
| 171 |
+
white-space: normal;
|
| 172 |
+
}
|
| 173 |
+
""")
|
| 174 |
with block:
|
| 175 |
api_key = gr.Textbox(
|
| 176 |
placeholder="Paste your OpenAPI API key here (sk-...)",
|
|
|
|
| 178 |
lines=1,
|
| 179 |
type="password"
|
| 180 |
)
|
| 181 |
+
gr.Markdown("""Paste a paragraph below, then choose one of the modes to generate feedback.""")
|
|
|
|
|
|
|
|
|
|
| 182 |
content = gr.Textbox(
|
| 183 |
+
label="Paragraph"
|
| 184 |
)
|
| 185 |
+
|
| 186 |
+
with gr.Tab("Grammar/Spelling"):
|
| 187 |
+
gr.Markdown("Suggests grammar and spelling revisions.")
|
| 188 |
+
submit = gr.Button(
|
| 189 |
+
value="Revise",
|
| 190 |
+
).style(full_width=False)
|
| 191 |
+
|
| 192 |
+
with gr.Row():
|
| 193 |
+
output_before = gr.HighlightedText(
|
| 194 |
+
label="Before",
|
| 195 |
+
combine_adjacent=True,
|
| 196 |
+
elem_classes="diff-component"
|
| 197 |
+
).style(color_map={
|
| 198 |
+
"-": "red",
|
| 199 |
+
# "→": "yellow",
|
| 200 |
+
})
|
| 201 |
+
output_after = gr.HighlightedText(
|
| 202 |
+
label="After",
|
| 203 |
+
combine_adjacent=True,
|
| 204 |
+
elem_classes="diff-component"
|
| 205 |
+
).style(color_map={
|
| 206 |
+
"+": "green",
|
| 207 |
+
# "→": "yellow",
|
| 208 |
+
})
|
| 209 |
+
|
| 210 |
+
followup_question = gr.Textbox(
|
| 211 |
+
label="Follow-up Question",
|
| 212 |
)
|
| 213 |
+
followup_submit = gr.Button(
|
| 214 |
+
value="Ask"
|
| 215 |
+
).style(full_width=False)
|
| 216 |
+
followup_answer = gr.Textbox(
|
| 217 |
+
label="Answer"
|
|
|
|
| 218 |
)
|
| 219 |
+
with gr.Tab("Intro"):
|
| 220 |
+
gr.Markdown("Checks for the key components of an introductory paragraph.")
|
| 221 |
+
submit_intro = gr.Button(
|
| 222 |
+
value="Run"
|
| 223 |
+
).style(full_width=False)
|
| 224 |
|
| 225 |
+
output_intro = gr.Textbox(
|
| 226 |
+
label="Output"
|
| 227 |
+
)
|
| 228 |
+
with gr.Tab("Body Paragraph"):
|
| 229 |
+
gr.Markdown("""1. identifies the topic sentence
|
| 230 |
+
2. outlines key points
|
| 231 |
+
3. checks for quotes/concrete details (e.g., textual evidence)
|
| 232 |
+
4. suggests topic sentence improvements
|
| 233 |
+
5. checks that the outline matches the topic
|
| 234 |
+
6. checks whether the quotes/evidence are from the book
|
| 235 |
+
7. checks that key points use textual evidence from the book
|
| 236 |
+
8. checks for further critical analysis""")
|
| 237 |
+
submit_body = gr.Button(
|
| 238 |
+
value="Run"
|
| 239 |
+
).style(full_width=False)
|
| 240 |
+
|
| 241 |
+
output_body = gr.Textbox(
|
| 242 |
+
label="Output"
|
| 243 |
+
)
|
| 244 |
+
with gr.Tab("Custom prompt"):
|
| 245 |
+
gr.Markdown("This mode is for testing and debugging.")
|
| 246 |
+
prompt = gr.Textbox(
|
| 247 |
+
label="Prompt",
|
| 248 |
+
value=GRAMMAR_PROMPT,
|
| 249 |
+
lines=2
|
| 250 |
+
)
|
| 251 |
+
submit_custom = gr.Button(
|
| 252 |
+
value="Run"
|
| 253 |
+
).style(full_width=False)
|
| 254 |
+
|
| 255 |
+
output_custom = gr.Textbox(
|
| 256 |
+
label="Output"
|
| 257 |
+
)
|
| 258 |
|
| 259 |
changes = gr.State()
|
| 260 |
edited = gr.State()
|
| 261 |
chain = gr.State()
|
| 262 |
llm = gr.State()
|
| 263 |
+
chain_intro = gr.State()
|
| 264 |
+
chain_body1 = gr.State()
|
| 265 |
|
| 266 |
+
api_key.change(load_chain, [api_key], [chain, llm, chain_intro, chain_body1])
|
|
|
|
| 267 |
|
| 268 |
inputs = [content, chain]
|
| 269 |
outputs = [output_before, output_after, changes, edited]
|
| 270 |
+
# content.submit(run_diff, inputs=inputs, outputs=outputs)
|
| 271 |
+
submit.click(run_diff, inputs=inputs, outputs=outputs)
|
| 272 |
|
| 273 |
output_before.select(select_diff, changes, followup_question)
|
| 274 |
output_after.select(select_diff, changes, followup_question)
|
|
|
|
| 278 |
followup_question.submit(run2, inputs2, outputs2)
|
| 279 |
followup_submit.click(run2, inputs2, outputs2)
|
| 280 |
|
| 281 |
+
submit_intro.click(run, [content, chain_intro], output_intro)
|
| 282 |
+
submit_body.click(run_body, [content, chain_body1, llm], output_body) # body part A only
|
| 283 |
+
submit_custom.click(run_custom, [content, llm, prompt], output_custom)
|
| 284 |
+
|
| 285 |
block.launch(debug=True)
|