Spaces:
Paused
Paused
qiulongquan commited on
Commit ·
1e88633
1
Parent(s): 2a1c702
Add application file
Browse files- 1.png +0 -0
- cobol_analysis_with_azure.py +221 -0
- config.json +7 -0
1.png
ADDED
|
cobol_analysis_with_azure.py
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import openai
|
| 3 |
+
import json
|
| 4 |
+
import tiktoken
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import time
|
| 7 |
+
|
| 8 |
+
"""
|
| 9 |
+
使用azure openai作为GPT模型
|
| 10 |
+
进行cobol代码分析
|
| 11 |
+
UI采用gradio框架
|
| 12 |
+
UI使用chatbot进行交互
|
| 13 |
+
已经实现chatbot的交互问答以及历史记录显示和历史内容保存
|
| 14 |
+
chatbot上面不显示prompt内容
|
| 15 |
+
实现稳定输出和创造性输出的切换
|
| 16 |
+
|
| 17 |
+
TODO:
|
| 18 |
+
1.还需要一个 stop 生成
|
| 19 |
+
2.流式stream输出
|
| 20 |
+
3.few-shot learning sample
|
| 21 |
+
"""
|
| 22 |
+
|
| 23 |
+
# 通过max_response_tokens控制回复的长度
|
| 24 |
+
max_response_tokens = 8000
|
| 25 |
+
history_show = []
|
| 26 |
+
temperature=0.5
|
| 27 |
+
top_p=0.95
|
| 28 |
+
|
| 29 |
+
# Load config values
|
| 30 |
+
with open('config.json') as config_file:
|
| 31 |
+
config_details = json.load(config_file)
|
| 32 |
+
|
| 33 |
+
# Setting up the deployment name 这个地方不是模型名字,是Azure OpenAI的部署名字
|
| 34 |
+
chatgpt_model_name = config_details['CHATGPT_MODEL']
|
| 35 |
+
openai.api_type = "azure"
|
| 36 |
+
# The API key for your Azure OpenAI resource.
|
| 37 |
+
openai.api_key = config_details['OPENAI_API_KEY']
|
| 38 |
+
# The base URL for your Azure OpenAI resource. e.g. "https://<your resource name>.openai.azure.com"
|
| 39 |
+
openai.api_base = config_details['OPENAI_API_BASE']
|
| 40 |
+
# Currently Chat Completions API have the following versions available: 2023-03-15-preview
|
| 41 |
+
openai.api_version = config_details['OPENAI_API_VERSION']
|
| 42 |
+
|
| 43 |
+
def radio_change(choice):
|
| 44 |
+
global temperature,top_p
|
| 45 |
+
if choice=="安定出力":
|
| 46 |
+
temperature=0.5
|
| 47 |
+
top_p=0.95
|
| 48 |
+
elif choice=="積極出力":
|
| 49 |
+
temperature=0.7
|
| 50 |
+
top_p=0.95
|
| 51 |
+
|
| 52 |
+
# Defining a function to send the prompt to the ChatGPT model
|
| 53 |
+
# More info : https://learn.microsoft.com/en-us/azure/cognitive-services/openai/how-to/chatgpt?pivots=programming-language-chat-completions
|
| 54 |
+
def cobol_analysis_process(history, messages, model_name, max_response_tokens=500):
|
| 55 |
+
print("temperature=",temperature,"top_p=",top_p)
|
| 56 |
+
response = openai.ChatCompletion.create(
|
| 57 |
+
engine=model_name,
|
| 58 |
+
messages=messages,
|
| 59 |
+
temperature=temperature,
|
| 60 |
+
top_p=top_p,
|
| 61 |
+
# temperature=0.7,
|
| 62 |
+
# top_p=0.95,
|
| 63 |
+
max_tokens=max_response_tokens,
|
| 64 |
+
frequency_penalty=0,
|
| 65 |
+
presence_penalty=0,
|
| 66 |
+
# stop="非非",
|
| 67 |
+
stream=True,
|
| 68 |
+
)
|
| 69 |
+
# print("response",response)
|
| 70 |
+
print("===========history",history)
|
| 71 |
+
history[-1][1] = ""
|
| 72 |
+
history_show[-1][1] = ""
|
| 73 |
+
for response_ in response:
|
| 74 |
+
for choice in response_.choices:
|
| 75 |
+
history[-1][1] += choice.delta.content if "content" in choice.delta else ""
|
| 76 |
+
history_show[-1][1] += choice.delta.content if "content" in choice.delta else ""
|
| 77 |
+
|
| 78 |
+
# Defining a function to print out the conversation in a readable format
|
| 79 |
+
# def print_conversation(messages):
|
| 80 |
+
# for message in messages:
|
| 81 |
+
# print(f"[{message['role'].upper()}]")
|
| 82 |
+
# print(message['content'])
|
| 83 |
+
# print()
|
| 84 |
+
|
| 85 |
+
def preprocess(history):
|
| 86 |
+
# print("history",history)
|
| 87 |
+
base_system_message = "あなたは優秀なCOBOLコード分析者です。あなたの仕事は要件に基づいてCOBOLコードを分析し、結果を出力することです。結果は日本語で出力する必要があります。"
|
| 88 |
+
messages=[{"role": "system", "content": base_system_message}]
|
| 89 |
+
for content in history:
|
| 90 |
+
messages.append({"role": "user", "content": content[0]})
|
| 91 |
+
if content[1] is not None:
|
| 92 |
+
messages.append({"role": "assistant", "content": content[1]})
|
| 93 |
+
print("messages",messages)
|
| 94 |
+
# response = cobol_analysis_process(messages, chatgpt_model_name, max_response_tokens)
|
| 95 |
+
# history[-1] = (history[-1][0], response)
|
| 96 |
+
cobol_analysis_process(history,messages, chatgpt_model_name, max_response_tokens)
|
| 97 |
+
# print_conversation(messages)
|
| 98 |
+
|
| 99 |
+
# 点击【提出】按钮后调用greet函数进行处理
|
| 100 |
+
def greet(history,user_input,analysis_options):
|
| 101 |
+
# print("==========analysis_options=============",analysis_options)
|
| 102 |
+
analysis_content=""
|
| 103 |
+
if analysis_options=="全体概要 入出力 COPY句 サブルーチン解析":
|
| 104 |
+
analysis_content="""
|
| 105 |
+
1.概述一下这个程序主要做了什么,全体程序的数据流程以及每个模块的主要内容。全体概要进行说明并使用table表格输出内容。\n
|
| 106 |
+
2.程序中所有的入力参数和出力参数,要求使用table表格分别表示,要求每一个对象要有简要的介绍。要再次确认不能有遗漏项目。\n
|
| 107 |
+
3.程序中所有的COPY句(COPY文),总结成list表格显示。要求每一个对象要有简要的介绍。要再次确认不能有遗漏项目,所有的COPY句都要总结并在list中输出。\n
|
| 108 |
+
4.全体程序中使用的子程序,包括CALL呼叫的子程序,调用外部文件的子程序。这些子程序总结成list表格显示。要求每一个对象要有简要的介绍。要再次确认不能有遗漏项目。
|
| 109 |
+
"""
|
| 110 |
+
elif analysis_options=="データ定義分析":
|
| 111 |
+
analysis_content="""
|
| 112 |
+
1.要求分析每一行COBOL代码,不能遗漏任何数据定义行,分析内容使用table表格输出
|
| 113 |
+
2.数据定义内容输出格式[等级][项目名][数据类型][长度][初期値]
|
| 114 |
+
3.PIC Xデータ型は文字型,PIC 9データ型は数値型
|
| 115 |
+
"""
|
| 116 |
+
elif analysis_options=="IF ELSE END解析":
|
| 117 |
+
analysis_content="""
|
| 118 |
+
要求:根据下面的要求以及分析例子分析上面的COBOL代码并使用table表格输出结果
|
| 119 |
+
1. 分析每一行cobol代码
|
| 120 |
+
2. 分析WHILE语句中条件内容
|
| 121 |
+
3. 全部IF ... OR ... ELSE ... END条件语句中条件,变量名,变量数值或者字段内容变化,MOVEコマンド内容,DISPLAY显示的内容,VCALL调用的子程序内容,PERFORM调用内容,RETURN返回内容。这些内容要使用table表格简要表示(tabel列内容包括 [行番号],[コマンド/条件],[層級],[変数名],[変数の変化],[MOVEコマンド内容],[DISPLAY内容],[CALL内容],[PERFORM内容],[RETURN内容])
|
| 122 |
+
4. [コマンド/条件]列需要把条件语句的全部内容都写入,条件语句结束标志END和ELSE需要单独一行加入[コマンド/条件]列,嵌套多层IF条件语句中的每一个ELSE,END都不能省略。
|
| 123 |
+
5. 程序中注释的语句不需要分析,不需要输出结果
|
| 124 |
+
6. 如果有嵌套IF ... ELSE ... IF ... ELSE ... END ... END 需要table中明确表示层级关系
|
| 125 |
+
7. 如果是同级别IF ... ELSE ... END table中层级关系数字相同
|
| 126 |
+
8. 如果有嵌套 WHILE 需要table中明确表示层级关系
|
| 127 |
+
9. CASE OF END语句不要表示[層級]数值
|
| 128 |
+
10. 如果是同级别WHILE, table中层级关系数字相同
|
| 129 |
+
11. RETURN: S 表示程序终了,在[RETURN内容]列输出[プログラム終了]
|
| 130 |
+
12. DISPLAY语句需要把全部内容显示在[DISPLAY内容]列,不能遗漏内容
|
| 131 |
+
例:DISPLAY "FMクブン エラー4 HINCODE = " L-HINCODE
|
| 132 |
+
输出 '"FMクブン エラー4 HINCODE = " L-HINCODE'
|
| 133 |
+
13. [変数の変化]列需要明确表示变数的变化状况。
|
| 134 |
+
例:IF: NB-CNT > 0
|
| 135 |
+
输出 NB-CNTが0より大きい場合
|
| 136 |
+
例:IF: L-FM = "1"
|
| 137 |
+
输出 L-FMが1となる場合
|
| 138 |
+
"""
|
| 139 |
+
elif analysis_options=="TABLE COND ACT END解析":
|
| 140 |
+
analysis_content="""
|
| 141 |
+
要求分析每一行cobol代码,结果使用table表格显示
|
| 142 |
+
如果有嵌套TABLE COND ACT END需要table中明确表示层级关系
|
| 143 |
+
同一个TABLE COND ACT END中所有的层级都相同
|
| 144 |
+
全部TABLE COND ACT END语句中条件,变量名,判断条件,判断结果。这些内容要使用table表格简要表示(tabel列内容包括 [行番号],[条件],[層級],[変数名],[判断条件],[判断结果])
|
| 145 |
+
例:
|
| 146 |
+
005070 TABLE: MSKSJ010
|
| 147 |
+
005080 COND: MSKSJ010
|
| 148 |
+
005090 NHINW-KBN2 (9) = "1" :Y,Y,N,N,N: MSKSJ010
|
| 149 |
+
005130 ACT: MSKSJ010
|
| 150 |
+
005230 NSKD1-KBN12 := "3" :-,-,-,-,X: MSKSJ010
|
| 151 |
+
005240 END: MSKSJ010
|
| 152 |
+
|
| 153 |
+
[行番号] 005090
|
| 154 |
+
[条件] NHINW-KBN2 (9) = "1"
|
| 155 |
+
[層級] 1
|
| 156 |
+
[変数名] NHINW-KBN2 (9)
|
| 157 |
+
[判断条件/変数値変化] "1" かどうかのチェック
|
| 158 |
+
[判断結果] :Y,Y,N,N,N:
|
| 159 |
+
"""
|
| 160 |
+
elif analysis_options=="コード解析":
|
| 161 |
+
analysis_content="""分析上面每一行cobol代码,不能有遗漏的代码行,使用table输出结果。table表格的列名[行番号 COBOLコード コード解析結果]
|
| 162 |
+
sample 1:
|
| 163 |
+
clang0 DS_START_PROC SECTION.
|
| 164 |
+
行番号:clang0
|
| 165 |
+
COBOLコード:DS_START_PROC SECTION
|
| 166 |
+
コード解析結果:DS_START_PROCというセクションの開始を宣言しています。
|
| 167 |
+
sample 2:
|
| 168 |
+
001120 UNTIL: X = MTOSM2W-KOSU
|
| 169 |
+
行番号:001120
|
| 170 |
+
COBOLコード:UNTIL: X = MTOSM2W-KOSU
|
| 171 |
+
コード解析結果:この行は、XがMTOSM2W-KOSUと等しいまでのループを示しています。
|
| 172 |
+
"""
|
| 173 |
+
elif analysis_options=="カスタマイズprompt":
|
| 174 |
+
analysis_content=""
|
| 175 |
+
history_show.append([analysis_options+"\n\n"+user_input, None])
|
| 176 |
+
if user_input != "":
|
| 177 |
+
user_input = user_input+"\n\n"+analysis_content
|
| 178 |
+
else:
|
| 179 |
+
user_input = ""
|
| 180 |
+
print("user_input==========",user_input)
|
| 181 |
+
history.append([user_input, None])
|
| 182 |
+
# print("history", history)
|
| 183 |
+
preprocess(history)
|
| 184 |
+
return history_show, gr.Textbox(value="", interactive=False)
|
| 185 |
+
|
| 186 |
+
def bot(history_show):
|
| 187 |
+
yield history_show
|
| 188 |
+
|
| 189 |
+
def print_like_dislike(x: gr.LikeData):
|
| 190 |
+
print(x.index, x.value, x.liked)
|
| 191 |
+
|
| 192 |
+
# 页面内容输出控制
|
| 193 |
+
with gr.Blocks() as demo:
|
| 194 |
+
gr.Markdown("""
|
| 195 |
+
<h1 style="text-align: center;">COBOL解析</h1>
|
| 196 |
+
""") # 设置标题 可以使用markdown语法
|
| 197 |
+
chatbot = gr.Chatbot(
|
| 198 |
+
[],
|
| 199 |
+
elem_id="chatbot",
|
| 200 |
+
bubble_full_width=False,
|
| 201 |
+
show_copy_button=True,
|
| 202 |
+
avatar_images=(None, (os.path.join(os.path.dirname(__file__), "1.png"))),
|
| 203 |
+
)
|
| 204 |
+
analysis_options = gr.Dropdown(['全体概要 入出力 COPY句 サブルーチン解析', 'データ定義分析', 'IF ELSE END解析', 'TABLE COND ACT END解析', 'コード解析', 'カスタマイズprompt'], label="解析タイプ選択")
|
| 205 |
+
radio=gr.Radio(["安定出力", "積極出力"], label="ランダム性制御", info="「安定出力」を採用するとモデルはより多くの決定論的な応答を生成します。「積極出力」を採用するとより多くの創造的な応答が生じます。")
|
| 206 |
+
user_input = gr.Textbox(scale=4,show_label=False,placeholder="user input", container=False,lines=1) # 设置输入框
|
| 207 |
+
# 使用gr.ClearButton来清空chatbot记录的内容
|
| 208 |
+
clear1 = gr.ClearButton([user_input],value="入力コンテンツクリア")
|
| 209 |
+
clear2 = gr.ClearButton([user_input, chatbot],value="Chatコンテンツクリア")
|
| 210 |
+
radio.change(fn=radio_change, inputs=radio)
|
| 211 |
+
txt_msg = user_input.submit(greet, [chatbot,user_input,analysis_options],[chatbot,user_input], queue=False).then(
|
| 212 |
+
bot, chatbot, chatbot, api_name="bot_response"
|
| 213 |
+
)
|
| 214 |
+
txt_msg.then(lambda: gr.Textbox(interactive=True), None, [user_input], queue=False)
|
| 215 |
+
|
| 216 |
+
chatbot.like(print_like_dislike, None, None)
|
| 217 |
+
|
| 218 |
+
|
| 219 |
+
demo.queue()
|
| 220 |
+
if __name__ == "__main__":
|
| 221 |
+
demo.launch(share=True)
|
config.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"CHATGPT_MODEL":"azure-canada-qiu-20240119",
|
| 3 |
+
"OPENAI_API_BASE":"https://azure-qiu-canada-east-20240119.openai.azure.com",
|
| 4 |
+
"OPENAI_API_VERSION":"2023-07-01-preview",
|
| 5 |
+
"OPENAI_API_KEY":"a00e229fc3414ccc8df341baccdbf1ab",
|
| 6 |
+
"OPENAI_EMBEDDINGS_MODEL":"azure-embedding-20240124"
|
| 7 |
+
}
|