File size: 1,742 Bytes
51e2020 |
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 |
import gradio as gr
from utils.inference import shared_state
import re
def convert_to_markdown(text):
text = text.replace("$", "$")
def replace_leading_tabs_and_spaces(line):
new_line = []
for char in line:
if char == "\t":
new_line.append("	")
elif char == " ":
new_line.append(" ")
else:
break
return "".join(new_line) + line[len(new_line) :]
markdown_text = ""
lines = text.split("\n")
in_code_block = False
for line in lines:
if in_code_block is False and line.startswith("```"):
in_code_block = True
markdown_text += "```\n"
elif in_code_block is True and line.startswith("```"):
in_code_block = False
markdown_text += "```\n"
elif in_code_block:
markdown_text += f"{line}\n"
else:
line = replace_leading_tabs_and_spaces(line)
line = re.sub(r"^(#)", r"\\\1", line)
markdown_text += f"{line} \n"
return markdown_text
def reset_textbox():
return gr.update(value=""), ""
def cancel_outputing():
shared_state.interrupt()
textbox = reset_textbox()
return "Stop Done"
def reset_state():
return [], [], "Reset Done"
def transfer_input(inputs):
textbox = reset_textbox()
return (
inputs,
gr.update(value=""),
gr.Button.update(visible=True),
gr.Button.update(visible=True)
)
def delete_last_conversation(chatbot, history):
if len(chatbot) > 0:
chatbot.pop()
if len(history) > 0:
history.pop()
return (
chatbot,
history,
"Delete Done",
) |