File size: 11,966 Bytes
e9ce3e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abd44d6
e9ce3e8
 
 
abd44d6
e9ce3e8
 
 
abd44d6
e9ce3e8
 
abd44d6
c1d41a3
 
 
e9ce3e8
c1d41a3
 
e9ce3e8
 
c1d41a3
 
abd44d6
e9ce3e8
c1d41a3
e9ce3e8
 
abd44d6
e9ce3e8
abd44d6
 
 
e9ce3e8
abd44d6
 
 
e9ce3e8
 
 
abd44d6
e9ce3e8
abd44d6
e9ce3e8
 
 
abd44d6
 
 
 
e9ce3e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abd44d6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# import gradio as gr
# import json, os
# import urllib.request

# # Replace 'YOUR_API_KEY' with your actual YouTube Data API key
# API_KEY = os.getenv('api_key')

# def get_youtube_id(youtube_url):
#     if 'youtube.com' in youtube_url:
#         video_id = youtube_url.split('v=')[-1]
#     elif 'youtu.be' in youtube_url:
#         video_id = youtube_url.split('/')[-1].split('?')[0]
#     return video_id

# def check_cc_license(youtube_url):
#     # Extract video ID from the URL
#     video_id = get_youtube_id(youtube_url)
    
#     # YouTube Data API URL to get video details
#     api_url = f'https://www.googleapis.com/youtube/v3/videos?id={video_id}&part=status&key={API_KEY}'
    
#     try:
#         # Fetch video details
#         response = urllib.request.urlopen(api_url)
#         data = json.load(response)
        
#         # Check the license status
#         for item in data['items']:
#             if item['status']['license'] == 'creativeCommon':
#                 return f"Yes."
#             else:
#                 return f"No."
                
#     except Exception as e:
#         return f"An error occurred: {str(e)}"

# # Gradio interface
# interface = gr.Interface(
#     fn=check_cc_license,
#     inputs=gr.Textbox(label="YouTube Video URL"),
#     outputs=gr.Textbox(label="Creative Commons license?")
# )

# if __name__ == "__main__":
#     interface.launch()


# import gradio as gr
# import asyncio
# import os
# from openai import AsyncOpenAI

# # 从您的模块中导入必要的函数
# from utils.generate_distractors import prepare_q_inputs, construct_prompt_textonly, generate_distractors
# from utils.api_utils import generate_from_openai_chat_completion
# # 修改generate_distractors函数,使其成为异步函数
# # 假设generate_distractors函数定义在您的模块中,我们需要修改它
# # 如果无法修改原始模块,请在此处重新定义

# async def generate_distractors_async(model_name: str, 
#                                      queries: list,
#                                      n: int=1,
#                                      max_tokens: int=4096):
#     assert model_name in ["gpt-4o-mini", "gpt-4-turbo", "gpt-4o", "gpt-4o-2024-08-06"], "Invalid model name"

#     client = AsyncOpenAI(api_key=os.environ.get("OPENAI_API_KEY"), base_url="https://yanlp.zeabur.app/v1")
#     messages = prepare_q_inputs(queries)

#     # 直接等待协程而不是使用asyncio.run()
#     responses = await generate_from_openai_chat_completion(
#         client,
#         messages=messages, 
#         engine_name=model_name,
#         n=n,
#         max_tokens=max_tokens,
#         requests_per_minute=30,
#         json_format=True
#     )

#     for query, response in zip(queries, responses):
#         new_options = response
#         if new_options and "distractors" in new_options:
#             query["option_5"] = new_options["distractors"].get("E", "")
#             query["option_6"] = new_options["distractors"].get("F", "")
#             query["option_7"] = new_options["distractors"].get("G", "")
#             query["distractor_analysis"] = new_options["distractors"].get("analysis_of_distractors", "")
#         else:
#             query["option_5"] = ""
#             query["option_6"] = ""
#             query["option_7"] = ""
#             query["distractor_analysis"] = ""

#     return queries

