Download app.py from Sangwon78/example9: direct link, hf CLI and curl.
- Browser
- Download file 1.75 kB
-
https://huggingface.co/spaces/Sangwon78/example9/resolve/main/app.py
- Command line
-
hf download hf://spaces/Sangwon78/example9/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Sangwon78/example9/resolve/main/app.py
1.75 kB
| import os | |
| import openai | |
| import gradio as gr | |
| # Hugging Face Spaces์์ ์ ์ฅํด๋ Secrets(ํ๊ฒฝ๋ณ์) ๋ถ๋ฌ์ค๊ธฐ | |
| openai.api_key = os.getenv("OPENAI_API_KEY") | |
| def translate_to_english(korean_text): | |
| """ | |
| ChatGPT API๋ฅผ ์ฌ์ฉํ์ฌ ํ๊ตญ์ด๋ฅผ ์์ด๋ก ๋ฒ์ญํ๋ ํจ์ | |
| """ | |
| if not korean_text.strip(): | |
| return "๋ฒ์ญํ ๋ฌธ์ฅ์ ์ ๋ ฅํด์ฃผ์ธ์." | |
| try: | |
| response = openai.ChatCompletion.create( | |
| model="gpt-3.5-turbo", | |
| messages=[ | |
| {"role": "system", "content": "You are a professional translator who translates Korean into natural English."}, | |
| {"role": "user", "content": korean_text}, | |
| ], | |
| temperature=0.2, | |
| max_tokens=200 | |
| ) | |
| # API ์๋ต์์ ๋ฒ์ญ๋ ํ ์คํธ ์ถ์ถ | |
| english_text = response["choices"][0]["message"]["content"].strip() | |
| return english_text | |
| except Exception as e: | |
| return f"์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}" | |
| def main(): | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## ํ๊ตญ์ด โ ์์ด ๋ฒ์ญ๊ธฐ (ChatGPT API ํ์ฉ)") | |
| with gr.Row(): | |
| with gr.Column(): | |
| input_text = gr.Textbox(label="ํ๊ตญ์ด ์ ๋ ฅ", placeholder="ํ๊ตญ์ด ๋ฌธ์ฅ์ ์ ๋ ฅํ์ธ์") | |
| with gr.Column(): | |
| output_text = gr.Textbox(label="์์ด ๋ฒ์ญ ๊ฒฐ๊ณผ") | |
| translate_button = gr.Button("๋ฒ์ญํ๊ธฐ") | |
| translate_button.click( | |
| fn=translate_to_english, | |
| inputs=input_text, | |
| outputs=output_text | |
| ) | |
| return demo | |
| if __name__ == "__main__": | |
| demo = main() | |
| # ssr ์ต์ ์ ๊ฑฐ | |
| demo.launch(share=True) | |