Download select_question.py from Risdom/english2: direct link, hf CLI and curl.
- Browser
- Download file 4.38 kB
-
https://huggingface.co/spaces/Risdom/english2/resolve/main/select_question.py
- Command line
-
hf download hf://spaces/Risdom/english2/select_question.py
-
curl -L -o select_question.py https://huggingface.co/spaces/Risdom/english2/resolve/main/select_question.py
4.38 kB
| from chatgpt_api import get_chatgpt_response | |
| import pandas as pd | |
| import re | |
| import tempfile | |
| import os | |
| from translate import translate_text | |
| # プロンプトを作成 | |
| def make_prompt(df): | |
| rows = [] | |
| for index, row in df.iterrows(): | |
| theme_str = str(row["テーマ"]) | |
| if theme_str.lower() == 'nan': | |
| continue | |
| themes_list = theme_str.split(',') | |
| questiontext = "" | |
| if "Question" in row["サンプル"]: | |
| questiontext = "・Questionがある場合は、「##リスニングスクリプト」の表現を真似しすぎず、what,why,how,when,whereなど、多様な角度から問う\n" | |
| else: | |
| questiontext = "" | |
| for theme in themes_list: | |
| new_prompt = "#条件\n"+ "・以下の「#サンプル」の形式([問題1]##リスニングスクリプト ... ##選択肢...[問題2]##リスニングスクリプト...##選択肢...)で、"+row['レベル']+"レベルのリスニング問題を生成\n"+"・[問題1]から[問題"+str(int(row["問題数"]))+"]まで必ず##リスニングスクリプトを"+str(int(row["問題数"]))+"種類作成\n"+ "・「##リスニングスクリプト」から「##選択肢」まですべて出力\n"+ "・「##リスニングスクリプト」は"+row['スクリプトパターン']+"\n"+"・問題の難易度、word数は「##リスニングスクリプト」と同等レベル\n"+ questiontext +"・各問題は、[問題1][問題2]という形式で始める\n"+"・ただし、スクリプトの内容は、"+theme+"に関するスクリプト\n"+"・[問題1]から[問題"+str(int(row["問題数"]))+"]までの誤答選択肢はすべて異なる内容・異なるパターン \n \n"+"#サンプル\n"+row["サンプル"] | |
| print(new_prompt) | |
| rows.append({ | |
| "形式":row["形式2"], | |
| "レベル":row["レベル"], | |
| "テーマ":theme, | |
| "選択肢読み上げ有無":row["選択肢読み上げ有無"], | |
| "複製パターン":row["複製パターン"], | |
| "プロンプト":new_prompt | |
| }) | |
| result_df = pd.DataFrame(rows) | |
| return result_df | |
| # 問題の分割と展開 | |
| def expand_problems(df): | |
| temp_data = [] | |
| # テキスト全体を処理 | |
| for index, row in df.iterrows(): | |
| print("返却値",row['問題1']) | |
| # 正規表現を用いて各問題を分割 | |
| problems = re.findall(r'\[(問題\d+)\](.*?)(?=\[問題\d+\]|$)', row['問題1'], re.DOTALL) | |
| # 各問題のIDとともに新たな行を追加 | |
| for num, text in problems: | |
| temp_data.append({ | |
| "形式": row["形式"], | |
| "レベル": row["レベル"], | |
| "テーマ":row["テーマ"], | |
| "選択肢読み上げ有無": row["選択肢読み上げ有無"], | |
| "複製パターン": row["複製パターン"], | |
| "問題1": text.strip() | |
| }) | |
| # 一時リストから新しいDataFrameを作成 | |
| return pd.DataFrame(temp_data) | |
| def create_choice_question(csv_file): | |
| # CSVファイルを読み込む | |
| df = pd.read_csv(csv_file.name) | |
| # プロンプトを作成する | |
| df_prompt = make_prompt(df) | |
| # 問題を生成する | |
| ## 問題1を生成する | |
| df_prompt["問題1"] = df_prompt["プロンプト"].apply(get_chatgpt_response) | |
| ## 問題1を展開する | |
| result_df = expand_problems(df_prompt) | |
| ## 問題2を生成する | |
| result_df["プロンプト2"] = result_df["複製パターン"]+"\n"+result_df["問題1"] | |
| result_df["問題2"] = result_df["プロンプト2"].apply(get_chatgpt_response) | |
| # 翻訳する | |
| result_df["翻訳_問題1"] = result_df["問題1"].apply(translate_text) | |
| result_df["翻訳_問題2"] = result_df["問題2"].apply(translate_text) | |
| # csvを書き出す | |
| with tempfile.NamedTemporaryFile(delete=False, suffix='.csv') as tmp: | |
| # cp932で保存、エラーは無視(置換しても良い) | |
| result_df.to_csv(tmp.name, index=False, encoding='cp932', errors='ignore') | |
| output_path = tmp.name | |
| # ファイル名を変更 | |
| new_path = os.path.join(os.path.dirname(output_path), "output.csv") | |
| os.rename(output_path, new_path) | |
| return new_path |