import torch import gradio as gr import json import time import random from transformers import pipeline import pycountry from datetime import datetime from pydantic import BaseModel, PydanticUserError, ConfigDict from pydantic import BaseModel, ConfigDict import json class MyModel(BaseModel): request: 'starlette.requests.Request' model_config = ConfigDict(arbitrary_types_allowed=True) from pydantic_core import core_schema from starlette.requests import Request def get_pydantic_core_schema(request_type, handler): return core_schema.any_schema() Request.__get_pydantic_core_schema__ = get_pydantic_core_schema # Initialize pipelines with error handling try: lang_detector = pipeline("text-classification", model="papluca/xlm-roberta-base-language-detection") #text_translator = pipeline("translation", model="facebook/nllb-200-distilled-600M", torch_dtype=torch.bfloat16) text_translator = pipeline("translation", model="facebook/nllb-200-distilled-600M") #text_translator = pipeline("translation", model="facebook/nllb-200-418M") print("🚀 AI Translation Hub initialized successfully!") except Exception as e: print(f"⚠️ Error initializing models: {e}") # Extended language support with emojis LANGUAGES = { 'English': '🇺🇸', 'Spanish': '🇪🇸', 'French': '🇫🇷', 'German': '🇩🇪', 'Italian': '🇮🇹', 'Portuguese': '🇵🇹', 'Russian': '🇷🇺', 'Chinese (Simplified)': '🇨🇳', 'Japanese': '🇯🇵', 'Korean': '🇰🇷', 'Arabic': '🇸🇦', 'Hindi': '🇮🇳', 'Dutch': '🇳🇱', 'Swedish': '🇸🇪', 'Norwegian': '🇳🇴' } # Load language data with fallback try: with open('language.json', 'r') as file: language_data = json.load(file) except FileNotFoundError: print("⚠️ Language data file not found. Using basic mapping.") language_data = {'languages': []} # Translation statistics translation_stats = { 'total_translations': 0, 'languages_detected': set(), 'session_start': datetime.now() } def get_FLORES_code_from_language(language): """Enhanced FLORES code lookup with fallback mapping""" # Remove emoji and extract clean language name import re # Remove emoji flags and extra spaces clean_language = re.sub(r'[🇦-🇿]{2}\s*', '', language).strip() for entry in language_data.get('languages', []): if entry['Language'].lower() == clean_language.lower(): return entry['FLORES-200 code'] # Fallback mapping for common languages fallback_mapping = { 'english': 'eng_Latn', 'spanish': 'spa_Latn', 'french': 'fra_Latn', 'german': 'deu_Latn', 'chinese (simplified)': 'zho_Hans', 'italian': 'ita_Latn', 'portuguese': 'por_Latn', 'russian': 'rus_Cyrl', 'japanese': 'jpn_Jpan', 'korean': 'kor_Hang', 'arabic': 'arb_Arab', 'hindi': 'hin_Deva', 'dutch': 'nld_Latn', 'swedish': 'swe_Latn', 'norwegian': 'nor_Latn' } return fallback_mapping.get(clean_language.lower()) def detect_language_confidence(text): """Get language detection with confidence score""" if not text.strip(): return "Unknown", 0.0 try: result = lang_detector(text)[0] return result['label'], result['score'] except: return "Unknown", 0.0 def translate_with_analytics(text, destination_language, show_confidence=True): """Enhanced translation with analytics and progress tracking""" if not text.strip(): return "⚠️ Please enter some text to translate", "", "" # Update statistics translation_stats['total_translations'] += 1 # Simulate processing for dramatic effect yield "🔍 Analyzing text...", "", "" time.sleep(0.5) # Detect source language with confidence detected_lang, confidence = detect_language_confidence(text) translation_stats['languages_detected'].add(detected_lang) yield f"🧠 Detected language: {detected_lang.upper()} ({confidence:.1%} confidence)", "", "" time.sleep(0.3) # Get language codes try: lang = pycountry.languages.get(alpha_2=detected_lang) src_code = f"{lang.alpha_3}_Latn" if lang else "eng_Latn" except: src_code = "eng_Latn" dest_code = get_FLORES_code_from_language(destination_language) if not dest_code: yield f"❌ Unsupported target language: {destination_language}", "", "" return yield f"⚡ Translating to {destination_language.split(' ', 1)[-1] if ' ' in destination_language else destination_language}...", "", "" time.sleep(0.5) # Handle same language if src_code == dest_code: analytics = f""" 📊 **Translation Analytics** - Source: {detected_lang.upper()} ({confidence:.1%} confidence) - Target: Same language detected - Action: No translation needed - Processing time: <1s """ yield "✅ Translation complete!", text, analytics.strip() return # Perform translation try: start_time = time.time() # Calculate appropriate max_length based on input length input_length = len(text) # Set max_length to be 1.5x input length with a minimum of 512 and maximum of 2048 max_length = max(512, min(2048, int(input_length * 1.5))) translation = text_translator( text, src_lang=src_code, tgt_lang=dest_code, max_length=max_length, do_sample=False, # For more consistent results num_beams=4 # Better quality translation ) processing_time = time.time() - start_time result = translation[0]['translation_text'] # Generate analytics import re clean_dest_lang = re.sub(r'[🇦-🇿]{2}\s*', '', destination_language).strip() analytics = f""" 📊 **Translation Analytics** - **Source Language**: {detected_lang.upper()} ({confidence:.1%} confidence) - **Target Language**: {clean_dest_lang} - **Characters Processed**: {len(text):,} - **Max Length Used**: {max_length} - **Processing Time**: {processing_time:.2f}s - **Session Translations**: {translation_stats['total_translations']} - **Languages Detected**: {len(translation_stats['languages_detected'])} """ yield "✅ Translation complete!", result, analytics.strip() except Exception as e: yield f"❌ Translation failed: {str(e)}", "", "" def clear_all(): """Reset all fields""" return "", "", "", "" # Custom CSS for a modern, sleek interface custom_css = """ .gradio-container { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; } .gr-button { background: linear-gradient(45deg, #FF6B6B, #4ECDC4); border: none; border-radius: 25px; color: white; font-weight: bold; transition: all 0.3s ease; box-shadow: 0 4px 15px rgba(0,0,0,0.2); } .gr-button:hover { transform: translateY(-2px); box-shadow: 0 6px 20px rgba(0,0,0,0.3); } .gr-textbox { border-radius: 15px; border: 2px solid #e0e0e0; transition: all 0.3s ease; } .gr-textbox:focus { border-color: #667eea; box-shadow: 0 0 15px rgba(102, 126, 234, 0.3); } .gr-dropdown { border-radius: 15px; border: 2px solid #e0e0e0; } .gr-panel { background: rgba(255,255,255,0.95); border-radius: 20px; backdrop-filter: blur(10px); box-shadow: 0 8px 32px rgba(0,0,0,0.1); } .gr-form { background: transparent; } .gr-box { border-radius: 15px; background: rgba(255,255,255,0.9); } """ # Create the interface with gr.Blocks(css=custom_css, title="🌍 KS Translation Hub") as demo: gr.HTML("""

