Spaces:
Runtime error
Runtime error
mkw18 commited on
Commit ·
88d0155
1
Parent(s): 0497b4d
first commit
Browse files- app.py +120 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import mdtex2html
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import random as rd
|
| 5 |
+
import os
|
| 6 |
+
import json
|
| 7 |
+
import time
|
| 8 |
+
import requests
|
| 9 |
+
|
| 10 |
+
rd.seed(time.time())
|
| 11 |
+
|
| 12 |
+
def postprocess(self, y):
|
| 13 |
+
if y is None:
|
| 14 |
+
return []
|
| 15 |
+
for i, (message, response) in enumerate(y):
|
| 16 |
+
y[i] = (
|
| 17 |
+
None if message is None else mdtex2html.convert((message)),
|
| 18 |
+
None if response is None else mdtex2html.convert(response),
|
| 19 |
+
)
|
| 20 |
+
return y
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
gr.Chatbot.postprocess = postprocess
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def parse_text(text):
|
| 27 |
+
"""copy from https://github.com/GaiZhenbiao/ChuanhuChatGPT/"""
|
| 28 |
+
lines = text.split("\n")
|
| 29 |
+
lines = [line for line in lines if line != ""]
|
| 30 |
+
count = 0
|
| 31 |
+
for i, line in enumerate(lines):
|
| 32 |
+
if "```" in line:
|
| 33 |
+
count += 1
|
| 34 |
+
items = line.split('`')
|
| 35 |
+
if count % 2 == 1:
|
| 36 |
+
lines[i] = f'<pre><code class="language-{items[-1]}">'
|
| 37 |
+
else:
|
| 38 |
+
lines[i] = f'<br></code></pre>'
|
| 39 |
+
else:
|
| 40 |
+
if i > 0:
|
| 41 |
+
if count % 2 == 1:
|
| 42 |
+
line = line.replace("`", "\`")
|
| 43 |
+
line = line.replace("<", "<")
|
| 44 |
+
line = line.replace(">", ">")
|
| 45 |
+
line = line.replace(" ", " ")
|
| 46 |
+
line = line.replace("*", "*")
|
| 47 |
+
line = line.replace("_", "_")
|
| 48 |
+
line = line.replace("-", "-")
|
| 49 |
+
line = line.replace(".", ".")
|
| 50 |
+
line = line.replace("!", "!")
|
| 51 |
+
line = line.replace("(", "(")
|
| 52 |
+
line = line.replace(")", ")")
|
| 53 |
+
line = line.replace("$", "$")
|
| 54 |
+
lines[i] = "<br>"+line
|
| 55 |
+
text = "".join(lines)
|
| 56 |
+
return text
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def showInput(input, chatbot):
|
| 60 |
+
chatbot.append((parse_text(input), ""))
|
| 61 |
+
return chatbot
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def predict(input, chatbot, messages):
|
| 65 |
+
chatbot.append((parse_text(input), ""))
|
| 66 |
+
messages.append({"role": 'user', "content": input})
|
| 67 |
+
response=requests.post(os.environ.get("URL"), data=messages).content
|
| 68 |
+
chatbot[-1] = (parse_text(input), parse_text(response))
|
| 69 |
+
messages.append({"role": "assistant", "content": response})
|
| 70 |
+
return chatbot, messages
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def reset_user_input():
|
| 74 |
+
return gr.update(value='')
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def reset_state():
|
| 78 |
+
chatbot = []
|
| 79 |
+
messages = []
|
| 80 |
+
return chatbot, messages
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def show_hide_answer():
|
| 84 |
+
global show_ans
|
| 85 |
+
if show_ans:
|
| 86 |
+
show_ans = False
|
| 87 |
+
return gr.update(value=""), gr.update(value="Show Answer")
|
| 88 |
+
else:
|
| 89 |
+
show_ans = True
|
| 90 |
+
return gr.update(value=answer), gr.update(value="Hide Answer")
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
with gr.Blocks() as demo:
|
| 94 |
+
gr.HTML("""<h1 align="center">海龟汤</h1>""")
|
| 95 |
+
|
| 96 |
+
chatbot = gr.Chatbot([])
|
| 97 |
+
with gr.Row():
|
| 98 |
+
with gr.Column(scale=4):
|
| 99 |
+
with gr.Column(scale=12):
|
| 100 |
+
user_input = gr.Textbox(show_label=False, placeholder="Input...", lines=10, max_lines=10).style(
|
| 101 |
+
container=False)
|
| 102 |
+
with gr.Column(min_width=32, scale=1):
|
| 103 |
+
submitBtn = gr.Button("Submit", variant="primary")
|
| 104 |
+
emptyBtn = gr.Button("New game")
|
| 105 |
+
with gr.Column(scale=1):
|
| 106 |
+
answer_output = gr.Textbox(show_label=False, lines=13, max_lines=13).style(
|
| 107 |
+
container=False)
|
| 108 |
+
answerBtn = gr.Button("Show Answer")
|
| 109 |
+
|
| 110 |
+
messages = gr.State([])
|
| 111 |
+
|
| 112 |
+
submitBtn.click(predict, [user_input, chatbot, messages], [chatbot, messages],
|
| 113 |
+
show_progress=True)
|
| 114 |
+
submitBtn.click(reset_user_input, [], [user_input])
|
| 115 |
+
|
| 116 |
+
emptyBtn.click(reset_state, outputs=[chatbot, messages], show_progress=True)
|
| 117 |
+
|
| 118 |
+
answerBtn.click(show_hide_answer, outputs=[answer_output, answerBtn])
|
| 119 |
+
|
| 120 |
+
demo.queue().launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
mdtex2html
|
| 3 |
+
pandas
|
| 4 |
+
openpyxl
|
| 5 |
+
requests
|