import os import json import gradio as gr import requests LANGUAGES = ['Bengali', 'Hindi', 'Tamil', 'English', 'German', 'Telugu', 'French', 'Spanish', 'Kannada', 'Dutch'] def get_alternate_text(text, language): ENDPOINT = "https://api.openai.com/v1/chat/completions" headers = { "Authorization": f"Bearer {os.environ.get('OPENAI_API_KEY')}", "Content-Type": "application/json", } 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." prompt = f"""Text: --- {text} --- """ payload = { "model": "gpt-3.5-turbo", "messages": [ {"role": "system", "content": system_prompt}, {"role": "user", "content": prompt} ], "temperature": 0.4 } response = requests.post(ENDPOINT, data=json.dumps(payload), headers=headers) text = None while text is None: try: text = response.json().get('choices')[0]['message']['content'].strip("\n").replace('"', "") except Exception as e: print(e) response = requests.post(ENDPOINT, data=json.dumps(payload), headers=headers) continue return text language = gr.Dropdown(LANGUAGES, value="Spanish", label="Language") text = gr.Textbox(lines=4, label="Text") interface = gr.Interface( get_alternate_text, [text, language], 'text', title="Deepsync Text Rephraser" ) if __name__=="__main__": interface.launch( auth=(os.environ.get('AUTH_USERNAME'), os.environ.get('AUTH_PASSWORD')) )