Upload 2 files
Browse files- app.py +86 -0
- requirements.txt +75 -0
app.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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("./env.env")
|
| 14 |
+
openai_api_key = env_values['OPEN_API_KEY']
|
| 15 |
+
|
| 16 |
+
# openai_api_key = "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"
|
| 17 |
+
|
| 18 |
+
llm = OpenAI(openai_api_key= openai_api_key, model_name="gpt-3.5-turbo", temperature= 0.0)
|
| 19 |
+
|
| 20 |
+
template = """Translate the text. You are a very professional translator. Translate the given sentence into {target}\
|
| 21 |
+
|
| 22 |
+
Text: {query}
|
| 23 |
+
|
| 24 |
+
Translated text: """
|
| 25 |
+
|
| 26 |
+
prompt_template = PromptTemplate(
|
| 27 |
+
input_variables=["target", "query"],
|
| 28 |
+
template=template
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def random_punctuation(text):
|
| 33 |
+
# punctuation = "#$%&*+-<=>@^_~"
|
| 34 |
+
punctuation = "_"
|
| 35 |
+
new_text = ""
|
| 36 |
+
for word in text.split():
|
| 37 |
+
if (len(word) > 3) or (word == 'غزة') or (word == 'غزه'):
|
| 38 |
+
result = ""
|
| 39 |
+
middle = len(word) // 2
|
| 40 |
+
middel_of_text = word[:middle] + random.choice(punctuation) + word[middle:]
|
| 41 |
+
# result = random.choice(punctuation) + middel_of_text + random.choice(punctuation)
|
| 42 |
+
result = '*' + middel_of_text + "*"
|
| 43 |
+
new_text += result + " "
|
| 44 |
+
else:
|
| 45 |
+
new_text += word + " "
|
| 46 |
+
return new_text.strip()
|
| 47 |
+
|
| 48 |
+
def MT(query, target):
|
| 49 |
+
translated_text = llm(prompt_template.format(target = target, query=query))
|
| 50 |
+
return translated_text
|
| 51 |
+
|
| 52 |
+
def gradio_func(query, target, style):
|
| 53 |
+
if len(query) > 400:
|
| 54 |
+
return "Please make your text shorter than upove | الرجاء تصغير النص للترجمه"
|
| 55 |
+
translated_text = MT(query, target)
|
| 56 |
+
if style == "Translate | الترجمه":
|
| 57 |
+
return translated_text
|
| 58 |
+
elif style == "Change the text | تغيير النص":
|
| 59 |
+
return random_punctuation(text)
|
| 60 |
+
else:
|
| 61 |
+
return random_punctuation(translated_text)
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
gr.close_all()
|
| 66 |
+
demo = gr.Interface(fn=gradio_func,
|
| 67 |
+
inputs=[
|
| 68 |
+
gr.Textbox(label="Your Text | النص الخاص بك", lines= 4),
|
| 69 |
+
|
| 70 |
+
gr.Radio(["Arabic", "English", "Mandarin Chinese", "Spanish", "Hindi", "Bengali", "Portuguese", "Russian", "Japanese", "French"],
|
| 71 |
+
label="Languages | اللغات",
|
| 72 |
+
info= "Which language you want to translate? | ما هي اللغه التي تود الترجمه إليها؟"),
|
| 73 |
+
|
| 74 |
+
gr.Radio(["Translate | الترجمه",
|
| 75 |
+
"Change the text | تغيير النص",
|
| 76 |
+
"Translate and change the text | الترجمه و تغير النص معاً"],
|
| 77 |
+
label="What you want? | ماذا تريد؟")
|
| 78 |
+
],
|
| 79 |
+
outputs=[
|
| 80 |
+
gr.Textbox(label="Generated Text", lines=4)
|
| 81 |
+
|
| 82 |
+
],
|
| 83 |
+
title="Text Translation And Text Formatter For Palestinian Case, Support Palestine 🇵🇸.",
|
| 84 |
+
description="## Flag, for any errorneous output.",
|
| 85 |
+
)
|
| 86 |
+
demo.launch(share=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
aiofiles==23.2.1
|
| 2 |
+
aiohttp==3.8.6
|
| 3 |
+
aiosignal==1.3.1
|
| 4 |
+
altair==5.1.2
|
| 5 |
+
annotated-types==0.6.0
|
| 6 |
+
anyio==3.7.1
|
| 7 |
+
async-timeout==4.0.3
|
| 8 |
+
attrs==23.1.0
|
| 9 |
+
certifi==2023.7.22
|
| 10 |
+
charset-normalizer==3.3.1
|
| 11 |
+
click==8.1.7
|
| 12 |
+
contourpy==1.1.1
|
| 13 |
+
cycler==0.12.1
|
| 14 |
+
dataclasses-json==0.6.1
|
| 15 |
+
exceptiongroup==1.1.3
|
| 16 |
+
fastapi==0.104.0
|
| 17 |
+
ffmpy==0.3.1
|
| 18 |
+
filelock==3.13.0
|
| 19 |
+
fonttools==4.43.1
|
| 20 |
+
frozenlist==1.4.0
|
| 21 |
+
fsspec==2023.10.0
|
| 22 |
+
gradio==3.50.2
|
| 23 |
+
gradio_client==0.6.1
|
| 24 |
+
h11==0.14.0
|
| 25 |
+
httpcore==0.18.0
|
| 26 |
+
httpx==0.25.0
|
| 27 |
+
huggingface-hub==0.18.0
|
| 28 |
+
idna==3.4
|
| 29 |
+
importlib-resources==6.1.0
|
| 30 |
+
Jinja2==3.1.2
|
| 31 |
+
jsonpatch==1.33
|
| 32 |
+
jsonpointer==2.4
|
| 33 |
+
jsonschema==4.19.1
|
| 34 |
+
jsonschema-specifications==2023.7.1
|
| 35 |
+
kiwisolver==1.4.5
|
| 36 |
+
langchain==0.0.325
|
| 37 |
+
langsmith==0.0.53
|
| 38 |
+
MarkupSafe==2.1.3
|
| 39 |
+
marshmallow==3.20.1
|
| 40 |
+
matplotlib==3.8.0
|
| 41 |
+
multidict==6.0.4
|
| 42 |
+
mypy-extensions==1.0.0
|
| 43 |
+
numpy==1.26.1
|
| 44 |
+
openai==0.28.1
|
| 45 |
+
orjson==3.9.10
|
| 46 |
+
packaging==23.2
|
| 47 |
+
pandas==2.1.2
|
| 48 |
+
Pillow==10.1.0
|
| 49 |
+
pydantic==2.4.2
|
| 50 |
+
pydantic_core==2.10.1
|
| 51 |
+
pydub==0.25.1
|
| 52 |
+
pyparsing==3.1.1
|
| 53 |
+
python-dateutil==2.8.2
|
| 54 |
+
python-dotenv==1.0.0
|
| 55 |
+
python-multipart==0.0.6
|
| 56 |
+
pytz==2023.3.post1
|
| 57 |
+
PyYAML==6.0.1
|
| 58 |
+
referencing==0.30.2
|
| 59 |
+
requests==2.31.0
|
| 60 |
+
rpds-py==0.10.6
|
| 61 |
+
semantic-version==2.10.0
|
| 62 |
+
six==1.16.0
|
| 63 |
+
sniffio==1.3.0
|
| 64 |
+
SQLAlchemy==2.0.22
|
| 65 |
+
starlette==0.27.0
|
| 66 |
+
tenacity==8.2.3
|
| 67 |
+
toolz==0.12.0
|
| 68 |
+
tqdm==4.66.1
|
| 69 |
+
typing-inspect==0.9.0
|
| 70 |
+
typing_extensions==4.8.0
|
| 71 |
+
tzdata==2023.3
|
| 72 |
+
urllib3==2.0.7
|
| 73 |
+
uvicorn==0.23.2
|
| 74 |
+
websockets==11.0.3
|
| 75 |
+
yarl==1.9.2
|