File size: 3,589 Bytes
96e3a4b
 
 
c566bb5
 
e773227
96e3a4b
 
 
9d81305
 
05e65b8
3413beb
 
 
 
d2502fb
4f02099
 
e6c7ef0
d2502fb
05e65b8
d2502fb
 
 
 
 
 
 
05e65b8
d2502fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f79eda
d2502fb
 
 
 
 
 
 
 
 
96e3a4b
 
801cd15
 
 
 
 
 
 
 
 
 
 
 
83551d0
4f02099
1a8a676
801cd15
0e0ef71
 
 
 
 
801cd15
 
 
 
 
 
 
 
 
36ae5b5
801cd15
 
05e65b8
1b30570
5041430
3413beb
801cd15
 
83551d0
 
801cd15
75cdd8c
 
 
 
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
import gradio as gr
import magic
import tempfile
import os
from langdetect import detect  # or your own language detector
from text import analyze_text            # 从 text.py 导入
from video import analyze_video          # 从 video.py 导入 
from audio import analyze_audio          # 从 audio.py 导入



def process_input(file, text, selected_lang):
    print("[DEBUG] 🟢 Analyze 按钮被点击")
    print(f"[DEBUG] file: {file}")
    print(f"[DEBUG] text: {text[:50]}")
    print(f"[DEBUG] lang: {selected_lang}")

    # 输入优先级: 文件 > 文本
    if file:
        ext = os.path.splitext(file)[-1].lower()

        if ext in [".txt", ".pdf", ".docx"]:
            try:
                extracted_text, auto_lang = extract_text(file)
                lang = selected_lang if selected_lang != "auto" else auto_lang
                print(f"[DEBUG] 使用语言: {lang}")
                return analyze_text(extracted_text, lang)
            except Exception as e:
                return f"❌ File parsing failed: {str(e)}"

        elif ext in [".mp3", ".wav", ".m4a"]:
            lang = None if selected_lang == "auto" else selected_lang
            try:
                return analyze_audio(file, lang)
            except Exception as e:
                return f"❌ Audio analysis failed: {str(e)}"

        elif ext in [".mp4", ".mov"]:
            lang = None if selected_lang == "auto" else selected_lang
            try:
                return analyze_video(file, lang)
            except Exception as e:
                return f"❌ Video analysis failed: {str(e)}"

        else:
            return "❌ Unsupported file type"

    # 如果没有上传文件,但用户输入了文本
    elif text and text.strip():
        lang = selected_lang if selected_lang != "auto" else detect(text)
        print(f"[DEBUG] 使用语言: {lang}")
        try:
            return analyze_text(text, lang)
        except Exception as e:
            return f"🔥 Analysis error: {str(e)}"

    else:
        return "⚠️ Please upload a file or enter some text."

# 构建交互界面
import gradio as gr
from text import analyze_text, extract_text

with gr.Blocks(theme=gr.themes.Default(spacing_size="sm")) as app:
    gr.Markdown("""
    # 🌐 EthosAI - Media Integrity Toolkit  
    *Empowering ethical journalism through AI-driven content analysis*
    """)
    
    with gr.Row():
        with gr.Column(scale=2):
            file_input = gr.File(
                label="📁 Upload File (Text / Video / Audio)",
                file_types=[".pdf", ".docx", ".txt", ".mp4", ".mov", ".wav", ".mp3", ".m4a"],
                type="filepath"  # ✅ 返回路径字符串,而不是 NamedString/File 对象
            )
            lang_dropdown = gr.Dropdown(
                label="🌐 Language",
                choices=["auto", "en", "zh", "fr", "es", "ar", "ru"],
                value="auto"
            )
        with gr.Column(scale=3):
            text_input = gr.Textbox(
                label="✍️ Or Input Text Directly",
                placeholder="Paste your content here...",
                lines=8
            )
    
    analyze_btn = gr.Button("🔍 Analyze Content", variant="primary")
    output = gr.Markdown()
    
    analyze_btn.click(
        fn=process_input,
        inputs=[file_input, text_input, lang_dropdown], # ✅ 必须包含 lang_dropdown
        outputs=output,
        api_name="analyze",
        show_progress="full"
    )


# 启动应用
app.launch(
    server_name="0.0.0.0",
    server_port=7860,
    debug=True
)