Download app.py from Corin1998/1: direct link, hf CLI and curl.
- Browser
- Download file 2.96 kB
-
https://huggingface.co/spaces/Corin1998/1/resolve/main/app.py
- Command line
-
hf download hf://spaces/Corin1998/1/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Corin1998/1/resolve/main/app.py
2.96 kB
| import os | |
| import json | |
| import gradio as gr | |
| from openai import OpenAI | |
| # ---- APIキー(SecretsにOPENAI_API_KEYをセット)---- | |
| OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") | |
| if not OPENAI_API_KEY: | |
| raise RuntimeError("OPENAI_API_KEY is not set. Add it in Settings → Variables and secrets.") | |
| client = OpenAI(api_key=OPENAI_API_KEY) | |
| MODEL = "gpt-4o" # 必要に応じて gpt-4o-mini などに変更 | |
| # ---- iOS等のノッチ/ホームバーに合わせて安全領域を確保(ここが追加)---- | |
| SAFEAREA_CSS = """ | |
| /* iOS/Androidのノッチ・ホームバー分の余白を動的に確保 */ | |
| html, body, #root, .gradio-container { | |
| padding-top: env(safe-area-inset-top); | |
| padding-bottom: env(safe-area-inset-bottom); | |
| padding-left: env(safe-area-inset-left); | |
| padding-right: env(safe-area-inset-right); | |
| /* 画面いっぱいに広げる */ | |
| min-height: 100dvh; | |
| box-sizing: border-box; | |
| } | |
| """ | |
| def summarize(text: str, length: str = "short") -> str: | |
| system = ( | |
| "あなたは日英の要約アシスタント。" | |
| "重要点の抽出、冗長排除、事実の改変の禁止。出力は日本語で。" | |
| f"長さの目安:{length}" | |
| ) | |
| resp = client.chat.completions.create( | |
| model=MODEL, | |
| messages=[ | |
| {"role": "system", "content": system}, | |
| {"role": "user", "content": text} | |
| ] | |
| ) | |
| return resp.choices[0].message.content.strip() | |
| def translate(text: str, target_lang: str = "en", formality: str = "neutral") -> str: | |
| system = "あなたは一流の翻訳家。意味忠実・自然な文体・用語統一・固有名詞も丁寧に。" | |
| user = f"次の文を{target_lang}に翻訳。文体:{formality}\n\n{text}" | |
| resp = client.chat.completions.create( | |
| model=MODEL, | |
| messages=[ | |
| {"role": "system", "content": system}, | |
| {"role": "user", "content": user} | |
| ] | |
| ) | |
| return resp.choices[0].message.content.strip() | |
| # ---- ここで css=SAFEAREA_CSS を渡す(ここが追加)---- | |
| with gr.Blocks(title="Summarize & Translate", css=SAFEAREA_CSS) as demo: | |
| gr.Markdown("# Summarize & Translate") | |
| with gr.Tab("Summarize"): | |
| txt = gr.Textbox(lines=8, label="本文") | |
| length = gr.Dropdown(["short", "medium", "long"], value="short", label="長さ") | |
| out = gr.Textbox(label="要約結果") | |
| btn = gr.Button("要約する") | |
| btn.click(fn=summarize, inputs=[txt, length], outputs=out) | |
| with gr.Tab("Translate"): | |
| txt2 = gr.Textbox(lines=8, label="本文") | |
| lang = gr.Dropdown(["ja", "en", "zh", "ko", "fr", "de"], value="en", label="言語") | |
| lform = gr.Dropdown(["neutral", "formal", "casual"], value="neutral", label="文体") | |
| out2 = gr.Textbox(label="翻訳結果") | |
| btn2 = gr.Button("翻訳する") | |
| btn2.click(fn=translate, inputs=[txt2, lang, lform], outputs=out2) | |
| if __name__ == "__main__": | |
| demo.launch() | |