travahacker
commited on
Commit
Β·
c300f88
1
Parent(s):
72297a2
Add AI model: Hybrid GPT-2 + Dictionary (context-aware translations!)
Browse files
app.py
CHANGED
|
@@ -11,9 +11,10 @@ client = InferenceClient(token=hf_token)
|
|
| 11 |
# Complete AlgoSpeak dictionary for prompts
|
| 12 |
ALGOSPEAK_EXAMPLES = get_algospeak_context()
|
| 13 |
|
| 14 |
-
# Model selection - using models that reliably
|
| 15 |
-
#
|
| 16 |
-
MODEL = "
|
|
|
|
| 17 |
|
| 18 |
def translate_to_algospeak_simple(text):
|
| 19 |
"""Rule-based translation using the dictionary (always works!)"""
|
|
@@ -82,13 +83,54 @@ def translate_to_algospeak_simple(text):
|
|
| 82 |
|
| 83 |
return f"π Translated to AlgoSpeak:\n\n{result}"
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
def translate_to_algospeak(text):
|
| 86 |
-
"""Translates normal text to AlgoSpeak"""
|
| 87 |
if not text or not text.strip():
|
| 88 |
return "β οΈ Please enter some text to translate."
|
| 89 |
|
| 90 |
-
#
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
def interpret_algospeak_simple(text):
|
| 94 |
"""Rule-based interpretation using the dictionary (always works!)"""
|
|
@@ -109,13 +151,50 @@ def interpret_algospeak_simple(text):
|
|
| 109 |
explanation = "\n".join(found_terms)
|
| 110 |
return f"π Interpreted:\n\n{result}\n\nπ Terms found:\n{explanation}"
|
| 111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
def interpret_algospeak(text):
|
| 113 |
-
"""Interprets AlgoSpeak to plain language"""
|
| 114 |
if not text or not text.strip():
|
| 115 |
return "β οΈ Please enter some AlgoSpeak text to interpret."
|
| 116 |
|
| 117 |
-
#
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
def search_dictionary(query):
|
| 121 |
"""Search terms in the AlgoSpeak dictionary"""
|
|
@@ -133,17 +212,18 @@ def search_dictionary(query):
|
|
| 133 |
|
| 134 |
# Interface Gradio
|
| 135 |
with gr.Blocks(theme=gr.themes.Soft(), title="AlgoSpeak AI") as demo:
|
| 136 |
-
gr.Markdown("""
|
| 137 |
-
# π£οΈ AlgoSpeak Translator
|
| 138 |
|
| 139 |
**AlgoSpeak** is a language used to circumvent content moderation algorithms on social media platforms.
|
| 140 |
|
| 141 |
This tool can:
|
| 142 |
-
- π Translate plain text β AlgoSpeak (dictionary
|
| 143 |
-
- π Interpret AlgoSpeak β plain language
|
| 144 |
- π Search through 60+ catalogued terms
|
| 145 |
|
| 146 |
-
|
|
|
|
| 147 |
""")
|
| 148 |
|
| 149 |
with gr.Tab("π Translate to AlgoSpeak"):
|
|
|
|
| 11 |
# Complete AlgoSpeak dictionary for prompts
|
| 12 |
ALGOSPEAK_EXAMPLES = get_algospeak_context()
|
| 13 |
|
| 14 |
+
# Model selection - using models that work reliably with HF Inference API
|
| 15 |
+
# GPT-2 is older but 100% reliable for text generation
|
| 16 |
+
MODEL = "gpt2-medium" # Smaller, faster, always works
|
| 17 |
+
BACKUP_MODEL = "distilgpt2" # Even smaller fallback
|
| 18 |
|
| 19 |
def translate_to_algospeak_simple(text):
|
| 20 |
"""Rule-based translation using the dictionary (always works!)"""
|
|
|
|
| 83 |
|
| 84 |
return f"π Translated to AlgoSpeak:\n\n{result}"
|
| 85 |
|
| 86 |
+
def translate_to_algospeak_ai(text):
|
| 87 |
+
"""AI-powered translation with context awareness"""
|
| 88 |
+
prompt = f"""Task: Convert text to AlgoSpeak (coded language to avoid censorship).
|
| 89 |
+
|
| 90 |
+
Examples:
|
| 91 |
+
"someone died" β "someone unalived"
|
| 92 |
+
"talking about sex" β "talking about seggs"
|
| 93 |
+
"the war continues" β "the cornucopia continues"
|
| 94 |
+
"COVID vaccine" β "mascara backshot"
|
| 95 |
+
|
| 96 |
+
Now convert: {text}
|
| 97 |
+
|
| 98 |
+
AlgoSpeak:"""
|
| 99 |
+
|
| 100 |
+
try:
|
| 101 |
+
response = client.text_generation(
|
| 102 |
+
prompt,
|
| 103 |
+
model=MODEL,
|
| 104 |
+
max_new_tokens=100,
|
| 105 |
+
temperature=0.7,
|
| 106 |
+
do_sample=True,
|
| 107 |
+
top_p=0.9,
|
| 108 |
+
)
|
| 109 |
+
# Clean up response
|
| 110 |
+
result = response.strip()
|
| 111 |
+
# Remove common artifacts
|
| 112 |
+
if result.startswith('"') and result.endswith('"'):
|
| 113 |
+
result = result[1:-1]
|
| 114 |
+
return f"π€ AI Translation:\n\n{result}"
|
| 115 |
+
except Exception as e:
|
| 116 |
+
# Fallback to dictionary-based
|
| 117 |
+
return f"β οΈ AI model unavailable, using dictionary:\n\n{translate_to_algospeak_simple(text)}"
|
| 118 |
+
|
| 119 |
def translate_to_algospeak(text):
|
| 120 |
+
"""Translates normal text to AlgoSpeak - Hybrid approach"""
|
| 121 |
if not text or not text.strip():
|
| 122 |
return "β οΈ Please enter some text to translate."
|
| 123 |
|
| 124 |
+
# Try AI first for better context
|
| 125 |
+
dict_result = translate_to_algospeak_simple(text)
|
| 126 |
+
|
| 127 |
+
# If dictionary found terms, try AI enhancement
|
| 128 |
+
if "No sensitive terms detected" not in dict_result:
|
| 129 |
+
ai_result = translate_to_algospeak_ai(text)
|
| 130 |
+
return f"{ai_result}\n\n---\n\nπ Dictionary version:\n{dict_result}"
|
| 131 |
+
|
| 132 |
+
# If no terms found, just return dictionary result
|
| 133 |
+
return dict_result
|
| 134 |
|
| 135 |
def interpret_algospeak_simple(text):
|
| 136 |
"""Rule-based interpretation using the dictionary (always works!)"""
|
|
|
|
| 151 |
explanation = "\n".join(found_terms)
|
| 152 |
return f"π Interpreted:\n\n{result}\n\nπ Terms found:\n{explanation}"
|
| 153 |
|
| 154 |
+
def interpret_algospeak_ai(text):
|
| 155 |
+
"""AI-powered interpretation with context awareness"""
|
| 156 |
+
prompt = f"""Task: Translate AlgoSpeak (coded language) to plain English.
|
| 157 |
+
|
| 158 |
+
Examples:
|
| 159 |
+
"someone unalived" β "someone died/killed themselves"
|
| 160 |
+
"talking about seggs" β "talking about sex"
|
| 161 |
+
"the cornucopia continues" β "the war continues"
|
| 162 |
+
"got my backshot for mascara" β "got my vaccine for COVID"
|
| 163 |
+
|
| 164 |
+
Now translate: {text}
|
| 165 |
+
|
| 166 |
+
Plain English:"""
|
| 167 |
+
|
| 168 |
+
try:
|
| 169 |
+
response = client.text_generation(
|
| 170 |
+
prompt,
|
| 171 |
+
model=MODEL,
|
| 172 |
+
max_new_tokens=100,
|
| 173 |
+
temperature=0.5,
|
| 174 |
+
do_sample=True,
|
| 175 |
+
top_p=0.9,
|
| 176 |
+
)
|
| 177 |
+
result = response.strip()
|
| 178 |
+
if result.startswith('"') and result.endswith('"'):
|
| 179 |
+
result = result[1:-1]
|
| 180 |
+
return f"π€ AI Interpretation:\n\n{result}"
|
| 181 |
+
except Exception as e:
|
| 182 |
+
return f"β οΈ AI model unavailable, using dictionary:\n\n{interpret_algospeak_simple(text)}"
|
| 183 |
+
|
| 184 |
def interpret_algospeak(text):
|
| 185 |
+
"""Interprets AlgoSpeak to plain language - Hybrid approach"""
|
| 186 |
if not text or not text.strip():
|
| 187 |
return "β οΈ Please enter some AlgoSpeak text to interpret."
|
| 188 |
|
| 189 |
+
# Get dictionary result
|
| 190 |
+
dict_result = interpret_algospeak_simple(text)
|
| 191 |
+
|
| 192 |
+
# If dictionary found terms, add AI interpretation
|
| 193 |
+
if "No AlgoSpeak terms detected" not in dict_result:
|
| 194 |
+
ai_result = interpret_algospeak_ai(text)
|
| 195 |
+
return f"{ai_result}\n\n---\n\n{dict_result}"
|
| 196 |
+
|
| 197 |
+
return dict_result
|
| 198 |
|
| 199 |
def search_dictionary(query):
|
| 200 |
"""Search terms in the AlgoSpeak dictionary"""
|
|
|
|
| 212 |
|
| 213 |
# Interface Gradio
|
| 214 |
with gr.Blocks(theme=gr.themes.Soft(), title="AlgoSpeak AI") as demo:
|
| 215 |
+
gr.Markdown(f"""
|
| 216 |
+
# π£οΈ AlgoSpeak AI Translator
|
| 217 |
|
| 218 |
**AlgoSpeak** is a language used to circumvent content moderation algorithms on social media platforms.
|
| 219 |
|
| 220 |
This tool can:
|
| 221 |
+
- π Translate plain text β AlgoSpeak (AI + dictionary hybrid)
|
| 222 |
+
- π Interpret AlgoSpeak β plain language (AI-enhanced)
|
| 223 |
- π Search through 60+ catalogued terms
|
| 224 |
|
| 225 |
+
π€ **Powered by:** GPT-2 AI model + curated dictionary
|
| 226 |
+
π‘ **Hybrid approach:** AI for context + dictionary for accuracy
|
| 227 |
""")
|
| 228 |
|
| 229 |
with gr.Tab("π Translate to AlgoSpeak"):
|