englissi's picture
Create app.py
6d11e07 verified
import gradio as gr
from huggingface_hub import InferenceClient
import os
# ๐ŸŽจ ์•„๊ธฐ์ž๊ธฐํ•˜๊ณ  ํŠธ๋ Œ๋””ํ•œ ํ…Œ๋งˆ ์„ค์ •
theme = gr.themes.Soft(primary_hue="indigo", secondary_hue="blue")
# โญ ์—๋Ÿฌ ์—†์ด ๊ฐ€์žฅ ๋˜‘๋˜‘ํ•˜๊ณ  ๋น ๋ฅธ Qwen 2.5 Instruct ๋ชจ๋ธ ์‚ฌ์šฉ
hf_token = os.environ.get("HF_TOKEN")
client = InferenceClient("Qwen/Qwen2.5-7B-Instruct", token=hf_token)
def transform_english(basic_text):
if not basic_text:
return "โš ๏ธ ์˜์ž‘ํ•  ๋ฌธ์žฅ์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.", "", ""
if not hf_token:
return "โŒ HF_TOKEN ์„ค์ • ์˜ค๋ฅ˜", "ํ† ํฐ์„ ํ™•์ธํ•ด์ฃผ์„ธ์š”.", "ํ† ํฐ์„ ํ™•์ธํ•ด์ฃผ์„ธ์š”."
# AI์—๊ฒŒ 3๊ฐ€์ง€ ์ธ๊ฒฉ์„ ๋™์‹œ์— ๋ถ€์—ฌํ•˜๋Š” ๊ฐ•๋ ฅํ•œ ํ”„๋กฌํ”„ํŠธ
prompt = f"""
You are an expert English linguist. Rewrite the following basic English text into three totally different styles.
Basic Text: "{basic_text}"
Task:
1. [Gen Z / TikTok]: Rewrite it using trendy internet slang, Gen-Z vocabulary, emojis, and a highly dramatic, casual tone.
2. [Harvard / Business]: Rewrite it in a highly sophisticated, formal, and elegant academic/business tone (like Harvard Business Review).
3. [Korean CSAT (์ˆ˜๋Šฅ)]: Rewrite it using unnecessarily complex grammatical structures typical of the Korean CSAT reading section. Forcefully include inversions (e.g., Not only did...), participial phrases, and heavy abstract vocabulary.
Format your response EXACTLY like this with no other text:
[TikTok]
(your text here)
[Harvard]
(your text here)
[CSAT]
(your text here)
"""
try:
response = client.chat_completion(
messages=[{"role": "user", "content": prompt}],
max_tokens=800,
temperature=0.8 # ์‚ด์ง ์ฐฝ์˜๋ ฅ์„ ๋†’์—ฌ์„œ ๋” ๊ทน์ ์ธ ๋ณ€ํ™”๋ฅผ ์œ ๋„
)
result = response.choices[0].message.content
# AI์˜ ์‘๋‹ต์„ 3๊ฐœ์˜ ๊ตฌ์—ญ์œผ๋กœ ์˜ˆ์˜๊ฒŒ ์ชผ๊ฐœ๊ธฐ
try:
tiktok = result.split("[TikTok]")[1].split("[Harvard]")[0].strip()
harvard = result.split("[Harvard]")[1].split("[CSAT]")[0].strip()
csat = result.split("[CSAT]")[1].strip()
except Exception:
# AI๊ฐ€ ๊ฐ€๋” ์–‘์‹์„ ์•ˆ ์ง€ํ‚ฌ ๋•Œ๋ฅผ ๋Œ€๋น„ํ•œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ
return result, "๊ฒฐ๊ณผ ํŒŒ์‹ฑ ์—๋Ÿฌ", "๊ฒฐ๊ณผ ํŒŒ์‹ฑ ์—๋Ÿฌ"
return tiktok, harvard, csat
except Exception as e:
return f"โŒ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}", "", ""
# UI ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌ์„ฑ
with gr.Blocks(theme=theme) as demo:
gr.Markdown("# ๐ŸŽญ ์ดˆ๋”ฉ ์˜์ž‘์„ ํ•˜๋ฒ„๋“œ์ƒ์œผ๋กœ! ๋‹ค์ค‘์ธ๊ฒฉ ๊ต์ •๊ธฐ")
gr.Markdown("### ์ฝฉ๊ธ€๋ฆฌ์‹œ๋‚˜ ์•„์ฃผ ๋‹จ์ˆœํ•œ ๋ฌธ์žฅ์„ ์ž…๋ ฅํ•˜๋ฉด, AI๊ฐ€ 3๊ฐ€์ง€ ์ธ๊ฒฉ์œผ๋กœ ์™„๋ฒฝํ•˜๊ฒŒ ํฌ์žฅํ•ด๋“œ๋ฆฝ๋‹ˆ๋‹ค.")
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("### โœ๏ธ 1. ๋‚˜์˜ ๊ธฐ์ดˆ ์˜์ž‘ ์ž…๋ ฅ")
user_input = gr.Textbox(
label="์•„์ฃผ ๋‹จ์ˆœํ•œ ์˜์–ด๋กœ ์ ์–ด๋ณด์„ธ์š”",
placeholder="์˜ˆ: I go to PC room and play game. It was fun.",
lines=5
)
gr.Examples(
examples=[
"I go to PC room and play game. It was fun.",
"I ate a very big hamburger. My stomach is full.",
"I sleep during math class because the teacher is boring."
],
inputs=user_input,
label="๐ŸŒŸ ๊ฟ€์žผ ์ฝฉ๊ธ€๋ฆฌ์‹œ ์˜ˆ์‹œ (ํด๋ฆญ!)"
)
run_btn = gr.Button("โœจ ๋‹ค์ค‘์ธ๊ฒฉ ๋ณ€ํ™˜ ์‹œ์ž‘! โœจ", variant="primary")
with gr.Column(scale=2):
gr.Markdown("### ๐Ÿช„ 2. AI์˜ 3๋‹จ ๋ณ€์‹  ๊ฒฐ๊ณผ")
with gr.Row():
out_tiktok = gr.Textbox(label="๐Ÿ“ฑ ์›์–ด๋ฏผ 10๋Œ€ ์Šฌ๋žญ (ํ‹ฑํ†ก/์ธ์Šคํƒ€ ๊ฐ์„ฑ)", lines=6, interactive=False)
out_harvard = gr.Textbox(label="๐Ÿ‘” ๋น„์ฆˆ๋‹ˆ์Šค / ํ•˜๋ฒ„๋“œ (๊ณ ๊ธ‰ ํ•™์ˆ  ํ†ค)", lines=6, interactive=False)
out_csat = gr.Textbox(label="๐Ÿ˜ˆ ์ˆ˜๋Šฅ 29๋ฒˆ ์–ด๋ฒ• (๋ณต์žกํ•œ ๋„์น˜/๋ถ„์‚ฌ๊ตฌ๋ฌธ)", lines=6, interactive=False)
run_btn.click(
transform_english,
inputs=[user_input],
outputs=[out_tiktok, out_harvard, out_csat]
)
if __name__ == "__main__":
demo.launch()