Files changed (1) hide show
  1. app.py +26 -174
app.py CHANGED
@@ -1,166 +1,3 @@
1
- # import gradio as gr
2
- # import json, os
3
- # import urllib.request
4
-
5
- # # Replace 'YOUR_API_KEY' with your actual YouTube Data API key
6
- # API_KEY = os.getenv('api_key')
7
-
8
- # def get_youtube_id(youtube_url):
9
- # if 'youtube.com' in youtube_url:
10
- # video_id = youtube_url.split('v=')[-1]
11
- # elif 'youtu.be' in youtube_url:
12
- # video_id = youtube_url.split('/')[-1].split('?')[0]
13
- # return video_id
14
-
15
- # def check_cc_license(youtube_url):
16
- # # Extract video ID from the URL
17
- # video_id = get_youtube_id(youtube_url)
18
-
19
- # # YouTube Data API URL to get video details
20
- # api_url = f'https://www.googleapis.com/youtube/v3/videos?id={video_id}&part=status&key={API_KEY}'
21
-
22
- # try:
23
- # # Fetch video details
24
- # response = urllib.request.urlopen(api_url)
25
- # data = json.load(response)
26
-
27
- # # Check the license status
28
- # for item in data['items']:
29
- # if item['status']['license'] == 'creativeCommon':
30
- # return f"Yes."
31
- # else:
32
- # return f"No."
33
-
34
- # except Exception as e:
35
- # return f"An error occurred: {str(e)}"
36
-
37
- # # Gradio interface
38
- # interface = gr.Interface(
39
- # fn=check_cc_license,
40
- # inputs=gr.Textbox(label="YouTube Video URL"),
41
- # outputs=gr.Textbox(label="Creative Commons license?")
42
- # )
43
-
44
- # if __name__ == "__main__":
45
- # interface.launch()
46
-
47
-
48
- # import gradio as gr
49
- # import asyncio
50
- # import os
51
- # from openai import AsyncOpenAI
52
-
53
- # # 从您的模块中导入必要的函数
54
- # from utils.generate_distractors import prepare_q_inputs, construct_prompt_textonly, generate_distractors
55
- # from utils.api_utils import generate_from_openai_chat_completion
56
- # # 修改generate_distractors函数,使其成为异步函数
57
- # # 假设generate_distractors函数定义在您的模块中,我们需要修改它
58
- # # 如果无法修改原始模块,请在此处重新定义
59
-
60
- # async def generate_distractors_async(model_name: str,
61
- # queries: list,
62
- # n: int=1,
63
- # max_tokens: int=4096):
64
- # assert model_name in ["gpt-4o-mini", "gpt-4-turbo", "gpt-4o", "gpt-4o-2024-08-06"], "Invalid model name"
65
-
66
- # client = AsyncOpenAI(api_key=os.environ.get("OPENAI_API_KEY"), base_url="https://yanlp.zeabur.app/v1")
67
- # messages = prepare_q_inputs(queries)
68
-
69
- # # 直接等待协程而不是使用asyncio.run()
70
- # responses = await generate_from_openai_chat_completion(
71
- # client,
72
- # messages=messages,
73
- # engine_name=model_name,
74
- # n=n,
75
- # max_tokens=max_tokens,
76
- # requests_per_minute=30,
77
- # json_format=True
78
- # )
79
-
80
- # for query, response in zip(queries, responses):
81
- # new_options = response
82
- # if new_options and "distractors" in new_options:
83
- # query["option_5"] = new_options["distractors"].get("E", "")
84
- # query["option_6"] = new_options["distractors"].get("F", "")
85
- # query["option_7"] = new_options["distractors"].get("G", "")
86
- # query["distractor_analysis"] = new_options["distractors"].get("analysis_of_distractors", "")
87
- # else:
88
- # query["option_5"] = ""
89
- # query["option_6"] = ""
90
- # query["option_7"] = ""
91
- # query["distractor_analysis"] = ""
92
-
93
- # return queries
94
-
95
- # # 定义异步处理函数
96
- # async def generate_distractors_gradio(question, option1, option2, option3, option4, answer, answer_analysis):
97
- # query = {
98
- # 'question': question,
99
- # 'option_1': option1,
100
- # 'option_2': option2,
101
- # 'option_3': option3,
102
- # 'option_4': option4,
103
- # 'answer': answer,
104
- # 'answer_analysis': answer_analysis
105
- # }
106
-
107
- # queries = [query] # 因为函数期望的是一个列表
108
-
109
- # # 调用异步生成干扰项的函数
110
- # results = await generate_distractors_async(
111
- # model_name="gpt-4o-mini",
112
- # queries=queries,
113
- # n=1,
114
- # max_tokens=4096
115
- # )
116
-
117
- # # 提取结果
118
- # result = results[0]
119
- # new_options = {
120
- # 'E': result.get('option_5', ''),
121
- # 'F': result.get('option_6', ''),
122
- # 'G': result.get('option_7', '')
123
- # }
124
- # distractor_analysis = result.get('distractor_analysis', '')
125
-
126
- # # 返回新的干扰项和分析
127
- # return new_options, distractor_analysis
128
-
129
- # # 创建Gradio界面
130
- # with gr.Blocks() as demo:
131
- # gr.Markdown("# 多项选择题干扰项生成器")
132
- # with gr.Row():
133
- # question_input = gr.Textbox(label="问题", lines=2)
134
- # with gr.Row():
135
- # option1_input = gr.Textbox(label="选项A")
136
- # option2_input = gr.Textbox(label="选项B")
137
- # with gr.Row():
138
- # option3_input = gr.Textbox(label="选项C")
139
- # option4_input = gr.Textbox(label="选项D")
140
- # with gr.Row():
141
- # answer_input = gr.Textbox(label="正确答案")
142
- # with gr.Row():
143
- # answer_analysis_input = gr.Textbox(label="答案解析", lines=3)
144
- # with gr.Row():
145
- # generate_button = gr.Button("生成干扰项")
146
- # with gr.Row():
147
- # output_options = gr.JSON(label="生成的干扰选项")
148
- # with gr.Row():
149
- # output_analysis = gr.Textbox(label="干扰项解析", lines=5)
150
-
151
- # # 定义按钮点击事件,注意这里不需要修改,Gradio会自动处理异步函数
152
- # generate_button.click(
153
- # fn=generate_distractors_gradio,
154
- # inputs=[question_input, option1_input, option2_input, option3_input, option4_input, answer_input, answer_analysis_input],
155
- # outputs=[output_options, output_analysis]
156
- # )
157
-
158
- # # 运行Gradio应用
159
- # demo.launch()
160
-
161
-
162
-
163
-
164
  import gradio as gr
