yosuke-i commited on
Commit
d4a9711
1 Parent(s): a096e1c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -2
app.py CHANGED
@@ -53,8 +53,93 @@ def process_csv(csv_file):
53
  os.rename(output_path, new_path)
54
 
55
  return new_path
56
- def testCSV(csv_file):
57
- return csv_file
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  # interface = gr.Interface(
59
  # fn=process_csv,
60
  # inputs=gr.File(label="CSVファイルをアップロード"),
 
53
  os.rename(output_path, new_path)
54
 
55
  return new_path
56
+
57
+
58
+ def create_choice_question(csv_file):
59
+ # CSVファイルを読み込む
60
+ df = pd.read_csv(csv_file.name)
61
+
62
+ # プロンプトを作成
63
+ prompt_df = pd.DataFrame(columns=['ステージ', 'レベル', 'カテゴリ', '問題形式','複製パターン','テーマ','prompt','quantity'])
64
+
65
+ # dfを反復処理
66
+ for index, row in df.iterrows():
67
+ # 'カテゴリ'カラムの文字列をリストに変換
68
+ try:
69
+ themes = ast.literal_eval(row['カテゴリ'])
70
+ except ValueError:
71
+ # ast.literal_evalに失敗した場合(例: 空の文字列、不正な形式など)
72
+ themes = []
73
+ for theme in themes:
74
+ # プロンプトとカテゴリを組み合わせて新しいpromptを作成
75
+ new_prompt = f"以下の形式の英語のリスニング問題を{row["問題数"]}問、作成して。
76
+ 各問題は、[問題1][問題2]という形式で始めて。
77
+ ただし、スクリプトの内容は、{theme}に関するスクリプトにして。"
78
+ # prompt_dfに新しい行を追加
79
+ new_row = {
80
+ 'ステージ': row['ステージ'],
81
+ 'レベル': row['レベル'],
82
+ 'カテゴリ': row['カテゴリ'],
83
+ '問題形式': row['問題形式'],
84
+ '複製パターン': row['複製パターン'],
85
+ 'テーマ': theme,
86
+ 'prompt': new_prompt, # 新しいプロンプト
87
+ 'quantity': row['問題数']
88
+ }
89
+ prompt_df = pd.concat([prompt_df,new_row],ignore_index=True)
90
+
91
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.csv') as tmp:
92
+ prompt_df.to_csv(tmp.name, index=False)
93
+ output_path = tmp.name
94
+ # ファイル名を変更
95
+ new_path = os.path.join(os.path.dirname(output_path), "output.csv")
96
+ os.rename(output_path, new_path)
97
+ return new_path
98
+
99
+
100
+
101
+ # 変数の配列からすべての組み合わせを生成し、新しいpromptを作成
102
+ for index, row in df.iterrows():
103
+ # 変数の値をリストとして取得、数値の場合は文字列に変換
104
+ variables_lists = [
105
+ str(row[f'変数{i}(var{i})']).split(',') if pd.notna(row[f'変数{i}(var{i})']) else ['']
106
+ for i in range(1, 6)
107
+ ]
108
+
109
+ # 数値を含む可能性のある列の値を文字列に変換
110
+ variables_lists = [[str(value).strip() for value in values] for values in variables_lists]
111
+
112
+ # 変数の配列からすべての組み合わせを生成
113
+ variables_combinations = list(itertools.product(*variables_lists))
114
+ for combination in variables_combinations:
115
+ # プロンプトテンプレートに変数の値を埋め込む
116
+ formatted_prompt = row['プロンプト'].format(var1=combination[0], var2=combination[1], var3=combination[2], var4=combination[3], var5=combination[4])
117
+ new_prompt = "以下の要件で問題を作ってください。" + formatted_prompt
118
+ # 新しい行をDataFrameに追加
119
+ new_row = pd.DataFrame([{
120
+ 'ID': row['ID'],
121
+ 'prompt': new_prompt,
122
+ 'quantity': row['作成問題数'],
123
+ 'output': ''
124
+ }], columns=['ID', 'prompt', 'quantity', 'output'])
125
+ new_df = pd.concat([new_df, new_row], ignore_index=True)
126
+
127
+ # 新しいDataFrameのprompt列をChatGPTに送信し、応答をoutput列に保存
128
+ new_df['output'] = new_df['prompt'].apply(get_chatgpt_response)
129
+
130
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.csv') as tmp:
131
+ new_df.to_csv(tmp.name, index=False)
132
+ output_path = tmp.name
133
+
134
+ # ファイル名を変更
135
+ new_path = os.path.join(os.path.dirname(output_path), "output.csv")
136
+ os.rename(output_path, new_path)
137
+
138
+ return new_path
139
+
140
+
141
+
142
+
143
  # interface = gr.Interface(
144
  # fn=process_csv,
145
  # inputs=gr.File(label="CSVファイルをアップロード"),