Spaces:
Sleeping
Sleeping
import gradio as gr | |
from langchain_community.llms import OpenAI | |
from langchain.prompts import PromptTemplate | |
import os | |
from dotenv import load_dotenv | |
from langchain_huggingface import HuggingFaceEndpoint | |
load_dotenv() | |
system_prompt_1 = """ | |
You are an advanced AI assistant tasked with helping to transcribes given texts into | |
simplified languages, specifically FALC (Facile à Lire et à Comprendre) and "Leichte Sprache" (Simple Language). | |
This system is intended to streamline the creation of accessible content for government websites. | |
Instructions for AI Development: | |
detect the language of text given then transcribes text into the same language which the guidelines of | |
FALC (Facile à Lire et à Comprendre) and "Leichte Sprache" (Simple Language) and | |
accurately transcribe complex texts into simplified language. | |
Ensure maintaining the context and meaning of the original text while simplifying its language. | |
text: {text} | |
transcribes text: """ | |
def translate_text(file, text_input): | |
repo_id = "mistralai/Mistral-7B-Instruct-v0.2" | |
llm = HuggingFaceEndpoint( | |
repo_id=repo_id, | |
max_length=128, | |
temperature=0.5, | |
huggingfacehub_api_token=api_token, | |
) | |
with open(file.name, 'r', encoding='utf-8') as f: | |
file_text = f.read() | |
prompt = PromptTemplate.from_template(system_prompt_1) | |
llm_chain = prompt | llm | |
file_translation = llm_chain.invoke({"text": file_text}) | |
output_file_path = "translated_file.txt" | |
with open(output_file_path, 'w', encoding='utf-8') as f: | |
f.write(file_translation) | |
return file_translation, output_file_path | |
iface = gr.Interface( | |
fn=translate_text, | |
inputs=[ | |
gr.File(label="Upload Text File") | |
], | |
outputs=[ | |
gr.Textbox(label="transcribes content"), | |
gr.File(label="Download Translated File Text") | |
], | |
title="Text Transcribes", | |
description="Upload a text file and provide a text input to translate the text using LangChain and Mistral-7B-Instruct-v0.2 model with predefined system prompts.", | |
allow_flagging="never" | |
) | |
iface.launch(debug=True) | |