cwadayi commited on
Commit
0ee2d51
·
verified ·
1 Parent(s): 4063a12

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -59
app.py CHANGED
@@ -1,36 +1,21 @@
1
  import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import torch
 
4
 
5
- # --- Configuration ---
6
- # This model is small enough to run on a free CPU Space
7
  MODEL_NAME = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
8
-
9
- # --- Model Loading ---
10
- print("Loading model and tokenizer...")
11
- # Load the model directly to CPU. No need for device_map or specific dtypes.
12
  model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
13
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
14
- print("Model and tokenizer loaded.")
15
 
16
- # --- Text Generation Function ---
17
  def generate_text(prompt, max_new_tokens, temperature, top_p):
18
- """
19
- Generates text using the TinyLlama model with its chat template.
20
- """
21
- print(f"Generating response for prompt: '{prompt}'")
22
-
23
- # Format the prompt using the model's chat template
24
- # The format is <|system|>...<|user|>...<|assistant|>
25
- chat = [
26
- {"role": "user", "content": prompt}
27
- ]
28
  formatted_prompt = tokenizer.apply_chat_template(chat, tokenize=False, add_generation_prompt=True)
29
-
30
- # Tokenize the formatted prompt
31
  inputs = tokenizer(formatted_prompt, return_tensors="pt")
32
-
33
- # Generate text
34
  with torch.no_grad():
35
  outputs = model.generate(
36
  **inputs,
@@ -40,47 +25,56 @@ def generate_text(prompt, max_new_tokens, temperature, top_p):
40
  top_p=float(top_p),
41
  eos_token_id=tokenizer.eos_token_id
42
  )
 
 
 
43
 
44
- # Decode the full output
45
- decoded_output = tokenizer.decode(outputs[0], skip_special_tokens=True)
46
-
47
- # Extract only the assistant's response
48
- # The output includes the prompt, so we find the start of the assistant's part
49
- assistant_marker = "<|assistant|>"
50
- assistant_start_index = decoded_output.find(assistant_marker)
51
- if assistant_start_index != -1:
52
- response = decoded_output[assistant_start_index + len(assistant_marker):].strip()
53
- else:
54
- # Fallback if the marker isn't found (should be rare)
55
- response = "Could not parse the model's response."
56
-
57
- print(f"Generated response: {response}")
58
- return response
59
 
60
- # --- Gradio Interface ---
61
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
62
- gr.Markdown("# 🦙 TinyLlama Chat (CPU Version)")
63
- gr.Markdown("This Space runs `TinyLlama-1.1B-Chat-v1.0`, a compact model that runs on free CPU hardware.")
64
-
65
- with gr.Column():
66
- prompt_input = gr.Textbox(
67
- label="Your Prompt",
68
- placeholder="Explain the importance of bees in the ecosystem."
69
- )
70
-
71
- with gr.Accordion("Generation Parameters", open=False):
72
- max_new_tokens_slider = gr.Slider(minimum=50, maximum=512, value=256, step=1, label="Max New Tokens")
73
- temperature_slider = gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.05, label="Temperature")
74
- top_p_slider = gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.05, label="Top-P (Nucleus Sampling)")
75
 
76
- generate_button = gr.Button("Generate Response ✨", variant="primary")
77
- output_text = gr.Textbox(label="Model's Response", lines=10, interactive=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
- generate_button.click(
80
- fn=generate_text,
81
- inputs=[prompt_input, max_new_tokens_slider, temperature_slider, top_p_slider],
82
- outputs=output_text
83
- )
 
 
 
 
 
 
 
 
84
 
85
- # --- Launch the App ---
86
  demo.launch()
 
1
  import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import torch
4
+ import webbrowser # 用於開啟網頁地圖連結
5
 
6
+ # --- 模型配置 (CPU TinyLlama) ---
 
7
  MODEL_NAME = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
8
+ print("Loading TinyLlama model and tokenizer...")
 
 
 
9
  model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
10
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
11
+ print("TinyLlama model and tokenizer loaded.")
12
 
13
+ # --- 文字生成函數 ---
14
  def generate_text(prompt, max_new_tokens, temperature, top_p):
15
+ print(f"Generating TinyLlama response for prompt: '{prompt}'")
16
+ chat = [{"role": "user", "content": prompt}]
 
 
 
 
 
 
 
 
17
  formatted_prompt = tokenizer.apply_chat_template(chat, tokenize=False, add_generation_prompt=True)
 
 
18
  inputs = tokenizer(formatted_prompt, return_tensors="pt")
 
 
19
  with torch.no_grad():
20
  outputs = model.generate(
21
  **inputs,
 
25
  top_p=float(top_p),
26
  eos_token_id=tokenizer.eos_token_id
27
  )
28
+ decoded_output = tokenizer.decode(outputs[:, inputs["input_ids"].shape[-1]:][0], skip_special_tokens=True)
29
+ print(f"Generated response: {decoded_output}")
30
+ return decoded_output.strip()
31
 
32
+ # --- 地圖生成函數 (開啟 Google 地圖網頁) ---
33
+ def show_map(location_query):
34
+ """開啟包含指定地點的 Google 地圖網頁。"""
35
+ base_url = "https://www.google.com/maps/search/?api=1&query="
36
+ map_url = base_url + location_query.replace(" ", "+")
37
+ webbrowser.open_new_tab(map_url)
38
+ return f"已在您的預設瀏覽器中開啟 {location_query} 的地圖。"
 
 
 
 
 
 
 
 
39
 
40
+ # --- Gradio 介面 ---
41
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
42
+ gr.Markdown("# 多功能應用:文字生成與地圖顯示")
43
+ gr.Markdown("這個應用程式可以生成文字,也可以根據您輸入的地點顯示地圖。")
 
 
 
 
 
 
 
 
 
 
 
44
 
45
+ with gr.TabbedInterface(["文字生成", "顯示地圖"]) as tabbed:
46
+ with gr.TabItem("文字生成"):
47
+ gr.Markdown("## 使用 TinyLlama 生成文字")
48
+ with gr.Column():
49
+ prompt_input = gr.Textbox(
50
+ label="您的提示詞",
51
+ placeholder="請輸入您想讓模型接續的文字..."
52
+ )
53
+ with gr.Accordion("生成參數", open=False):
54
+ max_new_tokens_slider = gr.Slider(minimum=50, maximum=512, value=256, step=1, label="最大生成 Token 數")
55
+ temperature_slider = gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.05, label="溫度 (Temperature)")
56
+ top_p_slider = gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.05, label="Top-P (Nucleus Sampling)")
57
+ generate_button = gr.Button("生成文字 ✨", variant="primary")
58
+ output_text = gr.Textbox(label="模型的回應", lines=10, interactive=False)
59
+ generate_button.click(
60
+ fn=generate_text,
61
+ inputs=[prompt_input, max_new_tokens_slider, temperature_slider, top_p_slider],
62
+ outputs=output_text
63
+ )
64
 
65
+ with gr.TabItem("顯示地圖"):
66
+ gr.Markdown("## 顯示指定地點的地圖")
67
+ location_input = gr.Textbox(
68
+ label="輸入地點名稱",
69
+ placeholder="例如:台北 101, New York Central Park, 東京鐵塔"
70
+ )
71
+ show_map_button = gr.Button("顯示地圖 🗺️", variant="primary")
72
+ map_output = gr.Textbox(label="地圖顯示訊息", interactive=False)
73
+ show_map_button.click(
74
+ fn=show_map,
75
+ inputs=location_input,
76
+ outputs=map_output
77
+ )
78
 
79
+ # --- 啟動 Gradio 應用 ---
80
  demo.launch()