File size: 2,742 Bytes
39ea455
 
 
 
 
 
 
 
 
 
 
 
 
8b49c77
39ea455
8b49c77
39ea455
 
 
8b49c77
 
 
 
 
 
 
39ea455
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import gradio as gr
from langchain_community.llms import OpenAI
from langchain.prompts import PromptTemplate
import os
from dotenv import load_dotenv

load_dotenv()

open_api_key = os.getenv('OPENAI_API_KEY')

os.environ["OPENAI_API_KEY"] = open_api_key

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: """

system_prompt_2 = """Please translate the following text field content in english langauge.
                    text: {text}
                    """

def translate_text(file, text_input):
    llm = OpenAI()

    with open(file.name, 'r', encoding='utf-8') as f:
        file_text = f.read()

    template_1 = PromptTemplate(input_variables=["text"], template=system_prompt_1)
    prompt_1 = template_1.format(text=file_text)
    file_translation = llm(prompt_1)

    template_2 = PromptTemplate(input_variables=["text"], template=system_prompt_2)
    prompt_2 = template_2.format(text=file_translation)
    text_translation_op = llm(prompt_2)


    template_3 = PromptTemplate(input_variables=["text"], template=system_prompt_2)
    prompt_3 = template_2.format(text=file_text)
    text_translation_ip = llm(prompt_3)

    output_file_path = "translated_file.txt"
    with open(output_file_path, 'w', encoding='utf-8') as f:
        f.write(file_translation)

    return text_translation_ip, file_translation, text_translation_op, output_file_path

iface = gr.Interface(
    fn=translate_text,
    inputs=[
        gr.File(label="Upload Text File")
 
           ],
    outputs=[
        gr.Textbox(label="transcribes content in english Translated of input content"),
        gr.Textbox(label="transcribes content"),
        gr.Textbox(label="transcribes content in english Translated of output 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 OpenAI with predefined system prompts.",
    allow_flagging="never"

)

iface.launch(debug=True)