Spaces:
Sleeping
Sleeping
File size: 2,490 Bytes
32a7553 249a64a 32a7553 7c5fedb 249a64a 32a7553 249a64a 7c5fedb 249a64a 7c5fedb 249a64a 7c5fedb 249a64a 7c5fedb e5df068 7c5fedb 249a64a 7c5fedb e5df068 7c5fedb e5df068 249a64a 7c5fedb 249a64a 32a7553 769b8f7 249a64a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
import gradio as gr
import os
import openai
# OpenAI API ν€λ₯Ό μ€μ ν©λλ€.
openai.api_key = os.getenv("OPENAI_API_KEY")
def translate_code_to_korean(code):
"""
μ
λ ₯λ μμ΄ μ½λλ₯Ό μμ°μ€λ½κ³ μ νν νκ΅μ΄λ‘ λ²μν©λλ€.
"""
system_message = (
"λΉμ μ νλ‘κ·Έλλ° κ΄λ ¨ μμ΄ ν
μ€νΈλ₯Ό νκ΅μ΄λ‘ λ²μνλ AIμ
λλ€. "
"λ²μμ λ¬Έλ²μ μΌλ‘ μ ννκ³ μμ°μ€λ¬μ΄ νλ¦μ μ°μ μΌλ‘ νλ©°, κΈ°μ μ μλ―Έλ₯Ό λͺ
ννκ² μ λ¬ν΄μΌ ν©λλ€. "
"- νΌλν λμ λ₯λν λ¬Έμ₯μ μ¬μ©ν©λλ€. \n"
"- λλͺ
μ¬λ₯Ό μ΅μννκ³ , λμ¬μ νμ©μ¬λ₯Ό μ°μ μ μΌλ‘ μ¬μ©ν©λλ€. \n"
"- νμ¬μ§ννλ³΄λ€ λ¨μ νμ¬λ μλ£νμ μ¬μ©ν©λλ€. \n"
"- μ£Όμ μ μν©μ λ§λ μ΄νλ₯Ό μ ννλ©°, μ§μκ³Ό μμ μ¬μ΄μ κ· νμ μ μ§ν©λλ€. \n"
"- λ²μλ ν
μ€νΈλ AIκ° μμ±ν λλμ μ£Όμ§ μμμΌ ν©λλ€."
)
try:
# OpenAI APIλ₯Ό νΈμΆν©λλ€.
response = openai.ChatCompletion.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": system_message},
{"role": "user", "content": code},
],
max_tokens=1500,
temperature=0.7,
top_p=0.9,
)
return response.choices[0].message['content']
except Exception as e:
return f"μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}"
# Gradio μΈν°νμ΄μ€λ₯Ό ꡬμ±ν©λλ€.
def interface():
with gr.Blocks() as app:
gr.Markdown("""
# μ½λ λ²μκΈ°
μμ΄λ‘ μμ±λ μ½λλ₯Ό μ
λ ₯νλ©΄ μμ°μ€λ½κ³ μ νν νκ΅μ΄λ‘ λ²μν΄ λ립λλ€.
""")
with gr.Row():
input_code = gr.Textbox(
label="λ²μν μμ΄ μ½λ",
lines=10,
placeholder="μ¬κΈ°μ μ½λλ₯Ό μ
λ ₯νμΈμ."
)
output_translation = gr.Textbox(
label="λ²μ κ²°κ³Ό",
lines=10,
interactive=False
)
translate_button = gr.Button("λ²μ μ€ν")
translate_button.click(
translate_code_to_korean,
inputs=[input_code],
outputs=[output_translation]
)
return app
if __name__ == "__main__":
app = interface()
app.launch()
|