# # 定义异步处理函数
# async def generate_distractors_gradio(question, option1, option2, option3, option4, answer, answer_analysis):
#     query = {
#         'question': question,
#         'option_1': option1,
#         'option_2': option2,
#         'option_3': option3,
#         'option_4': option4,
#         'answer': answer,
#         'answer_analysis': answer_analysis
#     }

#     queries = [query]  # 因为函数期望的是一个列表

#     # 调用异步生成干扰项的函数
#     results = await generate_distractors_async(
#         model_name="gpt-4o-mini",
#         queries=queries,
#         n=1,
#         max_tokens=4096
#     )

#     # 提取结果
#     result = results[0]
#     new_options = {
#         'E': result.get('option_5', ''),
#         'F': result.get('option_6', ''),
#         'G': result.get('option_7', '')
#     }
#     distractor_analysis = result.get('distractor_analysis', '')

#     # 返回新的干扰项和分析
#     return new_options, distractor_analysis

# # 创建Gradio界面
# with gr.Blocks() as demo:
#     gr.Markdown("# 多项选择题干扰项生成器")
#     with gr.Row():
#         question_input = gr.Textbox(label="问题", lines=2)
#     with gr.Row():
#         option1_input = gr.Textbox(label="选项A")
#         option2_input = gr.Textbox(label="选项B")
#     with gr.Row():
#         option3_input = gr.Textbox(label="选项C")
#         option4_input = gr.Textbox(label="选项D")
#     with gr.Row():
#         answer_input = gr.Textbox(label="正确答案")
#     with gr.Row():
#         answer_analysis_input = gr.Textbox(label="答案解析", lines=3)
#     with gr.Row():
#         generate_button = gr.Button("生成干扰项")
#     with gr.Row():
#         output_options = gr.JSON(label="生成的干扰选项")
#     with gr.Row():
#         output_analysis = gr.Textbox(label="干扰项解析", lines=5)

#     # 定义按钮点击事件,注意这里不需要修改,Gradio会自动处理异步函数
#     generate_button.click(
#         fn=generate_distractors_gradio,
#         inputs=[question_input, option1_input, option2_input, option3_input, option4_input, answer_input, answer_analysis_input],
#         outputs=[output_options, output_analysis]
#     )

# # 运行Gradio应用
# demo.launch()




import gradio as gr
import asyncio
import os
import json
import urllib.request
from openai import AsyncOpenAI

# 第一个功能:检查YouTube视频是否具有Creative Commons许可证

# 请确保在环境变量中设置了您的YouTube Data API密钥
API_KEY = "AIzaSyDyPpkFRUpUuSMQbhxwTFxCBLK5qTHU-ms"

def get_youtube_id(youtube_url):
    if 'youtube.com' in youtube_url:
        video_id = youtube_url.split('v=')[-1]
        video_id = video_id.split('&')[0]  # 移除可能的额外参数
    elif 'youtu.be' in youtube_url:
        video_id = youtube_url.split('/')[-1].split('?')[0]
    else:
        video_id = ''
    return video_id

def check_cc_license(youtube_url):
    # 从URL中提取视频ID
    video_id = get_youtube_id(youtube_url)
    if not video_id:
        return "Invalid YouTube URL."
    
    # YouTube Data API URL,用于获取视频详情
    api_url = f'https://www.googleapis.com/youtube/v3/videos?id={video_id}&part=status&key={API_KEY}'
    
    try:
        # 获取视频详情
        response = urllib.request.urlopen(api_url)
        data = json.load(response)
        
        # 检查许可证状态
        if 'items' in data and len(data['items']) > 0:
            item = data['items'][0]
            if item['status']['license'] == 'creativeCommon':
                return "Yes."
            else:
                return "No."
        else:
            return "Video not found."
                
    except Exception as e:
        return f"An error occurred: {str(e)}"

# 第二个功能:为多项选择题生成干扰项



# 从您的模块中导入必要的函数
from utils.generate_distractors import prepare_q_inputs, construct_prompt_textonly, generate_distractors
from utils.api_utils import generate_from_openai_chat_completion
# 修改generate_distractors函数,使其成为异步函数
# 假设generate_distractors函数定义在您的模块中,我们需要修改它
# 如果无法修改原始模块,请在此处重新定义

