File size: 3,768 Bytes
e75b192
 
 
 
 
 
699dba3
2b1cc8c
 
e75b192
 
 
 
 
 
 
 
 
6513ea1
d6456b2
 
e75b192
2b1cc8c
 
4296f22
2b1cc8c
 
 
101ae6b
27fb94e
d6456b2
aed9962
39f88fd
d6456b2
 
fccbe3c
d6456b2
adb4162
d6456b2
 
2b1cc8c
39f88fd
d6456b2
 
 
 
 
 
 
 
 
 
39f88fd
d6456b2
 
 
 
 
 
 
 
 
 
2b1cc8c
afce631
 
 
 
adb4162
 
2b1cc8c
 
 
 
 
adb4162
2b1cc8c
 
adb4162
 
 
2b1cc8c
 
 
adb4162
 
2b1cc8c
 
 
 
 
 
 
 
 
 
cc443c6
6f43c03
 
d6456b2
adb4162
e75b192
 
 
 
 
 
 
4296f22
e75b192
 
 
 
 
 
 
 
 
adb4162
 
d6456b2
e75b192
 
 
 
 
 
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import gradio as gr
from huggingface_hub import InferenceClient

"""
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co./docs/huggingface_hub/v0.22.2/en/guides/inference
"""
englishToFinnishClient = InferenceClient("Helsinki-NLP/opus-mt-en-fi")
googleClient = InferenceClient("google/flan-t5-large")#"facebook/nllb-200-1.3B")#"facebook/nllb-200-1.3B")
finnishToEnglishClient = InferenceClient("Helsinki-NLP/opus-mt-fi-en")


def respond(
    message,
    history: list[tuple[str, str]],
    system_message,
    max_tokens,
    temperature,
    top_p,
    sourceLanguage,
    targetLanguage,
    useGoogle
):
    toFinnish=targetLanguage=='Finnish' and sourceLanguage=="English"
    fromFinnish=targetLanguage=='English' and sourceLanguage=="Finnish"

    #adminClient=englishToFinnishClient if(toFinnish)else googleClient 

    #candidateClient=finnishToEnglishClient if(fromFinnish)else googleClient 

    print("the text",message, 'the source',sourceLanguage,"the target",targetLanguage)
  
    if(useGoogle=="true"):
        print("using google")
        text="Translate to "f"{targetLanguage}: "f"{message}"
        googleResponse=  googleClient.text_generation(
            text, max_new_tokens=240
        )

        return googleResponse
    
    if(toFinnish):
        print("to finnish")
        fiResponse=  englishToFinnishClient.translation(
            message,
            #max_new_tokens=100
            tgt_lang= targetLanguage,
            src_lang=sourceLanguage  
        )

        return fiResponse.translation_text
    
    if(fromFinnish):
        print("From Finnish called")
        fiResponse=finnishToEnglishClient.translation(
            message,
            tgt_lang= targetLanguage,
            src_lang=sourceLanguage  
        )

        return fiResponse.translation_text


    """ if(toFinnish):
        fiResponse=  englishToFinnishClient.translation(
        message,
        #max_new_tokens=100
        tgt_lang= targetLanguage,
        src_lang=sourceLanguage  
        )

        text="Translate to "f"{targetLanguage}: "f"{message}"
        print("the text",text)
        googResponse=  googleClient.text_generation(
        text
        )

        response=f"{googResponse}<b>Translation 2</b><br/> {fiResponse.translation_text}"
 
    else:    
        text="Translate to "f"{targetLanguage}: "f"{message}"
        print("the text",text)

        googleResponse=  googleClient.text_generation(
          text
        )

        response=f"{googleResponse}"

        if(fromFinnish):
            fiResponse=finnishToEnglishClient.translation(
                message,
                tgt_lang= targetLanguage,
                src_lang=sourceLanguage  
            )

            response+=f'<b>Translation 2</b><br/>{fiResponse.translation_text}'

    print("the response",response)

    response """
        

"""
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
"""
demo = gr.ChatInterface(
    respond,
    additional_inputs=[
        gr.Textbox(value="You are a language translator", label="System message"),
        gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
        gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
        gr.Slider(
            minimum=0.1,
            maximum=1.0,
            value=0.95,
            step=0.05,
            label="Top-p (nucleus sampling)",
        ),
        gr.Textbox(value="Finnish", label="Source Language"),
        gr.Textbox(value="English", label="Target Language"),
        gr.Textbox(value="false", label="Use Google"),
    ],
)


if __name__ == "__main__":
    demo.launch()