### 文言文翻译 import simplestart as ss import requests import json ##import mymodule as mm import psutil #函数部分 def mychange(event): ss.session.result = "" if event.index == 0: ss.session.prompt1 = "请将文言文拷贝到这里" data = mytext.data data["value"] = "子曰:温故而知新" mytext.data = data #mytext.value = "子曰:温故而知新" ### todo 随机 ss.session.method = 0 else: ss.session.prompt1 = "请将现代文拷贝到这里" data = mytext.data data["value"] = "谦虚使人进步,骄傲使人失败" mytext.data = data #mytext.value = "谦虚使人进步,骄傲使人失败" ### todo 随机 ss.session.method = 1 def predict(): ss.session.result = "稍安勿躁,服务器正在运算中..." ss.message(mytext.value) method = str(ss.session.method) input = mytext.value[:100].replace("\n", "") #本地模式 ##response = mm.predict("'" + input + "'", ss.session.method) ##res = response.replace("<|endoftext|>", "") #服务器模式 response = inference("'" + input + "'", method) res = response["processed_text"].replace("<|endoftext|>", "") myresult.value = res def inference(input_text, method): #url = "http://0.0.0.0:8106/process" # 假设POST请求的路径是 /process url = "https://test2023h5-fastapi-wyw.hf.space/process" data = {"text": input_text, "method": method} # 将 input_text 和 method 作为 JSON 数据发送 res = requests.post(url, json=data) # 发送 POST 请求,传递 JSON 数据 return res.json() # 假设服务器返回 JSON 响应 def demo1(state, value): ss.session["result"] = "" ss.session["comment"] = "" if ss.session.method == 0: mytext.value = "己所不欲勿施于人" else: mytext.value = "谦虚使人进步" def mem_info(): # 获取内存信息 mem_info = psutil.virtual_memory() # 剩余内存,单位为字节 remaining_memory = mem_info.available # 将字节转换为更易读的单位(例如 GB) remaining_memory_gb = remaining_memory / (1024 ** 3) return remaining_memory_gb #会话变量 ss.session.method = 0 ss.session.prompt1 = "请将文言文拷贝到这里" ss.session.text = "子曰:温故而知新" ### UI部分 ss.write("### 😾 古文-现代文翻译演示") myradio = ss.radio(["古文-现代文", "现代文-古文"], index=0, onchange=mychange) ss.space() mytext = ss.textarea("子曰:温故而知新", label="@prompt1") myresult = ss.textarea("@result", label="翻译结果 (CPU服务器,10秒内出结果,稍安勿躁)") ss.button("开始翻译", onclick = predict) ss.space() ss.write("---") ###剩余内存测试 remaining_memory_gb = mem_info() ss.write(f"剩余内存: {remaining_memory_gb:.2f} GB")