File size: 2,927 Bytes
fd45282
75ecc06
 
fd45282
abc9568
f5f891b
910df84
 
 
 
 
 
 
e8510b9
c0ba1b5
75ecc06
 
 
 
 
 
 
ecec6e3
75ecc06
 
 
 
 
 
ecec6e3
75ecc06
 
 
 
 
 
 
 
 
 
 
c80b246
14c753e
ecec6e3
 
 
1c0fdf3
ecec6e3
 
 
 
 
c80b246
ecec6e3
c80b246
ecec6e3
 
1c0fdf3
 
ecec6e3
 
1c0fdf3
ecec6e3
 
 
 
 
 
 
 
 
 
 
 
 
0316dcf
910df84
0316dcf
ecec6e3
 
 
 
 
 
 
0316dcf
ba7f366
ecec6e3
910df84
2b46df4
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
from peft import PeftModel
import torch
import requests
import json
import time

def auto_refresh_sensor():
    while True:
        sensor_box.value = sensor_display_text()
        time.sleep(5)

        demo.load(auto_refresh_sensor, None, None)

model_id = "deepseek-ai/deepseek-coder-1.3b-base"
lora_id = "Seunggg/lora-plant"

tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)

base = AutoModelForCausalLM.from_pretrained(
    model_id,
    torch_dtype=torch.float32,  # Hugging Face Spaces 一般用 float32
    trust_remote_code=True
)

model = PeftModel.from_pretrained(
    base,
    lora_id,
    torch_dtype=torch.float32
)
model.eval()

from transformers import pipeline
pipe = pipeline(
    "text-generation",
    model=model,
    tokenizer=tokenizer,
    max_new_tokens=256
)

def get_sensor_data():
    try:
        res = requests.get("https://arduino-realtime.onrender.com/api/data", timeout=5)
        sensor_data = res.json().get("sensorData", None)
        return sensor_data if sensor_data else {}
    except Exception as e:
        return {"错误": str(e)}

def sensor_display_text():
    sensor_data = get_sensor_data()
    return json.dumps(sensor_data, ensure_ascii=False, indent=2) if sensor_data else "暂无传感器数据"

def generate_answer(user_input):
    if not user_input.strip():
        return "请输入植物相关的问题 😊"
    prompt = f"用户提问:{user_input}\n请用更人性化的语言生成建议,并推荐相关植物文献或资料。\n回答:"
    try:
        result = pipe(prompt)
        output = result[0]["generated_text"]
        return output.replace(prompt, "").strip()
    except Exception as e:
        return f"生成建议时出错:{str(e)}"

def update_chart():
    sensor_data = get_sensor_data()
    if not sensor_data or "温度" not in sensor_data:
        return gr.LinePlot.update(value=None)
    return {
        "data": [
            {"x": [0], "y": [sensor_data.get("温度", 0)], "name": "温度"},
            {"x": [0], "y": [sensor_data.get("湿度", 0)], "name": "湿度"}
        ],
        "layout": {"title": "实时传感器数据"}
    }

# 在 Blocks 里这样写:
with gr.Blocks() as demo:
    gr.Markdown("# 🌱 植物助手 - 实时传感器联动")

    with gr.Row():
        sensor_box = gr.Textbox(label="🧪 当前传感器数据", lines=6, interactive=False)
        chart = gr.LinePlot(label="📈 实时数据图表", x="x", y="y", overlay=True)

    question = gr.Textbox(label="🌿 植物问题", lines=4, placeholder="请输入植物相关的问题 😊")
    answer_box = gr.Textbox(label="🤖 回答建议", lines=8, interactive=False)
    send_btn = gr.Button("发送")
    send_btn.click(fn=generate_answer, inputs=question, outputs=answer_box)
    # 启动后台线程更新数据
    demo.launch()