Mentosyevsky commited on
Commit
05e65b8
·
verified ·
1 Parent(s): 0e0ef71

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -12
app.py CHANGED
@@ -6,18 +6,46 @@ from video import analyze_video # 从 video.py 导入
6
  from audio import analyze_audio # 从 audio.py 导入
7
 
8
 
9
- def process_input(file, text):
10
  # 输入优先级: 文件 > 文本
11
  if file:
12
- try:
13
- ctext, lang = extract_text(file) # ✅ 提取文本 + 自动识别语言
14
- except Exception as e:
15
- return f"❌ File parsing failed: {str(e)}"
16
- elif text.strip():
17
- text = text.strip()
18
- lang = detect(text)
19
- else:
20
- return "⚠️ Please upload a file or input text"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  # 如果用户手动选择语言,则覆盖自动检测
23
  if selected_lang != "auto":
@@ -62,8 +90,8 @@ with gr.Blocks(theme=gr.themes.Default(spacing_size="sm")) as app:
62
 
63
  analyze_btn.click(
64
  fn=process_input,
65
- inputs=[file_input, text_input],
66
- outputs=output,
67
  api_name="analyze"
68
  )
69
 
 
6
  from audio import analyze_audio # 从 audio.py 导入
7
 
8
 
9
+ def process_input(file, text, selected_lang):
10
  # 输入优先级: 文件 > 文本
11
  if file:
12
+ ext = os.path.splitext(file)[-1].lower()
13
+
14
+ if ext in [".txt", ".pdf", ".docx"]:
15
+ try:
16
+ extracted_text, auto_lang = extract_text(file)
17
+ lang = selected_lang if selected_lang != "auto" else auto_lang
18
+ return analyze_text(extracted_text, lang)
19
+ except Exception as e:
20
+ return f" File parsing failed: {str(e)}"
21
+
22
+ elif ext in [".mp3", ".wav", ".m4a"]:
23
+ lang = None if selected_lang == "auto" else selected_lang
24
+ try:
25
+ return analyze_audio(file, lang)
26
+ except Exception as e:
27
+ return f"❌ Audio analysis failed: {str(e)}"
28
+
29
+ elif ext in [".mp4", ".mov"]:
30
+ lang = None if selected_lang == "auto" else selected_lang
31
+ try:
32
+ return analyze_video(file, lang)
33
+ except Exception as e:
34
+ return f"❌ Video analysis failed: {str(e)}"
35
+
36
+ else:
37
+ return "❌ Unsupported file type"
38
+
39
+ # 否则使用纯文本输入
40
+ elif text.strip():
41
+ lang = selected_lang if selected_lang != "auto" else detect(text)
42
+ try:
43
+ return analyze_text(text, lang)
44
+ except Exception as e:
45
+ return f"🔥 Analysis error: {str(e)}"
46
+
47
+ else:
48
+ return "⚠️ Please upload a file or enter some text."
49
 
50
  # 如果用户手动选择语言,则覆盖自动检测
51
  if selected_lang != "auto":
 
90
 
91
  analyze_btn.click(
92
  fn=process_input,
93
+ inputs=[file_input, text_input, lang_dropdown], # ✅ 必须包含 lang_dropdown
94
+ outputs=output
95
  api_name="analyze"
96
  )
97