Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,14 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
# --- CONFIGURATION ---
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# --- LOAD RESOURCES ---
|
| 9 |
print("Loading Tokenizer...")
|
|
@@ -13,53 +18,96 @@ print("Loading Models...")
|
|
| 13 |
model_k2h = AutoModelForSeq2SeqLM.from_pretrained(MODEL_K2H_REPO)
|
| 14 |
model_h2k = AutoModelForSeq2SeqLM.from_pretrained(MODEL_H2K_REPO)
|
| 15 |
|
| 16 |
-
# Create Pipelines
|
| 17 |
pipe_k2h = pipeline("text2text-generation", model=model_k2h, tokenizer=tokenizer)
|
| 18 |
pipe_h2k = pipeline("text2text-generation", model=model_h2k, tokenizer=tokenizer)
|
| 19 |
|
| 20 |
-
# ---
|
| 21 |
def translate_text(text, direction):
|
| 22 |
if not text:
|
| 23 |
return ""
|
| 24 |
-
|
| 25 |
target_pipeline = pipe_k2h if direction == "Kurukh -> Hindi" else pipe_h2k
|
| 26 |
-
|
| 27 |
try:
|
| 28 |
results = target_pipeline(text, max_length=128)
|
| 29 |
return results[0]['generated_text']
|
| 30 |
except Exception as e:
|
| 31 |
return f"Error: {str(e)}"
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
gr.Markdown("### Preserving Tribal Languages with Artificial Intelligence")
|
| 39 |
|
| 40 |
-
with
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
translate_btn = gr.Button("Translate 🚀", variant="primary")
|
| 52 |
|
| 53 |
-
gr.
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
["मुझे पानी दो।", "Hindi -> Kurukh"]
|
| 59 |
-
],
|
| 60 |
-
inputs=[input_text, direction]
|
| 61 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
+
import csv
|
| 4 |
+
import os
|
| 5 |
+
from datetime import datetime
|
| 6 |
|
| 7 |
# --- CONFIGURATION ---
|
| 8 |
+
|
| 9 |
+
MODEL_K2H_REPO = "ankitklakra/kurukh-to-hindi"
|
| 10 |
+
MODEL_H2K_REPO = "ankitklakra/hindi-to-kurukh"
|
| 11 |
+
FEEDBACK_FILE = "feedback_log.csv"
|
| 12 |
|
| 13 |
# --- LOAD RESOURCES ---
|
| 14 |
print("Loading Tokenizer...")
|
|
|
|
| 18 |
model_k2h = AutoModelForSeq2SeqLM.from_pretrained(MODEL_K2H_REPO)
|
| 19 |
model_h2k = AutoModelForSeq2SeqLM.from_pretrained(MODEL_H2K_REPO)
|
| 20 |
|
|
|
|
| 21 |
pipe_k2h = pipeline("text2text-generation", model=model_k2h, tokenizer=tokenizer)
|
| 22 |
pipe_h2k = pipeline("text2text-generation", model=model_h2k, tokenizer=tokenizer)
|
| 23 |
|
| 24 |
+
# --- HELPER FUNCTIONS ---
|
| 25 |
def translate_text(text, direction):
|
| 26 |
if not text:
|
| 27 |
return ""
|
|
|
|
| 28 |
target_pipeline = pipe_k2h if direction == "Kurukh -> Hindi" else pipe_h2k
|
|
|
|
| 29 |
try:
|
| 30 |
results = target_pipeline(text, max_length=128)
|
| 31 |
return results[0]['generated_text']
|
| 32 |
except Exception as e:
|
| 33 |
return f"Error: {str(e)}"
|
| 34 |
|
| 35 |
+
def save_feedback(original_text, translation, corrected_text, direction):
|
| 36 |
+
"""Saves user corrections to a CSV file"""
|
| 37 |
+
if not corrected_text:
|
| 38 |
+
return "⚠️ Please enter a correction before submitting."
|
| 39 |
|
| 40 |
+
file_exists = os.path.isfile(FEEDBACK_FILE)
|
|
|
|
| 41 |
|
| 42 |
+
with open(FEEDBACK_FILE, mode='a', newline='', encoding='utf-8') as file:
|
| 43 |
+
writer = csv.writer(file)
|
| 44 |
+
# Write header if file is new
|
| 45 |
+
if not file_exists:
|
| 46 |
+
writer.writerow(["Timestamp", "Direction", "Original", "AI_Translation", "User_Correction"])
|
| 47 |
+
|
| 48 |
+
writer.writerow([datetime.now(), direction, original_text, translation, corrected_text])
|
| 49 |
|
| 50 |
+
return "✅ Thank you! Your correction has been saved to improve the model."
|
| 51 |
+
|
| 52 |
+
# --- THE UI ---
|
| 53 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
|
|
| 54 |
|
| 55 |
+
gr.Markdown(
|
| 56 |
+
"""
|
| 57 |
+
# 🇮🇳 AI Kurukh (Kurux) Translator
|
| 58 |
+
### Preserving Tribal Languages with Artificial Intelligence
|
| 59 |
+
"""
|
|
|
|
|
|
|
|
|
|
| 60 |
)
|
| 61 |
+
|
| 62 |
+
with gr.Tabs():
|
| 63 |
+
# TAB 1: TRANSLATOR
|
| 64 |
+
with gr.TabItem("🗣️ Translator"):
|
| 65 |
+
with gr.Row():
|
| 66 |
+
direction = gr.Radio(
|
| 67 |
+
["Kurukh -> Hindi", "Hindi -> Kurukh"],
|
| 68 |
+
label="Translation Mode",
|
| 69 |
+
value="Kurukh -> Hindi"
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
with gr.Row():
|
| 73 |
+
with gr.Column():
|
| 74 |
+
input_text = gr.Textbox(label="Input Text", placeholder="Type here...", lines=4)
|
| 75 |
+
translate_btn = gr.Button("Translate 🚀", variant="primary")
|
| 76 |
+
|
| 77 |
+
with gr.Column():
|
| 78 |
+
output_text = gr.Textbox(label="AI Translation", lines=4, interactive=False)
|
| 79 |
+
|
| 80 |
+
translate_btn.click(fn=translate_text, inputs=[input_text, direction], outputs=output_text)
|
| 81 |
+
|
| 82 |
+
gr.Examples(
|
| 83 |
+
examples=[
|
| 84 |
+
["निघै नामे इन्द्रा हिकै?", "Kurukh -> Hindi"],
|
| 85 |
+
["इन्गे अम्मो चि'आ।", "Kurukh -> Hindi"],
|
| 86 |
+
["तुम्हारा नाम क्या है?", "Hindi -> Kurukh"]
|
| 87 |
+
],
|
| 88 |
+
inputs=[input_text, direction]
|
| 89 |
+
)
|
| 90 |
|
| 91 |
+
# TAB 2: FEEDBACK (THE NEW FEATURE)
|
| 92 |
+
with gr.TabItem("📝 Improve the AI"):
|
| 93 |
+
gr.Markdown("### Help us make this model better!")
|
| 94 |
+
gr.Markdown("If the AI made a mistake, please paste the text below and tell us the correct translation.")
|
| 95 |
+
|
| 96 |
+
with gr.Row():
|
| 97 |
+
fb_direction = gr.Radio(["Kurukh -> Hindi", "Hindi -> Kurukh"], label="Direction", value="Kurukh -> Hindi")
|
| 98 |
+
|
| 99 |
+
with gr.Row():
|
| 100 |
+
fb_original = gr.Textbox(label="Original Text")
|
| 101 |
+
fb_ai_output = gr.Textbox(label="What the AI said (Optional)")
|
| 102 |
+
fb_user_correct = gr.Textbox(label="What it SHOULD be (Your Correction)", lines=2)
|
| 103 |
+
|
| 104 |
+
submit_fb_btn = gr.Button("Submit Correction ✅")
|
| 105 |
+
fb_status = gr.Label(label="Status")
|
| 106 |
+
|
| 107 |
+
submit_fb_btn.click(
|
| 108 |
+
fn=save_feedback,
|
| 109 |
+
inputs=[fb_original, fb_ai_output, fb_user_correct, fb_direction],
|
| 110 |
+
outputs=fb_status
|
| 111 |
+
)
|
| 112 |
|
| 113 |
demo.launch()
|