Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,44 @@
|
|
| 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 |
MODEL_K2H_REPO = "ankitklakra/kurukh-to-hindi"
|
| 9 |
MODEL_H2K_REPO = "ankitklakra/hindi-to-kurukh"
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# --- LOAD RESOURCES ---
|
| 13 |
print("Loading Tokenizer...")
|
|
@@ -20,7 +51,7 @@ model_h2k = AutoModelForSeq2SeqLM.from_pretrained(MODEL_H2K_REPO)
|
|
| 20 |
pipe_k2h = pipeline("text2text-generation", model=model_k2h, tokenizer=tokenizer)
|
| 21 |
pipe_h2k = pipeline("text2text-generation", model=model_h2k, tokenizer=tokenizer)
|
| 22 |
|
| 23 |
-
# ---
|
| 24 |
def translate_text(text, direction):
|
| 25 |
if not text:
|
| 26 |
return ""
|
|
@@ -31,25 +62,8 @@ def translate_text(text, direction):
|
|
| 31 |
except Exception as e:
|
| 32 |
return f"Error: {str(e)}"
|
| 33 |
|
| 34 |
-
def save_feedback(original_text, translation, corrected_text, direction):
|
| 35 |
-
"""Saves user corrections to a CSV file"""
|
| 36 |
-
if not corrected_text:
|
| 37 |
-
return "⚠️ Please enter a correction before submitting."
|
| 38 |
-
|
| 39 |
-
file_exists = os.path.isfile(FEEDBACK_FILE)
|
| 40 |
-
|
| 41 |
-
with open(FEEDBACK_FILE, mode='a', newline='', encoding='utf-8') as file:
|
| 42 |
-
writer = csv.writer(file)
|
| 43 |
-
if not file_exists:
|
| 44 |
-
writer.writerow(["Timestamp", "Direction", "Original", "AI_Translation", "User_Correction"])
|
| 45 |
-
writer.writerow([datetime.now(), direction, original_text, translation, corrected_text])
|
| 46 |
-
|
| 47 |
-
return "✅ Thank you! Your correction has been saved."
|
| 48 |
-
|
| 49 |
# --- THE UI ---
|
| 50 |
-
|
| 51 |
with gr.Blocks() as demo:
|
| 52 |
-
|
| 53 |
gr.Markdown("# 🇮🇳 AI Kurukh (Kurux) Translator")
|
| 54 |
gr.Markdown("### Preserving Tribal Languages with Artificial Intelligence")
|
| 55 |
|
|
@@ -57,50 +71,39 @@ with gr.Blocks() as demo:
|
|
| 57 |
# TAB 1: TRANSLATOR
|
| 58 |
with gr.TabItem("🗣️ Translator"):
|
| 59 |
with gr.Row():
|
| 60 |
-
direction = gr.Radio(
|
| 61 |
-
["Kurukh -> Hindi", "Hindi -> Kurukh"],
|
| 62 |
-
label="Translation Mode",
|
| 63 |
-
value="Kurukh -> Hindi"
|
| 64 |
-
)
|
| 65 |
-
|
| 66 |
with gr.Row():
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
translate_btn = gr.Button("Translate 🚀", variant="primary")
|
| 70 |
-
|
| 71 |
-
with gr.Column():
|
| 72 |
-
output_text = gr.Textbox(label="AI Translation", lines=4, interactive=False)
|
| 73 |
|
|
|
|
| 74 |
translate_btn.click(fn=translate_text, inputs=[input_text, direction], outputs=output_text)
|
| 75 |
|
| 76 |
gr.Examples(
|
| 77 |
examples=[
|
| 78 |
["निघै नामे इन्द्रा हिकै?", "Kurukh -> Hindi"],
|
| 79 |
-
["इन्गे अम्मो चि'आ।", "Kurukh -> Hindi"],
|
| 80 |
["तुम्हारा नाम क्या है?", "Hindi -> Kurukh"]
|
| 81 |
],
|
| 82 |
inputs=[input_text, direction]
|
| 83 |
)
|
| 84 |
|
| 85 |
-
# TAB 2: FEEDBACK
|
| 86 |
with gr.TabItem("📝 Improve the AI"):
|
| 87 |
-
gr.Markdown("### Help us
|
| 88 |
-
|
| 89 |
with gr.Row():
|
| 90 |
fb_direction = gr.Radio(["Kurukh -> Hindi", "Hindi -> Kurukh"], label="Direction", value="Kurukh -> Hindi")
|
| 91 |
-
|
| 92 |
with gr.Row():
|
| 93 |
fb_original = gr.Textbox(label="Original Text")
|
| 94 |
fb_ai_output = gr.Textbox(label="What the AI said (Optional)")
|
| 95 |
-
fb_user_correct = gr.Textbox(label="
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
|
| 100 |
-
|
| 101 |
-
fn=
|
| 102 |
inputs=[fb_original, fb_ai_output, fb_user_correct, fb_direction],
|
| 103 |
-
outputs=
|
| 104 |
)
|
| 105 |
|
| 106 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
|
|
|
|
| 3 |
import os
|
| 4 |
+
import json
|
| 5 |
+
import gspread
|
| 6 |
+
from oauth2client.service_account import ServiceAccountCredentials
|
| 7 |
from datetime import datetime
|
| 8 |
|
| 9 |
# --- CONFIGURATION ---
|
| 10 |
MODEL_K2H_REPO = "ankitklakra/kurukh-to-hindi"
|
| 11 |
MODEL_H2K_REPO = "ankitklakra/hindi-to-kurukh"
|
| 12 |
+
SHEET_NAME = "Kurukh_Feedback_Log"
|
| 13 |
+
|
| 14 |
+
# --- GOOGLE SHEETS SETUP ---
|
| 15 |
+
def save_to_sheet(original, translation, correction, direction):
|
| 16 |
+
try:
|
| 17 |
+
# 1. Get credentials from Hugging Face Secrets
|
| 18 |
+
json_creds = os.getenv("GOOGLE_CREDENTIALS")
|
| 19 |
+
if not json_creds:
|
| 20 |
+
return "⚠️ Error: Google Credentials not found in Settings."
|
| 21 |
+
|
| 22 |
+
creds_dict = json.loads(json_creds)
|
| 23 |
+
|
| 24 |
+
# 2. Authenticate
|
| 25 |
+
scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
|
| 26 |
+
creds = ServiceAccountCredentials.from_json_keyfile_dict(creds_dict, scope)
|
| 27 |
+
client = gspread.authorize(creds)
|
| 28 |
+
|
| 29 |
+
# 3. Open Sheet
|
| 30 |
+
sheet = client.open(SHEET_NAME).sheet1
|
| 31 |
+
|
| 32 |
+
# 4. Add Headers if empty
|
| 33 |
+
if not sheet.get_all_values():
|
| 34 |
+
sheet.append_row(["Timestamp", "Direction", "Original Text", "AI Translation", "User Correction"])
|
| 35 |
+
|
| 36 |
+
# 5. Append Row
|
| 37 |
+
sheet.append_row([str(datetime.now()), direction, original, translation, correction])
|
| 38 |
+
return "✅ Success! Saved to Google Sheets."
|
| 39 |
+
|
| 40 |
+
except Exception as e:
|
| 41 |
+
return f"❌ Error saving to Sheet: {str(e)}"
|
| 42 |
|
| 43 |
# --- LOAD RESOURCES ---
|
| 44 |
print("Loading Tokenizer...")
|
|
|
|
| 51 |
pipe_k2h = pipeline("text2text-generation", model=model_k2h, tokenizer=tokenizer)
|
| 52 |
pipe_h2k = pipeline("text2text-generation", model=model_h2k, tokenizer=tokenizer)
|
| 53 |
|
| 54 |
+
# --- TRANSLATION FUNCTION ---
|
| 55 |
def translate_text(text, direction):
|
| 56 |
if not text:
|
| 57 |
return ""
|
|
|
|
| 62 |
except Exception as e:
|
| 63 |
return f"Error: {str(e)}"
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
# --- THE UI ---
|
|
|
|
| 66 |
with gr.Blocks() as demo:
|
|
|
|
| 67 |
gr.Markdown("# 🇮🇳 AI Kurukh (Kurux) Translator")
|
| 68 |
gr.Markdown("### Preserving Tribal Languages with Artificial Intelligence")
|
| 69 |
|
|
|
|
| 71 |
# TAB 1: TRANSLATOR
|
| 72 |
with gr.TabItem("🗣️ Translator"):
|
| 73 |
with gr.Row():
|
| 74 |
+
direction = gr.Radio(["Kurukh -> Hindi", "Hindi -> Kurukh"], label="Translation Mode", value="Kurukh -> Hindi")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
with gr.Row():
|
| 76 |
+
input_text = gr.Textbox(label="Input Text", placeholder="Type here...", lines=4)
|
| 77 |
+
output_text = gr.Textbox(label="AI Translation", lines=4, interactive=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
+
translate_btn = gr.Button("Translate 🚀", variant="primary")
|
| 80 |
translate_btn.click(fn=translate_text, inputs=[input_text, direction], outputs=output_text)
|
| 81 |
|
| 82 |
gr.Examples(
|
| 83 |
examples=[
|
| 84 |
["निघै नामे इन्द्रा हिकै?", "Kurukh -> Hindi"],
|
|
|
|
| 85 |
["तुम्हारा नाम क्या है?", "Hindi -> Kurukh"]
|
| 86 |
],
|
| 87 |
inputs=[input_text, direction]
|
| 88 |
)
|
| 89 |
|
| 90 |
+
# TAB 2: GOOGLE SHEETS FEEDBACK
|
| 91 |
with gr.TabItem("📝 Improve the AI"):
|
| 92 |
+
gr.Markdown("### Help us improve!")
|
|
|
|
| 93 |
with gr.Row():
|
| 94 |
fb_direction = gr.Radio(["Kurukh -> Hindi", "Hindi -> Kurukh"], label="Direction", value="Kurukh -> Hindi")
|
|
|
|
| 95 |
with gr.Row():
|
| 96 |
fb_original = gr.Textbox(label="Original Text")
|
| 97 |
fb_ai_output = gr.Textbox(label="What the AI said (Optional)")
|
| 98 |
+
fb_user_correct = gr.Textbox(label="Your Correction (Required)", lines=2)
|
| 99 |
|
| 100 |
+
submit_btn = gr.Button("Submit")
|
| 101 |
+
status_lbl = gr.Label(label="Status")
|
| 102 |
|
| 103 |
+
submit_btn.click(
|
| 104 |
+
fn=save_to_sheet,
|
| 105 |
inputs=[fb_original, fb_ai_output, fb_user_correct, fb_direction],
|
| 106 |
+
outputs=status_lbl
|
| 107 |
)
|
| 108 |
|
| 109 |
demo.launch()
|