File size: 2,533 Bytes
fd45282 75ecc06 fd45282 abc9568 f5f891b c0ba1b5 75ecc06 6727ea8 75ecc06 6727ea8 75ecc06 e5c24a4 75ecc06 6727ea8 75ecc06 c80b246 14c753e 1c0fdf3 6727ea8 1c0fdf3 6727ea8 c80b246 1c0fdf3 c80b246 1c0fdf3 c80b246 1c0fdf3 c80b246 1c0fdf3 abc9568 75ecc06 c80b246 47cf770 c80b246 47cf770 c80b246 47cf770 75ecc06 3550141 |
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 |
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
from peft import PeftModel
import torch
import requests
import json
model_id = "deepseek-ai/deepseek-coder-1.3b-base"
lora_id = "Seunggg/lora-plant"
# 加载 tokenizer
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
# 加载基础模型
base = AutoModelForCausalLM.from_pretrained(
model_id,
device_map="auto",
offload_folder="offload/",
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
trust_remote_code=True
)
# 加载 LoRA adapter
model = PeftModel.from_pretrained(
base,
lora_id,
device_map="auto",
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
)
model.eval()
# 创建 pipeline
from transformers import pipeline
pipe = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
device_map="auto",
max_new_tokens=256
)
def get_sensor_data():
try:
sensor_response = requests.get("https://arduino-realtime.onrender.com/api/data", timeout=5)
sensor_data = sensor_response.json().get("sensorData", None)
return json.dumps(sensor_data, ensure_ascii=False, indent=2) if sensor_data else "暂无传感器数据"
except Exception as e:
return "⚠️ 获取失败:" + str(e)
def respond(user_input):
sensor_display = get_sensor_data()
if not user_input.strip():
return sensor_display, "请输入植物相关的问题 😊"
prompt = f"用户提问:{user_input}\n"
try:
sensor_response = requests.get("https://arduino-realtime.onrender.com/api/data", timeout=5)
sensor_data = sensor_response.json().get("sensorData", None)
if sensor_data:
prompt += f"当前传感器数据:{json.dumps(sensor_data, ensure_ascii=False)}\n"
prompt += "请用更人性化的语言生成建议,并推荐相关植物文献或资料。\n回答:"
result = pipe(prompt)
full_output = result[0]["generated_text"]
answer = full_output.replace(prompt, "").strip()
except Exception as e:
answer = f"生成建议时出错:{str(e)}"
return sensor_display, answer
# Gradio 界面
with gr.Blocks() as demo:
sensor_box = gr.Textbox(label="🧪 当前传感器数据", lines=6, interactive=False, value=get_sensor_data())
def auto_refresh():
return get_sensor_data()
sensor_box.change(fn=auto_refresh, inputs=None, outputs=sensor_box).every(5)
demo.launch() |