165
  import asyncio
166
  import os
@@ -172,7 +9,6 @@ from openai import AsyncOpenAI
172
 
173
  # 请确保在环境变量中设置了您的YouTube Data API密钥
174
  API_KEY = "AIzaSyDyPpkFRUpUuSMQbhxwTFxCBLK5qTHU-ms"
175
- os.environ["OPENAI_API_KEY"] = "sk-h8rybT55JORrT40y2397A06c8f2444F8Ad49407fE85eBf76"
176
 
177
  def get_youtube_id(youtube_url):
178
  if 'youtube.com' in youtube_url:
@@ -218,9 +54,6 @@ def check_cc_license(youtube_url):
218
  # 从您的模块中导入必要的函数
219
  from utils.generate_distractors import prepare_q_inputs, construct_prompt_textonly, generate_distractors
220
  from utils.api_utils import generate_from_openai_chat_completion
221
- # 修改generate_distractors函数,使其成为异步函数
222
- # 假设generate_distractors函数定义在您的模块中,我们需要修改它
223
- # 如果无法修改原始模块,请在此处重新定义
224
 
225
  async def generate_distractors_async(model_name: str,
226
  queries: list,
@@ -259,6 +92,10 @@ async def generate_distractors_async(model_name: str,
259
 
260
  # 定义异步处理函数
261
  async def generate_distractors_gradio(question, option1, option2, option3, option4, answer, answer_analysis):
 
 
 
 
262
  query = {
263
  'question': question,
264
  'option_1': option1,
@@ -273,7 +110,7 @@ async def generate_distractors_gradio(question, option1, option2, option3, optio
273
 
274
  # 调用异步生成干扰项的函数
275
  results = await generate_distractors_async(
276
- model_name="gpt-4o-mini",
277
  queries=queries,
278
  n=1,
279
  max_tokens=4096
@@ -286,11 +123,28 @@ async def generate_distractors_gradio(question, option1, option2, option3, optio
286
  'F': result.get('option_6', ''),
287
  'G': result.get('option_7', '')
288
  }
 
289
  distractor_analysis = result.get('distractor_analysis', '')
290
 
291
  # 返回新的干扰项和分析
292
- return new_options, distractor_analysis
293
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
294
 
295
  with gr.Blocks() as demo:
296
  gr.Markdown("# CC检查器和干扰项生成器")
@@ -313,15 +167,14 @@ with gr.Blocks() as demo:
313
  with gr.Row():
314
  option1_input = gr.Textbox(label="选项A")
315
  option2_input = gr.Textbox(label="选项B")
316
- with gr.Row():
317
  option3_input = gr.Textbox(label="选项C")
318
- option4_input = gr.Textbox(label="选项D(可以输入也可以不输入)")
319
  with gr.Row():
320
  answer_input = gr.Textbox(label="正确答案(A/B/C/D)")
321
  with gr.Row():
322
  answer_analysis_input = gr.Textbox(label="答案解析", lines=3)
323
  generate_button = gr.Button("生成干扰项")
324
- output_options = gr.JSON(label="生成的干扰选项")
325
  output_analysis = gr.Textbox(label="干扰项解析", lines=5)
326
  generate_button.click(
327
  fn=generate_distractors_gradio,
@@ -331,4 +184,3 @@ with gr.Blocks() as demo:
331
 
332
  # 运行Gradio应用
333
  demo.launch()
334
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import asyncio
3
  import os
 
9
 
10
  # 请确保在环境变量中设置了您的YouTube Data API密钥
11
  API_KEY = "AIzaSyDyPpkFRUpUuSMQbhxwTFxCBLK5qTHU-ms"
 
12
 
13
  def get_youtube_id(youtube_url):
14
  if 'youtube.com' in youtube_url:
 
54
  # 从您的模块中导入必要的函数
55
  from utils.generate_distractors import prepare_q_inputs, construct_prompt_textonly, generate_distractors
56
  from utils.api_utils import generate_from_openai_chat_completion
 
 
 
57
 
58
  async def generate_distractors_async(model_name: str,
59
  queries: list,
 
92
 
93
  # 定义异步处理函数
94
  async def generate_distractors_gradio(question, option1, option2, option3, option4, answer, answer_analysis):
95
+ is_valid, message = validate_inputs(question, option1, option2, option3, option4, answer, answer_analysis)
96
+ if not is_valid:
97
+ return {"error": message}, "" # Output error message
98
+
99
  query = {
100
  'question': question,
101
  'option_1': option1,
 
110
 
111
  # 调用异步生成干扰项的函数
112
  results = await generate_distractors_async(
113
+ model_name="gpt-4o",
114
  queries=queries,
115
  n=1,
116
  max_tokens=4096
 
123
  'F': result.get('option_6', ''),
124
  'G': result.get('option_7', '')
125
  }
126
+ new_option_str = f"E: {new_options['E']}\nF:{new_options['F']}\nG:{new_options['G']}"
127
  distractor_analysis = result.get('distractor_analysis', '')
128
 
129
  # 返回新的干扰项和分析
130
+ return new_option_str, distractor_analysis
131
+
132
+ def validate_inputs(question, option1, option2, option3, option4, answer, analysis):
133
+ if not question:
134
+ return False, "问题不能为空"
135
+ if not option1:
136
+ return False, "选项A不能为空"
137
+ if not option2:
138
+ return False, "选项B不能为空"
139
+ if not option3:
140
+ return False, "选项C不能为空"
141
+ if not option4:
142
+ return False, "选项D不能为空"
143
+ if not answer:
144
+ return False, "正确答案不能为空"
145
+ if not analysis:
146
+ return False, "答案解析不能为空"
147
+ return True, ""
148
 
149
  with gr.Blocks() as demo:
150
  gr.Markdown("# CC检查器和干扰项生成器")
 
167
  with gr.Row():
168
  option1_input = gr.Textbox(label="选项A")
169
  option2_input = gr.Textbox(label="选项B")
 
170
  option3_input = gr.Textbox(label="选项C")
171
+ option4_input = gr.Textbox(label="选项D")
172
  with gr.Row():
173
  answer_input = gr.Textbox(label="正确答案(A/B/C/D)")
174
  with gr.Row():
175
  answer_analysis_input = gr.Textbox(label="答案解析", lines=3)
176
  generate_button = gr.Button("生成干扰项")
177
+ output_options = gr.Textbox(label="生成的干扰选项")
178
  output_analysis = gr.Textbox(label="干扰项解析", lines=5)
179
  generate_button.click(
180
  fn=generate_distractors_gradio,
 
184
 
185
  # 运行Gradio应用
186
  demo.launch()