import torch import gradio as gr import json from transformers import pipeline, AutoTokenizer print("Loading Summarization model: t5-base...") text_summary = pipeline( "summarization", model="t5-base", torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32 ) print("Summarization model loaded.") def summarize_text(input_text): output = text_summary(input_text, min_length=30, max_length=120) return output[0]['summary_text'] print("Loading Translation model: facebook/nllb-200-distilled-600M...") text_translator = pipeline( "translation", model="facebook/nllb-200-distilled-600M", torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32 ) print("Translation model loaded.") with open('language.json', 'r', encoding='utf-8') as file: language_data = json.load(file) def get_FLORES_code_from_language(language_name): for entry in language_data: if entry['Language'].lower() == language_name.lower(): return entry['FLORES-200 code'] return None def translate_text(text, destination_language_name): dest_code = get_FLORES_code_from_language(destination_language_name) if not dest_code: return f"Error: FLORES-200 code not found for '{destination_language_name}'. Please select a language from the list." print(f"Translating from English (eng_Latn) to {destination_language_name} ({dest_code})...") translation = text_translator( text, src_lang="eng_Latn", tgt_lang=dest_code ) return translation[0]["translation_text"] print("Loading Question-Answering model: distilbert-base-uncased-distilled-squad...") Youtube = pipeline( "question-answering", model="distilbert-base-uncased-distilled-squad", torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32 ) print("Question-Answering model loaded.") def read_file_content(file_obj): try: with open(file_obj.name, 'r', encoding='utf-8') as file: return file.read() except UnicodeDecodeError: try: with open(file_obj.name, 'r', encoding='latin-1') as file: return file.read() except Exception as e: return f"An error occurred: {e}" except Exception as e: return f"An error occurred: {e}" def get_answer(file, question): context = read_file_content(file) if "An error occurred" in context: return context answer = Youtube(question=question, context=context) if answer['score'] < 0.2: return "I am not confident I can answer this question based on the provided text." return answer["answer"] def landing_page(): return ( "
"
"Summarize, translate, or ask questions on documents using a modern, friendly interface.
"
"Select a tool below to get started."
"