import gradio as gr import subprocess import openapi import time import os def translate(text_input, openapi_key): openapi.api_key = openapi_key text_list = text_input.split('\n')[12:] reply = [] for i in range(0,len(text_list)+9,10): content = """What do these sentences about Hugging Face Transformers (a machine learning library) mean in Korean? Please do not translate the word after a πŸ€— emoji as it is a product name. Please ignore the video and image and translate only the sentences I provided. Ignore the contents of the iframe tag. ```md %s"""%'\n'.join(text_list[i:i+10]) chat = openai.ChatCompletion.create( model = "gpt-3.5-turbo-0301", messages=[ {"role": "system", "content": content},]) print("질문") print(content) print("응닡") print(chat.choices[0].message.content) reply.append(chat.choices[0].message.content) time.sleep(20) return reply inputs = [ gr.inputs.Textbox(lines=2, label="Input Open API Key"), gr.inputs.File(label="Upload MDX File") ] outputs = gr.outputs.Textbox(label="Translation") def translate_with_upload(text, file): openapi_key = text if file is not None: # μƒˆλ‘œμš΄ 파일 이름을 μƒμ„±ν•©λ‹ˆλ‹€. new_filename = f"text_{file.name}" # sed λͺ…λ Ήμ–΄λ₯Ό μ‚¬μš©ν•˜μ—¬ νŒŒμΌμ„ λ³€ν™˜ν•©λ‹ˆλ‹€. subprocess.run(f"sed '/```/,/```/d' {file.name} | sed '/^|.*|$/d' | sed '/^$/N;/^\n$/D' > {new_filename}", shell=True) # μƒˆλ‘œμš΄ νŒŒμΌμ„ μ—΄μ–΄μ„œ λ‚΄μš©μ„ μ½μ–΄μ˜΅λ‹ˆλ‹€. with open(new_filename, 'r') as f: text_input = f.read() else: # 파일이 μ—…λ‘œλ“œλ˜μ§€ μ•Šμ€ 경우, 빈 λ¬Έμžμ—΄λ‘œ μ΄ˆκΈ°ν™”ν•©λ‹ˆλ‹€. text_input = "" return translate(text_input, openapi_key) prompt_translate = gr.Interface( fn=translate_with_upload, inputs=inputs, outputs=outputs, title="ChatGPT Korean Prompt Translation", description="Translate your text into Korean using the GPT-3 model.", theme="compact" ) prompt_translate.launch()