Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
# Load language detection model
|
5 |
+
lang_classifier = pipeline("text-classification", model="papluca/xlm-roberta-base-language-detection")
|
6 |
+
|
7 |
+
# Load translation model (multi-language to English)
|
8 |
+
translator = pipeline("translation", model="facebook/nllb-200-distilled-600M")
|
9 |
+
|
10 |
+
# Load hate speech detection model
|
11 |
+
offensive_classifier = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-offensive")
|
12 |
+
|
13 |
+
# Mapping from ISO 639-1 to NLLB-200 language codes
|
14 |
+
LANGUAGE_CODES = {
|
15 |
+
"en": "eng_Latn", "fr": "fra_Latn", "es": "spa_Latn", "de": "deu_Latn",
|
16 |
+
"bg": "bul_Cyrl", "ru": "rus_Cyrl", "it": "ita_Latn", "zh": "zho_Hans",
|
17 |
+
"ar": "arb_Arab", "pt": "por_Latn", "nl": "nld_Latn", "hi": "hin_Deva"
|
18 |
+
}
|
19 |
+
|
20 |
+
def analyze_text(text):
|
21 |
+
if not text.strip():
|
22 |
+
return {"error": "No text provided"}, {"error": "No text provided"}
|
23 |
+
|
24 |
+
# Detect language
|
25 |
+
lang_result = lang_classifier(text)
|
26 |
+
detected_language = lang_result[0]['label']
|
27 |
+
language_confidence = lang_result[0]['score']
|
28 |
+
|
29 |
+
# Convert detected language to NLLB-200 format
|
30 |
+
detected_language_nllb = LANGUAGE_CODES.get(detected_language, "eng_Latn")
|
31 |
+
|
32 |
+
# Translate if not English
|
33 |
+
translated_text = text
|
34 |
+
if detected_language_nllb != "eng_Latn":
|
35 |
+
translation_result = translator(text, src_lang=detected_language_nllb, tgt_lang="eng_Latn")
|
36 |
+
translated_text = translation_result[0]['translation_text']
|
37 |
+
|
38 |
+
# Detect hate speech using the translated text
|
39 |
+
hate_result = offensive_classifier(translated_text)
|
40 |
+
|
41 |
+
language_output = {
|
42 |
+
"language": detected_language,
|
43 |
+
"confidence": language_confidence,
|
44 |
+
"original_text": text,
|
45 |
+
"translated_text": translated_text if detected_language_nllb != "eng_Latn" else "Already in English"
|
46 |
+
}
|
47 |
+
|
48 |
+
hate_output = {
|
49 |
+
"label": hate_result[0]['label'],
|
50 |
+
"score": hate_result[0]['score']
|
51 |
+
}
|
52 |
+
|
53 |
+
return language_output, hate_output
|
54 |
+
|
55 |
+
# Define the Gradio interface
|
56 |
+
iface = gr.Interface(
|
57 |
+
fn=analyze_text,
|
58 |
+
inputs=gr.Textbox(label="Enter text"),
|
59 |
+
outputs=[
|
60 |
+
gr.JSON(label="Language Detection & Translation"),
|
61 |
+
gr.JSON(label="Hate Speech Detection")
|
62 |
+
],
|
63 |
+
title="Detect language, translate, and check for offensive speech",
|
64 |
+
description="Enter text..."
|
65 |
+
)
|
66 |
+
|
67 |
+
# Launch the Gradio app
|
68 |
+
iface.launch(server_name="0.0.0.0", server_port=7860, share=True)
|