Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import json
|
3 |
+
import random
|
4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
5 |
+
import os
|
6 |
+
|
7 |
+
# 模型初始化(Hugging Face Spaces會跑)
|
8 |
+
model_name = "mistralai/Mistral-7B-Instruct"
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
10 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
11 |
+
|
12 |
+
# 資料夾路徑
|
13 |
+
DATA_DIR = "./data"
|
14 |
+
|
15 |
+
# 核心函數:抽單字+造句
|
16 |
+
def get_words_with_sentences(source="common3000", n=10):
|
17 |
+
try:
|
18 |
+
# 動態讀取指定資料檔
|
19 |
+
data_path = os.path.join(DATA_DIR, f"{source}.json")
|
20 |
+
with open(data_path, 'r', encoding='utf-8') as f:
|
21 |
+
words = json.load(f)
|
22 |
+
|
23 |
+
# 隨機抽取
|
24 |
+
selected_words = random.sample(words, n)
|
25 |
+
results = []
|
26 |
+
|
27 |
+
# 每個單字請 GPT 造句
|
28 |
+
for word_data in selected_words:
|
29 |
+
word = word_data['word']
|
30 |
+
prompt = f"Write a simple English sentence using the word '{word}' suitable for beginners."
|
31 |
+
|
32 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
33 |
+
outputs = model.generate(**inputs, max_new_tokens=30)
|
34 |
+
sentence = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
35 |
+
|
36 |
+
results.append({
|
37 |
+
"word": word,
|
38 |
+
"phonetic": word_data["phonetic"],
|
39 |
+
"sentence": sentence
|
40 |
+
})
|
41 |
+
|
42 |
+
return results
|
43 |
+
|
44 |
+
except Exception as e:
|
45 |
+
return [{"error": f"發生錯誤: {str(e)}"}]
|
46 |
+
|
47 |
+
# Gradio 介面設定
|
48 |
+
demo = gr.Interface(
|
49 |
+
fn=get_words_with_sentences,
|
50 |
+
inputs=[
|
51 |
+
gr.Textbox(value="common3000", label="選擇單字庫"),
|
52 |
+
gr.Number(value=10, label="抽幾個單字")
|
53 |
+
],
|
54 |
+
outputs="json"
|
55 |
+
)
|
56 |
+
|
57 |
+
demo.launch()
|