E_K / app.py
unnastyle's picture
Update app.py
b611d43 verified
import os
import gradio as gr
import openai
# OpenAI API ν‚€ μ„€μ •
openai.api_key = os.getenv("OPENAI_API_KEY")
# OpenAI API 호좜 ν•¨μˆ˜
def call_api(content, system_message, max_tokens=100, temperature=0.7, top_p=1.0):
response = openai.ChatCompletion.create(
model="gpt-4", # λͺ¨λΈ λ³€κ²½: 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)}"
# Gradio μΈν„°νŽ˜μ΄μŠ€ μ„€μ •
def gradio_interface(input_text):
translated_text = translate_text(input_text)
return translated_text
# Gradio μ•± 생성
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()