| import gradio as gr |
| import pandas as pd |
| import itertools |
| import tempfile |
| import os |
| import ast |
| import requests |
| import re |
| from chatgpt_api import get_chatgpt_response |
| from select_question import create_choice_question |
|
|
| def kousei(csv_file,input_text): |
|
|
| prompt_text =input_text + "指摘は、全ての問題において問題がない場合も含めて、必ず全問題、[問題ID]に続けて結果を書くフォーマットで返してください。[問題ID]の後は改行しないで。(必ず[問題ID]が5つ表示されるはずです)指摘方法は、問題ない場合は「問題なし」、指摘がある場合は「問題あり」\n「問題あり」の場合、問題のある箇所を具体的に指摘してください。\n#リスト" |
| |
| df = pd.read_csv(csv_file.name) |
| |
| df['group_id'] = df.index // 5 |
| grouped = df.groupby('group_id') |
|
|
| |
| def create_prompt(group, base_prompt): |
| prompt = base_prompt |
| for _, row in group.iterrows(): |
| prompt += f"\n[{row['id']}]\n{row['原稿']}" |
| |
| return prompt |
| |
| |
| prompts = grouped.apply(lambda g: create_prompt(g, prompt_text)) |
| |
| |
| |
| prompts = prompts.reset_index(name='prompt_after') |
| |
| |
| prompts['response'] = prompts['prompt_after'].apply(get_chatgpt_response) |
| |
| |
| |
| def split_responses(grouped_df): |
| rows = [] |
| |
| for _, row in grouped_df.iterrows(): |
| response = row['response'] |
| |
| split_response = re.split(r'\[([A-Z0-9]+)\]\s*', response) |
| ids_texts = list(zip(split_response[1::2], split_response[2::2])) |
| |
| for id_text in ids_texts: |
| problem_id = id_text[0] |
| correction_result = id_text[1].strip() |
| original_content = df.loc[df['id'] == problem_id, '原稿'].iloc[0] |
| |
| |
| rows.append({ |
| 'id': problem_id, |
| 'contents': original_content, |
| '校正結果': correction_result |
| }) |
| |
| |
| result_df = pd.DataFrame(rows) |
| return result_df |
| |
| |
| final_results = split_responses(prompts) |
| |
| with tempfile.NamedTemporaryFile(delete=False, suffix='.csv') as tmp: |
| |
| final_results.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 |
|
|
|
|
|
|
| title = "英語生成ツール" |
|
|
| with gr.Blocks(theme=gr.themes.Soft()) as demo: |
| gr.Markdown( |
| f""" |
| # {title} |
| """ |
| ) |
| with gr.Tab("問題生成(選択肢)"): |
| with gr.Column(): |
| gr.Markdown(""" |
| ## 利用手順 |
| 1. こちらの[マスタ](https://drive.google.com/uc?export=download&id=1VyDBtVrnDUlddmITiXg7ybqyB0CTCPeu)を手元に置く |
| 2. シート「input」に、生成したい問題パターンを書いてください(赤字の要素は固定。選択肢は可変。適宜行追加OK) |
| 3. 完成したら、「ファイル>名前を付けて保存」から「CSV UTF-8(コンマ区切り)(*.csv)」形式で保存 |
| 4. 3のCSVを本サイトにアップロード |
| """) |
|
|
| with gr.Row(): |
| inputs=gr.File(label="CSVファイルをアップロード") |
| outputs=gr.File(label="ダウンロード", file_count="singular") |
| gr.Button("問題生成").click( |
| create_choice_question, |
| inputs=[inputs], |
| outputs=[outputs] |
| ) |
| with gr.Tab("問題生成(並び替え)"): |
| with gr.Column(): |
| with gr.Row(): |
| inputs=gr.File(label="CSVファイルをアップロード") |
| outputs=gr.File(label="ダウンロード", file_count="singular") |
| gr.Button("問題生成").click( |
| create_choice_question, |
| inputs=[inputs], |
| outputs=[outputs] |
| ) |
| with gr.Tab("校正"): |
| with gr.Column(): |
| input_text_kousei = gr.Textbox(label="校正観点を入力してください。",value="英単語習得を目的として、以下2種類の問題を用意しています。\n1.英語の正しい日本語訳を選択する4択問題\n2.日本語の正しい英語訳を選択する4択問題\n「#リスト」の誤答選択肢の中に、正解選択肢の別解になってしまっているもの、または別解とは言えないが紛らわしすぎるものがないか、探して指摘してください。") |
| with gr.Row(): |
| inputs=gr.File(label="CSVファイルをアップロード") |
| outputs=gr.File(label="ダウンロード", file_count="singular") |
| gr.Button("問題生成").click( |
| kousei, |
| inputs=[inputs,input_text_kousei], |
| outputs=[outputs] |
| ) |
| demo.launch(share=True) |
|
|
|
|