Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import json
|
3 |
+
import gradio as gr
|
4 |
+
import requests
|
5 |
+
|
6 |
+
|
7 |
+
LANGUAGES = ['Bengali', 'Hindi', 'Tamil', 'English', 'German', 'Telugu', 'French', 'Spanish', 'Kannada', 'Dutch']
|
8 |
+
|
9 |
+
def get_alternate_text(text, language):
|
10 |
+
ENDPOINT = "https://api.openai.com/v1/chat/completions"
|
11 |
+
headers = {
|
12 |
+
"Authorization": f"Bearer {os.environ.get('OPENAI_API_KEY')}",
|
13 |
+
"Content-Type": "application/json",
|
14 |
+
}
|
15 |
+
system_prompt = f"Please rephrase this text in {language} in a manner that it is natural sounding, very concise but without any key information loss. Give 3 such texts separated by new line."
|
16 |
+
prompt = f"""Text:
|
17 |
+
---
|
18 |
+
{text}
|
19 |
+
---
|
20 |
+
"""
|
21 |
+
payload = {
|
22 |
+
"model": "gpt-3.5-turbo",
|
23 |
+
"messages": [
|
24 |
+
{"role": "system", "content": system_prompt},
|
25 |
+
{"role": "user", "content": prompt}
|
26 |
+
],
|
27 |
+
"temperature": 0.4
|
28 |
+
}
|
29 |
+
response = requests.post(ENDPOINT, data=json.dumps(payload), headers=headers)
|
30 |
+
text = None
|
31 |
+
while text is None:
|
32 |
+
try:
|
33 |
+
text = response.json().get('choices')[0]['message']['content'].strip("\n").replace('"', "")
|
34 |
+
except Exception as e:
|
35 |
+
print(e)
|
36 |
+
response = requests.post(ENDPOINT, data=json.dumps(payload), headers=headers)
|
37 |
+
continue
|
38 |
+
return text
|
39 |
+
|
40 |
+
language = gr.Dropdown(LANGUAGES, value="Spanish", label="Language")
|
41 |
+
text = gr.Textbox(lines=4, label="Text")
|
42 |
+
|
43 |
+
interface = gr.Interface(
|
44 |
+
get_alternate_text,
|
45 |
+
[text, language],
|
46 |
+
'text',
|
47 |
+
title="Deepsync Text Rephraser"
|
48 |
+
)
|
49 |
+
|
50 |
+
if __name__=="__main__":
|
51 |
+
interface.launch(
|
52 |
+
auth=(os.environ.get('AUTH_USERNAME'), os.environ.get('AUTH_PASSWORD'))
|
53 |
+
)
|