Mentosyevsky commited on
Commit
4f02099
·
verified ·
1 Parent(s): 602fac3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -40
app.py CHANGED
@@ -5,30 +5,23 @@ from text import analyze_text # 从 text.py 导入
5
  from video import analyze_video # 从 video.py 导入
6
  from audio import analyze_audio # 从 audio.py 导入
7
 
8
- def process_file(file):
9
- try:
10
- # 检测文件类型
11
- file_type = magic.from_buffer(file.read(1024), mime=True).split('/')[0]
12
- file.seek(0)
13
 
14
- # 保存临时文件
15
- with tempfile.NamedTemporaryFile(suffix=file.name) as tmp:
16
- tmp.write(file.read())
17
- tmp.seek(0)
18
-
19
- # 分发处理逻辑
20
- if file_type == "text":
21
- return analyze_text(tmp.name)
22
- elif file_type == "video":
23
- return analyze_video(tmp.name)
24
- elif file_type == "audio":
25
- return analyze_audio(tmp.name)
26
- else:
27
- return {"error": "Unsupported file type"}
28
-
29
  except Exception as e:
30
- return {"error": f"Processing failed: {str(e)}"}
31
-
32
 
33
  # 构建交互界面
34
  import gradio as gr
@@ -44,7 +37,7 @@ with gr.Blocks(theme=gr.themes.Default(spacing_size="sm")) as app:
44
  with gr.Column(scale=2):
45
  file_input = gr.File(
46
  label="📁 Upload File (Text / Video / Audio)",
47
- file_types=[".pdf", ".docx", ".txt", ".mp4", ".mov", ".wav", ".mp3", ".m4a"]
48
  type="filepath" # ✅ 返回路径字符串,而不是 NamedString/File 对象
49
  )
50
  with gr.Column(scale=3):
@@ -56,23 +49,6 @@ with gr.Blocks(theme=gr.themes.Default(spacing_size="sm")) as app:
56
 
57
  analyze_btn = gr.Button("🔍 Analyze Content", variant="primary")
58
  output = gr.Markdown()
59
-
60
- def process_input(file, text):
61
- # 输入优先级: 文件 > 文本
62
- if file:
63
- try:
64
- ctext, lang = extract_text(file) # ✅ 提取文本 + 自动识别语言
65
- except Exception as e:
66
- return f"❌ File parsing failed: {str(e)}"
67
- elif text.strip():
68
- content = text
69
- else:
70
- return "⚠️ Please upload a file or input text"
71
-
72
- try:
73
- return analyze_text(content)
74
- except Exception as e:
75
- return f"🔥 Analysis error: {str(e)}"
76
 
77
  analyze_btn.click(
78
  fn=process_input,
 
5
  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
+ content = text
18
+ else:
19
+ return "⚠️ Please upload a file or input text"
20
+
21
+ try:
22
+ return analyze_text(text, lang)
 
23
  except Exception as e:
24
+ return f"🔥 Analysis error: {str(e)}"
 
25
 
26
  # 构建交互界面
27
  import gradio as gr
 
37
  with gr.Column(scale=2):
38
  file_input = gr.File(
39
  label="📁 Upload File (Text / Video / Audio)",
40
+ file_types=[".pdf", ".docx", ".txt", ".mp4", ".mov", ".wav", ".mp3", ".m4a"],
41
  type="filepath" # ✅ 返回路径字符串,而不是 NamedString/File 对象
42
  )
43
  with gr.Column(scale=3):
 
49
 
50
  analyze_btn = gr.Button("🔍 Analyze Content", variant="primary")
51
  output = gr.Markdown()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
  analyze_btn.click(
54
  fn=process_input,