CHUNYU0505 commited on
Commit
2aa3d8b
·
verified ·
1 Parent(s): fb13185

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -0
app.py CHANGED
@@ -1,3 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # -------------------------------
2
  # 5. 生成文章(即時進度)
3
  # -------------------------------
@@ -66,3 +101,4 @@ with gr.Blocks() as demo:
66
  # -------------------------------
67
  if __name__ == "__main__":
68
  demo.launch()
 
 
1
+ # -------------------------------
2
+ # 4. 本地推論模型設定
3
+ # -------------------------------
4
+ MODEL_MAP = {
5
+ "Auto": None, # 自動選擇
6
+ "Gemma-2B": "google/gemma-2b",
7
+ "Gemma-7B": "google/gemma-7b",
8
+ "BTLM-3B-8K": "cerebras/btlm-3b-8k",
9
+ "gpt-oss-20B": "openai-community/gpt-oss-20b"
10
+ }
11
+
12
+ # 快取 pipeline 避免每次重建
13
+ _loaded_pipelines = {}
14
+
15
+ def get_pipeline(model_name):
16
+ if model_name not in _loaded_pipelines:
17
+ print(f"🔄 正在載入模型 {model_name} ...")
18
+ model_id = MODEL_MAP[model_name]
19
+ generator = pipeline(
20
+ "text-generation",
21
+ model=model_id,
22
+ tokenizer=model_id,
23
+ device_map="auto",
24
+ )
25
+ _loaded_pipelines[model_name] = generator
26
+ return _loaded_pipelines[model_name]
27
+
28
+ def call_local_inference(model_name, prompt, max_new_tokens=512):
29
+ try:
30
+ generator = get_pipeline(model_name)
31
+ outputs = generator(prompt, max_new_tokens=max_new_tokens, do_sample=True, temperature=0.7)
32
+ return outputs[0]["generated_text"]
33
+ except Exception as e:
34
+ return f"(生成失敗:{e})"
35
+
36
  # -------------------------------
37
  # 5. 生成文章(即時進度)
38
  # -------------------------------
 
101
  # -------------------------------
102
  if __name__ == "__main__":
103
  demo.launch()
104
+