Initial comit.
Browse files
app.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import openai
|
3 |
+
from dotenv import dotenv_values
|
4 |
+
from langchain.llms import OpenAI
|
5 |
+
from langchain.chat_models import ChatOpenAI
|
6 |
+
from langchain import PromptTemplate
|
7 |
+
import gradio as gr
|
8 |
+
import random
|
9 |
+
import string
|
10 |
+
|
11 |
+
|
12 |
+
|
13 |
+
env_values = dotenv_values("./app.env")
|
14 |
+
openai_api_key = env_values['OPEN_API_KEY']
|
15 |
+
|
16 |
+
llm = OpenAI(openai_api_key= openai_api_key, model_name="gpt-3.5-turbo", temperature= 0.0)
|
17 |
+
|
18 |
+
template = """Translate the text. You are a very professional translator. Translate the given sentence into {target}\
|
19 |
+
|
20 |
+
Text: {query}
|
21 |
+
|
22 |
+
Translated text: """
|
23 |
+
|
24 |
+
prompt_template = PromptTemplate(
|
25 |
+
input_variables=["target", "query"],
|
26 |
+
template=template
|
27 |
+
)
|
28 |
+
|
29 |
+
|
30 |
+
def random_punctuation(text):
|
31 |
+
# punctuation = "#$%&*+-<=>@^_~"
|
32 |
+
punctuation = "_"
|
33 |
+
new_text = ""
|
34 |
+
for word in text.split():
|
35 |
+
if (len(word) > 3) or (word == 'غزة') or (word == 'غزه'):
|
36 |
+
result = ""
|
37 |
+
middle = len(word) // 2
|
38 |
+
middel_of_text = word[:middle] + random.choice(punctuation) + word[middle:]
|
39 |
+
# result = random.choice(punctuation) + middel_of_text + random.choice(punctuation)
|
40 |
+
result = '*' + middel_of_text + "*"
|
41 |
+
new_text += result + " "
|
42 |
+
else:
|
43 |
+
new_text += word + " "
|
44 |
+
return new_text.strip()
|
45 |
+
|
46 |
+
def MT(query, target):
|
47 |
+
translated_text = llm(prompt_template.format(target = target, query=query))
|
48 |
+
return translated_text
|
49 |
+
|
50 |
+
def gradio_func(query, target, style):
|
51 |
+
if len(query) > 400:
|
52 |
+
return "Please make your text shorter than upove | الرجاء تصغير النص للترجمه"
|
53 |
+
translated_text = MT(query, target)
|
54 |
+
if style == "Translate | الترجمه":
|
55 |
+
return translated_text
|
56 |
+
elif style == "Change the text | تغيير النص":
|
57 |
+
return random_punctuation(text)
|
58 |
+
else:
|
59 |
+
return random_punctuation(translated_text)
|
60 |
+
|
61 |
+
|
62 |
+
|
63 |
+
gr.close_all()
|
64 |
+
demo = gr.Interface(fn=gradio_func,
|
65 |
+
inputs=[
|
66 |
+
gr.Textbox(label="Your Text | النص الخاص بك", lines= 4),
|
67 |
+
|
68 |
+
gr.Radio(["Arabic", "English", "Mandarin Chinese", "Spanish", "Hindi", "Bengali", "Portuguese", "Russian", "Japanese", "French"],
|
69 |
+
label="Languages | اللغات",
|
70 |
+
info= "Which language you want to translate? | ما هي اللغه التي تود الترجمه إليها؟"),
|
71 |
+
|
72 |
+
gr.Radio(["Translate | الترجمه",
|
73 |
+
"Change the text | تغيير النص",
|
74 |
+
"Translate and change the text | الترجمه و تغير النص معاً"],
|
75 |
+
label="What you want? | ماذا تريد؟")
|
76 |
+
],
|
77 |
+
outputs=[
|
78 |
+
gr.Textbox(label="Generated Text", lines=4)
|
79 |
+
|
80 |
+
],
|
81 |
+
title="Text Translation And Text Formatter For Palestinian Case, Support Palestine 🇵🇸.",
|
82 |
+
description="## Flag, for any errorneous output.",
|
83 |
+
)
|
84 |
+
demo.launch(share=True)
|
env.env
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
OPEN_API_KEY = "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"
|