Abdo1Kamr commited on
Commit
cc6fcce
1 Parent(s): eff8a17

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -86
app.py DELETED
@@ -1,86 +0,0 @@
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
- openai_api_key = "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"
16
-
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)