| import os |
| import gradio as gr |
| import openai |
|
|
| |
| openai.api_key = os.getenv("OPENAI_API_KEY") |
|
|
| |
| def call_api(content, system_message, max_tokens=100, temperature=0.7, top_p=1.0): |
| response = openai.ChatCompletion.create( |
| model="gpt-4", |
| messages=[ |
| {"role": "system", "content": system_message}, |
| {"role": "user", "content": content}, |
| ], |
| max_tokens=max_tokens, |
| temperature=temperature, |
| top_p=top_p, |
| ) |
| return response.choices[0].message['content'] |
|
|
| |
| def translate_text(input_text): |
| |
| if any('κ°' <= char <= 'ν£' for char in input_text): |
| system_message = "Translate the following Korean text to English. Output only the translated text, without any additional context or greetings." |
| else: |
| system_message = "Translate the following English text to Korean. Output only the translated text, without any additional context or greetings." |
|
|
| try: |
| result = call_api(content=input_text, system_message=system_message) |
| return result.strip() |
| except Exception as e: |
| return f"μ€λ₯ λ°μ: {str(e)}" |
|
|
| |
| def gradio_interface(input_text): |
| translated_text = translate_text(input_text) |
| return translated_text |
|
|
| |
| iface = gr.Interface( |
| fn=gradio_interface, |
| inputs=gr.Textbox(label="ν
μ€νΈ μ
λ ₯", lines=2, placeholder="λ²μν ν
μ€νΈλ₯Ό μ
λ ₯νμΈμ."), |
| outputs=gr.Textbox(label="λ²μλ ν
μ€νΈ", lines=2), |
| title="μΈμ΄ λ²μκΈ°", |
| description="gpt-4 λͺ¨λΈμ νμ©νμ¬ μ
λ ₯λ ν
μ€νΈλ₯Ό νκ΅μ΄ λλ μμ΄λ‘ λ²μν©λλ€. λ²μλ ν
μ€νΈλ§ μΆλ ₯λ©λλ€.", |
| ) |
|
|
| if __name__ == "__main__": |
| iface.launch() |
|
|