async def generate_distractors_async(model_name: str, 
                                     queries: list,
                                     n: int=1,
                                     max_tokens: int=4096):
    assert model_name in ["gpt-4o-mini", "gpt-4-turbo", "gpt-4o", "gpt-4o-2024-08-06"], "Invalid model name"

    client = AsyncOpenAI(api_key=os.environ.get("OPENAI_API_KEY"), base_url="https://yanlp.zeabur.app/v1")
    messages = prepare_q_inputs(queries)

    # 直接等待协程而不是使用asyncio.run()
    responses = await generate_from_openai_chat_completion(
        client,
        messages=messages, 
        engine_name=model_name,
        n=n,
        max_tokens=max_tokens,
        requests_per_minute=30,
        json_format=True
    )

    for query, response in zip(queries, responses):
        new_options = response
        if new_options and "distractors" in new_options:
            query["option_5"] = new_options["distractors"].get("E", "")
            query["option_6"] = new_options["distractors"].get("F", "")
            query["option_7"] = new_options["distractors"].get("G", "")
            query["distractor_analysis"] = new_options["distractors"].get("analysis_of_distractors", "")
        else:
            query["option_5"] = ""
            query["option_6"] = ""
            query["option_7"] = ""
            query["distractor_analysis"] = ""

    return queries

# 定义异步处理函数
async def generate_distractors_gradio(question, option1, option2, option3, option4, answer, answer_analysis):
    query = {
        'question': question,
        'option_1': option1,
        'option_2': option2,
        'option_3': option3,
        'option_4': option4,
        'answer': answer,
        'answer_analysis': answer_analysis
    }

    queries = [query]  # 因为函数期望的是一个列表

    # 调用异步生成干扰项的函数
    results = await generate_distractors_async(
        model_name="gpt-4o-mini",
        queries=queries,
        n=1,
        max_tokens=4096
    )

    # 提取结果
    result = results[0]
    new_options = {
        'E': result.get('option_5', ''),
        'F': result.get('option_6', ''),
        'G': result.get('option_7', '')
    }
    distractor_analysis = result.get('distractor_analysis', '')

    # 返回新的干扰项和分析
    return new_options, distractor_analysis


with gr.Blocks() as demo:
    gr.Markdown("# 多功能Gradio应用")
    
    with gr.Tabs():
        with gr.TabItem("YouTube Creative Commons检查器"):
            gr.Markdown("## 检查YouTube视频是否具有Creative Commons许可证")
            youtube_url_input = gr.Textbox(label="YouTube视频URL")
            cc_license_output = gr.Textbox(label="是否为Creative Commons许可证?")
            check_button = gr.Button("检查许可证")
            check_button.click(
                fn=check_cc_license,
                inputs=youtube_url_input,
                outputs=cc_license_output
            )
        with gr.TabItem("多项选择题干扰项生成器"):
            gr.Markdown("## 为多项选择题生成干扰项")
            with gr.Row():
                question_input = gr.Textbox(label="问题", lines=2)
            with gr.Row():
                option1_input = gr.Textbox(label="选项A")
                option2_input = gr.Textbox(label="选项B")
            with gr.Row():
                option3_input = gr.Textbox(label="选项C")
                option4_input = gr.Textbox(label="选项D")
            with gr.Row():
                answer_input = gr.Textbox(label="正确答案")
            with gr.Row():
                answer_analysis_input = gr.Textbox(label="答案解析", lines=3)
            generate_button = gr.Button("生成干扰项")
            output_options = gr.JSON(label="生成的干扰选项")
            output_analysis = gr.Textbox(label="干扰项解析", lines=5)
            generate_button.click(
                fn=generate_distractors_gradio,
                inputs=[question_input, option1_input, option2_input, option3_input, option4_input, answer_input, answer_analysis_input],
                outputs=[output_options, output_analysis]
            )

# 运行Gradio应用
demo.launch()