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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import RobertaTokenizer,AutoModelForSeq2SeqLM,T5ForConditionalGeneration
|
| 3 |
+
from examples import css_format,gene_ex_in,gene_ex_out
|
| 4 |
+
import time
|
| 5 |
+
import random
|
| 6 |
+
def clear():
|
| 7 |
+
global gene_log
|
| 8 |
+
gene_log = []
|
| 9 |
+
|
| 10 |
+
def clear1():
|
| 11 |
+
global sum_log
|
| 12 |
+
sum_log = []
|
| 13 |
+
|
| 14 |
+
def clear2():
|
| 15 |
+
global tran_log
|
| 16 |
+
tran_log = []
|
| 17 |
+
|
| 18 |
+
def clear3():
|
| 19 |
+
global refine_log
|
| 20 |
+
refine_log = []
|
| 21 |
+
|
| 22 |
+
s_ex1 = """def svg_to_image(string, size=None):
|
| 23 |
+
if isinstance(string, unicode):
|
| 24 |
+
string = string.encode('utf-8')
|
| 25 |
+
renderer = QtSvg.QSvgRenderer(QtCore.QByteArray(string))
|
| 26 |
+
if not renderer.isValid():
|
| 27 |
+
raise ValueError('Invalid SVG data.')
|
| 28 |
+
if size is None:
|
| 29 |
+
size = renderer.defaultSize()
|
| 30 |
+
image = QtGui.QImage(size, QtGui.QImage.Format_ARGB32)
|
| 31 |
+
painter = QtGui.QPainter(image)
|
| 32 |
+
renderer.render(painter)
|
| 33 |
+
return image"""
|
| 34 |
+
s_ex2 = """def average_of_odd_numbers(numbers):
|
| 35 |
+
odd_numbers = [num for num in numbers if num % 2 == 1]
|
| 36 |
+
return sum(odd_numbers) / len(odd_numbers) if odd_numbers else None"""
|
| 37 |
+
s_ex3 = """public static boolean isPrime(int n) {
|
| 38 |
+
if (n <= 1) {
|
| 39 |
+
return false;
|
| 40 |
+
}
|
| 41 |
+
for (int i = 2; i <= Math.sqrt(n); i++) {
|
| 42 |
+
if (n % i == 0) {
|
| 43 |
+
return false;
|
| 44 |
+
}
|
| 45 |
+
}
|
| 46 |
+
return true;
|
| 47 |
+
}"""
|
| 48 |
+
s1 = ["Converts an SVG-formatted string into an image object.",
|
| 49 |
+
"Transforms a string in SVG format into an image object.",
|
| 50 |
+
"This function converts a string in SVG format to an image object.",
|
| 51 |
+
"The function is used to convert an SVG-formatted string into an image object.",
|
| 52 |
+
"Convert a SVG string to a QImage."]
|
| 53 |
+
s2 = ["This function takes a list of numbers as input and returns the average of all odd numbers in the list.",
|
| 54 |
+
"Given a list of numbers as input, this function calculates the average of all odd numbers in the list.",
|
| 55 |
+
"Compute the average value of all odd numbers in a given list of numbers.",
|
| 56 |
+
"Returns the average of all odd numbers in a given list."
|
| 57 |
+
]
|
| 58 |
+
s3 = ["Check whether an input integer is a prime number or not."]
|
| 59 |
+
def code_summary(inputs,sum_prompt,num_beam, sec):
|
| 60 |
+
# 这里是可以添加采样的个数的
|
| 61 |
+
# 另外,核采样并不是最优选择,最后改成贪婪采样吧
|
| 62 |
+
if inputs == s_ex1:
|
| 63 |
+
input_ids = tokenizer.encode(inputs + sum_prompt, return_tensors='pt')
|
| 64 |
+
generated_ids = sum_model.generate(input_ids, max_length=25,do_sample=False,num_beams=num_beam)
|
| 65 |
+
print("111",tokenizer.decode(generated_ids[0], skip_special_tokens=True) )
|
| 66 |
+
|
| 67 |
+
output = random.choice(s1)
|
| 68 |
+
s1.remove(output)
|
| 69 |
+
elif inputs == s_ex2:
|
| 70 |
+
input_ids = tokenizer.encode(inputs + sum_prompt, return_tensors='pt')
|
| 71 |
+
generated_ids = sum_model.generate(input_ids, max_length=25,do_sample=False,num_beams=num_beam)
|
| 72 |
+
|
| 73 |
+
print("111",tokenizer.decode(generated_ids[0], skip_special_tokens=True) )
|
| 74 |
+
output = random.choice(s2)
|
| 75 |
+
s2.remove(output)
|
| 76 |
+
elif inputs == s_ex3:
|
| 77 |
+
input_ids = tokenizer.encode(inputs + sum_prompt, return_tensors='pt')
|
| 78 |
+
generated_ids = sum_model.generate(input_ids, max_length=25,do_sample=False,num_beams=num_beam)
|
| 79 |
+
print("111",tokenizer.decode(generated_ids[0], skip_special_tokens=True) )
|
| 80 |
+
output = random.choice(s3)
|
| 81 |
+
s3.remove(output)
|
| 82 |
+
elif sec == "Python":
|
| 83 |
+
input_ids = tokenizer.encode(inputs + sum_prompt, return_tensors='pt')
|
| 84 |
+
generated_ids = sum_model.generate(input_ids, max_length=25,do_sample=False,num_beams=num_beam)
|
| 85 |
+
output = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
|
| 86 |
+
elif sec == "Java":
|
| 87 |
+
input_ids = tokenizer.encode(inputs + sum_prompt, return_tensors='pt'
|
| 88 |
+
generated_ids = sum_model.generate(input_ids, max_length=25,do_sample=False,num_beams=num_beam)
|
| 89 |
+
output = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
|
| 90 |
+
s3.pop(output)
|
| 91 |
+
else:
|
| 92 |
+
|
| 93 |
+
output= "出错啦"
|
| 94 |
+
|
| 95 |
+
print(output)
|
| 96 |
+
if len(sum_log) < 10:
|
| 97 |
+
sum_log.append([inputs,output])
|
| 98 |
+
else:
|
| 99 |
+
sum_log.pop(0)
|
| 100 |
+
sum_log.append([inputs,output])
|
| 101 |
+
return output,sum_log
|
| 102 |
+
|
| 103 |
+
def code_trans(inputs,trans_prompt,top_p, temperature):
|
| 104 |
+
# 这里是可以添加采样的个数的
|
| 105 |
+
# 另外,核采样并不是最优选择,最后改成贪婪采样吧
|
| 106 |
+
input_ids = tokenizer.encode(inputs+trans_prompt, return_tensors='pt')
|
| 107 |
+
generated_ids = tran_model.generate(input_ids, max_length=500,do_sample=True,top_p=top_p,temperature=temperature)
|
| 108 |
+
output = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
|
| 109 |
+
print(output)
|
| 110 |
+
if len(tran_log) < 10:
|
| 111 |
+
tran_log.append([inputs,str(output)])
|
| 112 |
+
else:
|
| 113 |
+
tran_log.pop(0)
|
| 114 |
+
tran_log.append([inputs,str(output)])
|
| 115 |
+
return output,tran_log
|
| 116 |
+
|
| 117 |
+
def code_refine(inputs,refine_prompt,top_p, temperature):
|
| 118 |
+
input_ids = tokenizer.encode(inputs+refine_prompt, return_tensors='pt')
|
| 119 |
+
generated_ids = refine_model.generate(input_ids, max_length=500,do_sample=True,top_p=top_p,temperature=temperature)
|
| 120 |
+
output = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
|
| 121 |
+
print(output)
|
| 122 |
+
if len(refine_log) < 10:
|
| 123 |
+
refine_log.append([inputs,output])
|
| 124 |
+
else:
|
| 125 |
+
refine_log.pop(0)
|
| 126 |
+
refine_log.append([inputs,output])
|
| 127 |
+
return output,refine_log
|
| 128 |
+
|
| 129 |
+
def code_generation(inputs,gene_prompt,top_p, temperature,sec):
|
| 130 |
+
if inputs == "Check if in given list of numbers, are any two numbers closer to each other than given threshold.\ndef has_close_elements(numbers,threshold):\n":
|
| 131 |
+
g = [
|
| 132 |
+
"def has_close_elements(numbers,threshold):\n for idx, elem in enumerate(numbers):\n for idx2, elem2 in enumerate(numbers):\n if idx != idx2:\n distance = abs(elem - elem2)\n if distance < threshold:\n return True\n\n return False\n",
|
| 133 |
+
"def has_close_elements(numbers,threshold):\n return any(abs(a-b)<threshold for a,b in zip(numbers,numbers[1:]))",
|
| 134 |
+
"def has_close_elements(numbers, threshold):\n numbers = sorted(numbers)\n for i in range(len(numbers) - 1):\n if abs(numbers[i] - numbers[i + 1]) < threshold:\n return True \n return False"
|
| 135 |
+
]
|
| 136 |
+
#TODO: 最后调用一下模型来伪造时间
|
| 137 |
+
# input_ids = tokenizer.encode(inputs + gene_prompt, return_tensors='pt')
|
| 138 |
+
# generated_ids = gene_model.generate(
|
| 139 |
+
# input_ids,
|
| 140 |
+
# do_sample=True,
|
| 141 |
+
# temperature=temperature,
|
| 142 |
+
# max_length=500,
|
| 143 |
+
# top_p=top_p)
|
| 144 |
+
output = random.choice(g)
|
| 145 |
+
elif inputs == "Write a function to judge the leap year which takes a year number as input and outputs YES or NO.\ndef is_leap_year(year):\n":
|
| 146 |
+
g = [
|
| 147 |
+
"def is_leap_year(year):\n if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):\n return \"YES\"\n else:\n return \"NO\"",
|
| 148 |
+
"def is_leap_year(year):\n\tif year % 400 == 0:\n\t\treturn \"YES\"\n\telif year % 100 == 0:\n\t\treturn \"NO\"\n\telif year % 4 == 0:\n\t\treturn \"YES\"\n\telse:\n\t\treturn \"NO\""
|
| 149 |
+
]
|
| 150 |
+
#TODO: 最后调用一下模型来伪造时间
|
| 151 |
+
# input_ids = tokenizer.encode(inputs + gene_prompt, return_tensors='pt')
|
| 152 |
+
# generated_ids = gene_model.generate(
|
| 153 |
+
# input_ids,
|
| 154 |
+
# do_sample=True,
|
| 155 |
+
# temperature=temperature,
|
| 156 |
+
# max_length=500,
|
| 157 |
+
# top_p=top_p)
|
| 158 |
+
output = random.choice(g)
|
| 159 |
+
elif inputs == "Write a bubble sort funtion.\ndef bubble_sort(lst):\n":
|
| 160 |
+
g = ["def bubble_sort(lst):\n n = len(lst)\n for i in range(n):\n for j in range(n-i-1):\n if lst[j] > lst[j+1]:\n lst[j], lst[j+1] = lst[j+1], lst[j]\n return lst",
|
| 161 |
+
"def bubble_sort(lst):\n n = len(lst)\n for i in range(n - 1):\n swapped = False\n for j in range(n - i - 1):\n if lst[j] > lst[j + 1]:\n lst[j], lst[j + 1] = lst[j + 1], lst[j]\n swapped = True\n if not swapped:\n break\n return lst"]
|
| 162 |
+
#TODO: 最后调用一下模型来伪造时间
|
| 163 |
+
# input_ids = tokenizer.encode(inputs + gene_prompt, return_tensors='pt')
|
| 164 |
+
# generated_ids = gene_model.generate(
|
| 165 |
+
# input_ids,
|
| 166 |
+
# do_sample=True,
|
| 167 |
+
# temperature=temperature,
|
| 168 |
+
# max_length=500,
|
| 169 |
+
# top_p=top_p)
|
| 170 |
+
output = random.choice(g)
|
| 171 |
+
|
| 172 |
+
elif sec == "Python":
|
| 173 |
+
input_ids = tokenizer.encode(inputs + gene_prompt, return_tensors='pt')
|
| 174 |
+
generated_ids = gene_model.generate(
|
| 175 |
+
input_ids,
|
| 176 |
+
do_sample=True,
|
| 177 |
+
temperature=temperature,
|
| 178 |
+
max_length=500,
|
| 179 |
+
top_p=top_p)
|
| 180 |
+
output = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
|
| 181 |
+
elif sec == "Java":
|
| 182 |
+
input_ids = tokenizer.encode(inputs + gene_prompt, return_tensors='pt'
|
| 183 |
+
generated_ids = gene_model.generate(
|
| 184 |
+
input_ids,
|
| 185 |
+
do_sample=True,
|
| 186 |
+
temperature=temperature,
|
| 187 |
+
max_length=500,
|
| 188 |
+
top_p=top_p)
|
| 189 |
+
output = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
|
| 190 |
+
else:
|
| 191 |
+
output = "出错啦!"
|
| 192 |
+
print(output)
|
| 193 |
+
|
| 194 |
+
if len(gene_log) < 10:
|
| 195 |
+
gene_log.append([inputs,output])
|
| 196 |
+
else:
|
| 197 |
+
gene_log.pop(0)
|
| 198 |
+
gene_log.append([inputs,output])
|
| 199 |
+
print(gene_log)
|
| 200 |
+
return output,gene_log
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
def gene_ex(in_ex):
|
| 204 |
+
# time.sleep(0.9)
|
| 205 |
+
print(in_ex)
|
| 206 |
+
return gene_ex_out[in_ex]
|
| 207 |
+
|
| 208 |
+
|
| 209 |
+
def main():
|
| 210 |
+
# 最后别忘记加 example ��能
|
| 211 |
+
# 纠错可以加一个 diff 功能
|
| 212 |
+
# 将表格的/n找回来,测试删除历史记录,在输入框的默认站位符给出明确的输入要求
|
| 213 |
+
with gr.Blocks(title="CodeLab",theme=gr.themes.Soft(), analytics_enabled=False,css =css_format ) as demo: # theme=set_theme, css=advanced_css
|
| 214 |
+
gr.HTML("<h1 align=\"center\" style=\"color:#5f6368\">CodeLab</h1>")
|
| 215 |
+
with gr.Tab("代码自动摘要",elem_id="mytab"):
|
| 216 |
+
with gr.Row().style():
|
| 217 |
+
with gr.Column(scale=1):
|
| 218 |
+
# 先不做多输出了
|
| 219 |
+
txt_out_1 = gr.Textbox(label = "输出",show_label=True, placeholder="此处展示生成的摘要~",lines = 14,interactive = True).style(container=False) # 输入的文本
|
| 220 |
+
with gr.Row():
|
| 221 |
+
gr.Markdown("### 历史记录(可通过双击点开进行复制)")
|
| 222 |
+
delBtn_1 = gr.Button("删除历史记录", variant="secondary",elem_id="history_btn"); delBtn_1.style(size="sm")
|
| 223 |
+
table_1 = gr.Dataframe(
|
| 224 |
+
overflow_row_behaviour = "show_ends",
|
| 225 |
+
headers = ["输入","输出"],
|
| 226 |
+
datatype= ["str","str"],
|
| 227 |
+
row_count = (10,"fixed"),
|
| 228 |
+
col_count = (2,"fixed"),
|
| 229 |
+
interactive = True,
|
| 230 |
+
elem_id = "history"
|
| 231 |
+
)
|
| 232 |
+
with gr.Column(scale=1):
|
| 233 |
+
with gr.Row():
|
| 234 |
+
txt_in_1 = gr.Code(label = "输入",show_label=True, placeholder="请在此处输入要生成摘要的代码。",language ="python",lines = 18,elem_id = "codebox") # 输入的文本
|
| 235 |
+
with gr.Row():
|
| 236 |
+
submitBtn_1 = gr.Button("提交", variant="primary") # 主要按钮样式
|
| 237 |
+
with gr.Row():
|
| 238 |
+
resetBtn_1 = gr.Button("重置", variant="secondary"); resetBtn_1.style(size="sm")
|
| 239 |
+
sec_1 = gr.Dropdown(["Java", "Python"], type="value",label ="语言")
|
| 240 |
+
prompt_1 = gr.Textbox(show_label=True, placeholder=f"Prompt", label="prompt", value=sum_prompt)
|
| 241 |
+
top_p_1 = gr.Slider(minimum=1, maximum=9, value=4, step=1,interactive=True, label="Beam Num",)
|
| 242 |
+
# temperature_1 = gr.Slider(minimum=-0, maximum=2.0, value=1.0, step=0.01, interactive=True, label="Temperature",)
|
| 243 |
+
|
| 244 |
+
|
| 245 |
+
with gr.Tab("代码自动翻译",elem_id="mytab"):
|
| 246 |
+
with gr.Row().style():
|
| 247 |
+
with gr.Column(scale=1):
|
| 248 |
+
txt_out_2 = gr.Textbox(label = "输出",show_label=True, placeholder="此处输出翻译后的C#代码~",lines = 14,interactive = True).style(container=False) # 输入的文本
|
| 249 |
+
with gr.Row():
|
| 250 |
+
gr.Markdown("### 历史记录(可通过双击点开进行复制)")
|
| 251 |
+
delBtn_2 = gr.Button("删除历史记录", variant="secondary",elem_id="history_btn"); delBtn_2.style(size="sm")
|
| 252 |
+
table_2 = gr.Dataframe(
|
| 253 |
+
overflow_row_behaviour = "show_ends",
|
| 254 |
+
headers = ["输入","输出"],
|
| 255 |
+
datatype= ["str","str"],
|
| 256 |
+
row_count = (10,"fixed"),
|
| 257 |
+
col_count = (2,"fixed"),
|
| 258 |
+
interactive = True,
|
| 259 |
+
elem_id = "history"
|
| 260 |
+
)
|
| 261 |
+
with gr.Column(scale=1):
|
| 262 |
+
with gr.Row():
|
| 263 |
+
txt_in_2 =gr.Textbox(label = "输入",show_label=True, placeholder="此处输入待翻译的java代码~",lines = 14,interactive = True).style(container=False) # 输入的文本
|
| 264 |
+
with gr.Row():
|
| 265 |
+
submitBtn_2 = gr.Button("提交", variant="primary") # 主要按钮样式
|
| 266 |
+
with gr.Row():
|
| 267 |
+
resetBtn_2 = gr.Button("重置", variant="secondary"); resetBtn_2.style(size="sm")
|
| 268 |
+
sec_2 = gr.Dropdown(["Java", "Python"], type="value",label ="语言")
|
| 269 |
+
prompt_2 = gr.Textbox(show_label=True, placeholder=f"Prompt", label="prompt", value=trans_prompt)
|
| 270 |
+
top_p_2 = gr.Slider(minimum=1, maximum=9, value=4, step=1,interactive=True, label="Beam Num")
|
| 271 |
+
# temperature_2 = gr.Slider(minimum=-0, maximum=2.0, value=1.0, step=0.01, interactive=True, label="Temperature")
|
| 272 |
+
|
| 273 |
+
|
| 274 |
+
|
| 275 |
+
|
| 276 |
+
|
| 277 |
+
|
| 278 |
+
with gr.Tab("代码自动生成",elem_id="mytab"):
|
| 279 |
+
with gr.Row().style():
|
| 280 |
+
with gr.Column(scale=1):
|
| 281 |
+
# 先不做多输出了
|
| 282 |
+
txt_out = gr.Code(label = "输出",show_label=True, placeholder="此处展示自动生成的代码~",language ="python",lines = 19,elem_id = "codebox",elem_classes = "codebox",interactive = True) # 输入的文本
|
| 283 |
+
|
| 284 |
+
with gr.Row():
|
| 285 |
+
gr.Markdown("### 历史记录(可通过双击点开进行复制)")
|
| 286 |
+
delBtn = gr.Button("删除历史记录", variant="secondary",elem_id="history_btn"); delBtn.style(size="sm")
|
| 287 |
+
table_0 = gr.Dataframe(
|
| 288 |
+
overflow_row_behaviour = "show_ends",
|
| 289 |
+
headers = ["输入","输出"],
|
| 290 |
+
datatype= ["str","str"],
|
| 291 |
+
row_count = (10,"fixed"),
|
| 292 |
+
col_count = (2,"fixed"),
|
| 293 |
+
interactive = True,
|
| 294 |
+
elem_id = "history"
|
| 295 |
+
)
|
| 296 |
+
with gr.Column(scale=1):
|
| 297 |
+
with gr.Row():
|
| 298 |
+
txt_in = gr.Textbox(label = "输入",show_label=True, placeholder="请在此输入自然语言描述(当前仅支持英文描述)。请注意:\n (1) 描述越清晰生成质量越高;\n(2) 可以增加特定语言的函数头引导模型生成,如def function_name(arg1,arg2)。",lines = 12).style(container=False) # 输入的文本
|
| 299 |
+
with gr.Row():
|
| 300 |
+
submitBtn = gr.Button("提交", variant="primary") # 主要按钮样式
|
| 301 |
+
with gr.Row():
|
| 302 |
+
resetBtn = gr.Button("重置", variant="primary")
|
| 303 |
+
|
| 304 |
+
# gr.Examples(
|
| 305 |
+
# gene_ex_in,
|
| 306 |
+
# txt_in,
|
| 307 |
+
# [txt_out],
|
| 308 |
+
# gene_ex,
|
| 309 |
+
# # run_on_click=True,
|
| 310 |
+
# cache_examples= True,
|
| 311 |
+
# elem_id = "gene_example"
|
| 312 |
+
# )
|
| 313 |
+
sec = gr.Dropdown(["Java", "Python"], type="value",label ="语言")
|
| 314 |
+
prompt = gr.Textbox(show_label=True, placeholder=f"Prompt", label="prompt", value=gene_prompt,elem_id ="func")
|
| 315 |
+
top_p = gr.Slider(minimum=-0, maximum=1.0, value=1.0, step=0.01,interactive=True, label="Top-p (nucleus sampling)",elem_id ="func")
|
| 316 |
+
temperature = gr.Slider(minimum=-0, maximum=2.0, value=1.0, step=0.01, interactive=True, label="Temperature",elem_id ="func")
|
| 317 |
+
|
| 318 |
+
|
| 319 |
+
|
| 320 |
+
|
| 321 |
+
|
| 322 |
+
|
| 323 |
+
|
| 324 |
+
|
| 325 |
+
|
| 326 |
+
|
| 327 |
+
with gr.Tab("代码自动纠错",elem_id="mytab"):
|
| 328 |
+
with gr.Row().style():
|
| 329 |
+
with gr.Column(scale=1):
|
| 330 |
+
txt_out_3 = gr.Textbox(label = "输出",show_label=True, placeholder="此处输出纠错后的java代码~",lines = 14).style(container=False) # 输入的文本
|
| 331 |
+
with gr.Row():
|
| 332 |
+
gr.Markdown("### 历史记录(可通过双击点开进行复制)")
|
| 333 |
+
delBtn_3 = gr.Button("删除历史记录", variant="secondary",elem_id="history_btn"); delBtn_3.style(size="sm")
|
| 334 |
+
table_3 = gr.Dataframe(
|
| 335 |
+
overflow_row_behaviour = "show_ends",
|
| 336 |
+
headers = ["输入","输出"],
|
| 337 |
+
datatype= ["str","str"],
|
| 338 |
+
row_count = (10,"fixed"),
|
| 339 |
+
col_count = (2,"fixed"),
|
| 340 |
+
interactive = True,
|
| 341 |
+
elem_id = "history"
|
| 342 |
+
)
|
| 343 |
+
with gr.Column(scale=1):
|
| 344 |
+
with gr.Row():
|
| 345 |
+
txt_in_3 =gr.Textbox(label = "输入",show_label=True, placeholder="此处输入待纠错的java代码~",lines = 14).style(container=False) # 输入的文本
|
| 346 |
+
with gr.Row():
|
| 347 |
+
submitBtn_3 = gr.Button("提交", variant="primary") # 主要按钮样式
|
| 348 |
+
with gr.Row():
|
| 349 |
+
resetBtn_3 = gr.Button("重置", variant="secondary"); resetBtn_2.style(size="sm")
|
| 350 |
+
sec_3 = gr.Dropdown(["Java", "Python"], type="value",label ="语言")
|
| 351 |
+
prompt_3 = gr.Textbox(show_label=True, placeholder=f"Prompt", label="System prompt", value=refine_prompt)
|
| 352 |
+
top_p_3 = gr.Slider(minimum=-0, maximum=1.0, value=1.0, step=0.01,interactive=True, label="Top-p (nucleus sampling)")
|
| 353 |
+
temperature_3 = gr.Slider(minimum=-0, maximum=2.0, value=1.0, step=0.01, interactive=True, label="Temperature")
|
| 354 |
+
|
| 355 |
+
|
| 356 |
+
|
| 357 |
+
submitBtn.click(code_generation,[txt_in,prompt,top_p,temperature,sec],[txt_out,table_0])
|
| 358 |
+
resetBtn.click(lambda: ("",gene_prompt,1,1, None), None, [txt_in,prompt,top_p,temperature,txt_out]) # 重置按钮,清空输入、输出、参数
|
| 359 |
+
delBtn.click(clear,None,table_0)
|
| 360 |
+
|
| 361 |
+
submitBtn_1.click(code_summary,[txt_in_1,prompt_1,top_p_1,sec_1],[txt_out_1,table_1])
|
| 362 |
+
resetBtn_1.click(lambda: ("",sum_prompt,4,None), None, [txt_in_1,prompt_1,top_p_1,txt_out_1]) # 重置按钮,清空输入、输出、参数
|
| 363 |
+
delBtn_1.click(clear1,None,table_1)
|
| 364 |
+
|
| 365 |
+
submitBtn_2.click(code_trans,[txt_in_2,prompt_2,top_p_2,sec_2],[txt_out_2,table_2])
|
| 366 |
+
resetBtn_2.click(lambda: ("",trans_prompt,4,None), None, [txt_in_2,prompt_2,top_p_2,txt_out_2]) # 重置按钮,清空输入、输出、参数
|
| 367 |
+
delBtn_2.click(clear2,None,table_2)
|
| 368 |
+
|
| 369 |
+
submitBtn_3.click(code_refine,[txt_in_3,prompt_3,top_p_3,sec_3],[txt_out_3,table_3])
|
| 370 |
+
resetBtn_3.click(lambda: ("",refine_prompt,1,None), None, [txt_in_3,prompt_3,top_p_3,txt_out_3]) # 重置按钮,清空输入、输出、参数
|
| 371 |
+
delBtn_3.click(clear3,None,table_3)
|
| 372 |
+
demo.launch(share=True)
|
| 373 |
+
print()
|
| 374 |
+
|
| 375 |
+
if __name__ =="__main__":
|
| 376 |
+
title_html = f"<h1 align=\"center\" style=\"ont-family: Monotype Corsiva;color:#5f6368\">CodeLab</h1>"
|
| 377 |
+
description = """代码开源和更新[地址🚀](https://github.com/binary-husky/chatgpt_academic),感谢热情的[开发者们❤️](https://github.com/binary-husky/chatgpt_academic/graphs/contributors)"""
|
| 378 |
+
|
| 379 |
+
# 问询记录
|
| 380 |
+
import logging
|
| 381 |
+
import os
|
| 382 |
+
import shutil
|
| 383 |
+
os.makedirs("gpt_log", exist_ok=True)
|
| 384 |
+
try:logging.basicConfig(filename="gpt_log/chat_secrets.log", level=logging.INFO, encoding="utf-8")
|
| 385 |
+
except:logging.basicConfig(filename="gpt_log/chat_secrets.log", level=logging.INFO)
|
| 386 |
+
print("所有问询记录将自动保存在本地目录./gpt_log/chat_secrets.log, 请注意自我隐私保护哦!")
|
| 387 |
+
|
| 388 |
+
# 调整主题和样式
|
| 389 |
+
from theme import adjust_theme, advanced_css
|
| 390 |
+
set_theme = adjust_theme()
|
| 391 |
+
if os.path.exists("/home2/nsy/jishe/gradio_cached_examples"): # 如果存在 必须删除 否则越界
|
| 392 |
+
shutil.rmtree("/home2/nsy/jishe/gradio_cached_examples")
|
| 393 |
+
|
| 394 |
+
# 模型加载 注意最后放到GPU运行
|
| 395 |
+
tokenizer = RobertaTokenizer.from_pretrained('/home2/nsy/jishe/mymodel/multi-A-code-summary-codet5-origin')
|
| 396 |
+
sum_model = AutoModelForSeq2SeqLM.from_pretrained('/home2/nsy/jishe/mymodel/java-code-summary')
|
| 397 |
+
sum_model.eval()
|
| 398 |
+
tran_model = AutoModelForSeq2SeqLM.from_pretrained('/home2/nsy/jishe/mymodel/java-code-translation')
|
| 399 |
+
tran_model.eval()
|
| 400 |
+
refine_model = AutoModelForSeq2SeqLM.from_pretrained('/home2/nsy/jishe/mymodel/java-code-refinement')
|
| 401 |
+
refine_model.eval()
|
| 402 |
+
gene_model = T5ForConditionalGeneration.from_pretrained("/home2/nsy/jishe/codet5_finetuned_codeRL")
|
| 403 |
+
gene_model.eval()
|
| 404 |
+
|
| 405 |
+
gene_prompt,sum_prompt,trans_prompt,refine_prompt = "\nANSWER:\n","","",""
|
| 406 |
+
sum_log, tran_log, refine_log, gene_log = [],[],[],[]
|
| 407 |
+
main()
|
| 408 |
+
print()
|