Update app.py
Browse files
app.py
CHANGED
@@ -11,7 +11,7 @@ lora_id = "Seunggg/lora-plant"
|
|
11 |
# 加载 tokenizer
|
12 |
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
13 |
|
14 |
-
#
|
15 |
base = AutoModelForCausalLM.from_pretrained(
|
16 |
model_id,
|
17 |
device_map="auto",
|
@@ -20,7 +20,7 @@ base = AutoModelForCausalLM.from_pretrained(
|
|
20 |
trust_remote_code=True
|
21 |
)
|
22 |
|
23 |
-
# 加载 LoRA adapter
|
24 |
model = PeftModel.from_pretrained(
|
25 |
base,
|
26 |
lora_id,
|
@@ -30,7 +30,7 @@ model = PeftModel.from_pretrained(
|
|
30 |
|
31 |
model.eval()
|
32 |
|
33 |
-
#
|
34 |
from transformers import pipeline
|
35 |
pipe = pipeline(
|
36 |
"text-generation",
|
@@ -40,19 +40,13 @@ pipe = pipeline(
|
|
40 |
max_new_tokens=256
|
41 |
)
|
42 |
|
43 |
-
from ask_api import ask_with_sensor # 引入调用函数
|
44 |
-
|
45 |
def get_sensor_data():
|
46 |
try:
|
47 |
sensor_response = requests.get("https://arduino-realtime.onrender.com/api/data", timeout=5)
|
48 |
sensor_data = sensor_response.json().get("sensorData", None)
|
49 |
-
|
50 |
except Exception as e:
|
51 |
-
|
52 |
-
return sensor_display
|
53 |
-
|
54 |
-
def show_sensor_data(_=None):
|
55 |
-
return get_sensor_data()
|
56 |
|
57 |
def respond(user_input):
|
58 |
sensor_display = get_sensor_data()
|
@@ -75,21 +69,6 @@ def respond(user_input):
|
|
75 |
|
76 |
return sensor_display, answer
|
77 |
|
78 |
-
# 构建提示词
|
79 |
-
prompt = f"用户提问:{user_input}\n"
|
80 |
-
if sensor_data:
|
81 |
-
prompt += f"当前传感器数据:{json.dumps(sensor_data, ensure_ascii=False)}\n"
|
82 |
-
prompt += "请用更人性化的语言生成建议,并推荐相关植物文献或资料。\n回答:"
|
83 |
-
|
84 |
-
# 模型生成
|
85 |
-
try:
|
86 |
-
result = pipe(prompt)
|
87 |
-
full_output = result[0]["generated_text"]
|
88 |
-
# 删除重复的提问部分,只保留回答段
|
89 |
-
answer = full_output.replace(prompt, "").strip()
|
90 |
-
except Exception as e:
|
91 |
-
answer = f"生成建议时出错:{str(e)}"
|
92 |
-
|
93 |
# Gradio 界面
|
94 |
with gr.Blocks() as demo:
|
95 |
with gr.Row():
|
@@ -97,12 +76,13 @@ with gr.Blocks() as demo:
|
|
97 |
question_box = gr.Textbox(label="🌿 植物问题", lines=4)
|
98 |
answer_box = gr.Textbox(label="🤖 回答建议", lines=8, interactive=False)
|
99 |
|
100 |
-
#
|
101 |
-
sensor_box.change(fn=show_sensor_data, inputs=None, outputs=sensor_box).every(2)
|
102 |
-
|
103 |
-
# 用户提问后,更新传感器 + 回答
|
104 |
question_box.submit(fn=respond, inputs=question_box, outputs=[sensor_box, answer_box])
|
105 |
|
106 |
-
|
|
|
|
|
107 |
|
|
|
108 |
|
|
|
|
11 |
# 加载 tokenizer
|
12 |
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
13 |
|
14 |
+
# 加载基础模型
|
15 |
base = AutoModelForCausalLM.from_pretrained(
|
16 |
model_id,
|
17 |
device_map="auto",
|
|
|
20 |
trust_remote_code=True
|
21 |
)
|
22 |
|
23 |
+
# 加载 LoRA adapter
|
24 |
model = PeftModel.from_pretrained(
|
25 |
base,
|
26 |
lora_id,
|
|
|
30 |
|
31 |
model.eval()
|
32 |
|
33 |
+
# 创建 pipeline
|
34 |
from transformers import pipeline
|
35 |
pipe = pipeline(
|
36 |
"text-generation",
|
|
|
40 |
max_new_tokens=256
|
41 |
)
|
42 |
|
|
|
|
|
43 |
def get_sensor_data():
|
44 |
try:
|
45 |
sensor_response = requests.get("https://arduino-realtime.onrender.com/api/data", timeout=5)
|
46 |
sensor_data = sensor_response.json().get("sensorData", None)
|
47 |
+
return json.dumps(sensor_data, ensure_ascii=False, indent=2) if sensor_data else "暂无传感器数据"
|
48 |
except Exception as e:
|
49 |
+
return "⚠️ 获取失败:" + str(e)
|
|
|
|
|
|
|
|
|
50 |
|
51 |
def respond(user_input):
|
52 |
sensor_display = get_sensor_data()
|
|
|
69 |
|
70 |
return sensor_display, answer
|
71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
# Gradio 界面
|
73 |
with gr.Blocks() as demo:
|
74 |
with gr.Row():
|
|
|
76 |
question_box = gr.Textbox(label="🌿 植物问题", lines=4)
|
77 |
answer_box = gr.Textbox(label="🤖 回答建议", lines=8, interactive=False)
|
78 |
|
79 |
+
# 用户提交提问后,更新传感器+回答
|
|
|
|
|
|
|
80 |
question_box.submit(fn=respond, inputs=question_box, outputs=[sensor_box, answer_box])
|
81 |
|
82 |
+
# 定时每 5 秒自动刷新传感器数据
|
83 |
+
def update_sensor():
|
84 |
+
return get_sensor_data()
|
85 |
|
86 |
+
gr.Timer(interval=5, fn=update_sensor, outputs=sensor_box)
|
87 |
|
88 |
+
demo.launch()
|