EthosAI / app.py
Mentosyevsky's picture
Update app.py
5041430 verified
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
)