File size: 2,737 Bytes
8abdd3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
70
71
72
73
74
75
import os
import gradio as gr
from openai import OpenAI

# 从 Hugging Face Space 的 “Secrets” 读取
API_KEY = os.getenv("OPENAI_API_KEY")
if not API_KEY:
    raise RuntimeError("Missing OPENAI_API_KEY secret. Set it in Settings → Variables & secrets.")

# 可改成你账号可用的模型名
MODEL_NAME = "gpt-4o-mini"

client = OpenAI(api_key=API_KEY)

SYSTEM_PROMPT = """You are an English vocabulary tutor.
Given a list of English target words/phrases, generate short, practical exercises
for each word. Keep the language simple and concise.
For EACH word, produce 3–5 exercises from the pool below:
1) Meaning multiple-choice (3 options, one correct).
2) Cloze sentence (with 3 options).
3) Synonym OR antonym (1 item).
4) Word form/use (e.g., noun→adj; collocation).
5) A simple example sentence (CEFR A2–B1).
After each word block, provide an Answer Key.
Keep formatting compact and copy-friendly.
"""

def build_user_prompt(words_raw: str) -> str:
    raw = (words_raw or "").replace(",", ",")
    items = []
    for seg in raw.split(","):
        for w in seg.splitlines():
            w = w.strip()
            if w:
                items.append(w)
    words = items[:12]  # 限制数量,避免超长
    if not words:
        return ""
    return (
        "TARGET WORDS:\n- " + "\n- ".join(words) +
        "\n\nFORMAT STRICTLY LIKE:\n"
        "== word ==\n"
        "1) ...\n2) ...\n3) ...\n4) ...(optional)\n5) ...(optional)\n"
        "[Answer Key]: ...\n"
        "Use plain English. Avoid overlong sentences. CEFR A2–B1."
    )

def generate(words_raw: str, level: str):
    prompt = build_user_prompt(words_raw)
    if not prompt:
        return "Please paste today’s English words (comma or newline separated)."
    # 根据难度微调系统提示
    level_hint = f"Target difficulty: {level}."
    resp = client.chat.completions.create(
        model=MODEL_NAME,
        temperature=0.4,
        max_tokens=1200,
        messages=[
            {"role": "system", "content": SYSTEM_PROMPT + "\n" + level_hint},
            {"role": "user", "content": prompt},
        ],
    )
    return resp.choices[0].message.content

with gr.Blocks(title="English Vocab Trainer") as demo:
    gr.Markdown("## 🧠 English Vocab Trainer\nPaste today’s words, choose level, click **Generate**.")
    with gr.Row():
        level = gr.Radio(choices=["A2", "B1", "B2"], value="A2", label="Difficulty")
    words = gr.Textbox(lines=8, label="Today's words (comma or newline separated)", placeholder="serendipity\nobscure, diligent\ncome up with")
    btn = gr.Button("Generate")
    out = gr.Markdown()
    btn.click(generate, inputs=[words, level], outputs=out)

if __name__ == "__main__":
    demo.launch()