🌍 KS Translation Hub

Powered by Advanced Neural Networks • Real-time Language Detection • 15+ Languages

""") with gr.Row(): with gr.Column(scale=1): gr.HTML("

📝 Input

") input_text = gr.Textbox( label="Enter text to translate", placeholder="Type or paste your text here... 🖊️", lines=8, show_label=False ) with gr.Row(): target_lang = gr.Dropdown( choices=[f"{flag} {lang}" for lang, flag in LANGUAGES.items()], label="🎯 Target Language", value="🇪🇸 Spanish", show_label=True ) with gr.Column(scale=1): gr.HTML("

✨ Output

") output_text = gr.Textbox( label="Translation", lines=8, show_label=False, interactive=False ) with gr.Row(): with gr.Column(scale=1): status_text = gr.Textbox( label="🔄 Status", value="Ready to translate...", interactive=False, show_label=True ) with gr.Column(scale=1): analytics_text = gr.Textbox( label="📊 Analytics", value="Translation analytics will appear here...", interactive=False, show_label=True, lines=6 ) with gr.Row(): translate_btn = gr.Button("🚀 Translate", variant="primary", size="lg") clear_btn = gr.Button("🗑️ Clear All", variant="secondary", size="lg") # Event handlers translate_btn.click( fn=translate_with_analytics, inputs=[input_text, target_lang], outputs=[status_text, output_text, analytics_text] ) clear_btn.click( fn=clear_all, outputs=[input_text, output_text, status_text, analytics_text] ) # Auto-translate on Enter key input_text.submit( fn=translate_with_analytics, inputs=[input_text, target_lang], outputs=[status_text, output_text, analytics_text] ) gr.HTML("""

🤖 Powered by Transformers • 🔒 Privacy-First • ⚡ Real-time Processing

""") # Launch with enhanced settings if __name__ == "__main__": demo.launch() #demo.launch( # share=True, # server_name="127.0.0.1", # server_port=7860, #show_tips=True, #enable_queue=True, # max_threads=40 #)