Spaces:
Sleeping
Sleeping
Fix Gradio compatibility issues and startup configuration
Browse files- app.py +62 -98
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import torch
|
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
| 4 |
from peft import PeftModel
|
| 5 |
import warnings
|
|
|
|
| 6 |
warnings.filterwarnings("ignore")
|
| 7 |
|
| 8 |
# 模型配置
|
|
@@ -64,7 +65,7 @@ def generate_response(prompt, max_tokens=200, temperature=0.7, top_p=0.9):
|
|
| 64 |
return "❌ Model not loaded. Please check the logs or try again."
|
| 65 |
|
| 66 |
try:
|
| 67 |
-
# 格式化输入
|
| 68 |
formatted_prompt = prompt.strip()
|
| 69 |
|
| 70 |
# 编码输入
|
|
@@ -114,105 +115,68 @@ def chat_interface(message, history, max_tokens, temperature, top_p):
|
|
| 114 |
history.append((message, error_msg))
|
| 115 |
return history, ""
|
| 116 |
|
| 117 |
-
# 创建 Gradio 应用
|
| 118 |
-
|
| 119 |
-
gr.
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
This is a fine-tuned version of Meta's Llama 3.1 8B model specialized for **robot task planning** using QLoRA technique.
|
| 123 |
-
|
| 124 |
-
**Capabilities**: Convert natural language robot commands into structured task sequences for excavators, dump trucks, and other construction robots.
|
| 125 |
-
|
| 126 |
-
**Model**: [YongdongWang/llama-3.1-8b-dart-qlora](https://huggingface.co/YongdongWang/llama-3.1-8b-dart-qlora)
|
| 127 |
-
|
| 128 |
-
⚠️ **Note**: Model loading may take a few minutes on first startup.
|
| 129 |
-
""")
|
| 130 |
-
|
| 131 |
-
with gr.Row():
|
| 132 |
-
with gr.Column(scale=3):
|
| 133 |
-
chatbot = gr.Chatbot(
|
| 134 |
-
label="Task Planning Results",
|
| 135 |
-
height=400,
|
| 136 |
-
show_label=True,
|
| 137 |
-
container=True,
|
| 138 |
-
bubble_full_width=False
|
| 139 |
-
)
|
| 140 |
-
|
| 141 |
-
msg = gr.Textbox(
|
| 142 |
-
label="Robot Command",
|
| 143 |
-
placeholder="Enter robot task command (e.g., 'Deploy Excavator 1 to Soil Area 1')...",
|
| 144 |
-
lines=2,
|
| 145 |
-
max_lines=5,
|
| 146 |
-
show_label=True,
|
| 147 |
-
container=True
|
| 148 |
-
)
|
| 149 |
-
|
| 150 |
-
with gr.Row():
|
| 151 |
-
send_btn = gr.Button("Generate Tasks", variant="primary", size="sm")
|
| 152 |
-
clear_btn = gr.Button("Clear", variant="secondary", size="sm")
|
| 153 |
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
examples=[
|
| 187 |
-
["Deploy Excavator 1 to Soil Area 1 for excavation."],
|
| 188 |
-
["Send Dump Truck 1 to collect material, then unload at storage area."],
|
| 189 |
-
["Move all robots to avoid Puddle 1 after inspection."],
|
| 190 |
-
["Deploy multiple excavators to different soil areas simultaneously."],
|
| 191 |
-
["Coordinate dump trucks to transport materials from excavation site to storage."],
|
| 192 |
-
["Send robot to inspect rock area, then avoid with all other robots."],
|
| 193 |
-
["Return all robots to start position after completing tasks."],
|
| 194 |
-
],
|
| 195 |
-
inputs=msg,
|
| 196 |
-
label="💡 Example Robot Commands"
|
| 197 |
-
)
|
| 198 |
-
|
| 199 |
-
# 事件处理
|
| 200 |
-
msg.submit(
|
| 201 |
-
chat_interface,
|
| 202 |
-
inputs=[msg, chatbot, max_tokens, temperature, top_p],
|
| 203 |
-
outputs=[chatbot, msg]
|
| 204 |
-
)
|
| 205 |
-
|
| 206 |
-
send_btn.click(
|
| 207 |
-
chat_interface,
|
| 208 |
-
inputs=[msg, chatbot, max_tokens, temperature, top_p],
|
| 209 |
-
outputs=[chatbot, msg]
|
| 210 |
-
)
|
| 211 |
|
| 212 |
-
|
| 213 |
-
lambda: ([], ""),
|
| 214 |
-
outputs=[chatbot, msg]
|
| 215 |
-
)
|
| 216 |
|
| 217 |
if __name__ == "__main__":
|
| 218 |
-
demo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
| 4 |
from peft import PeftModel
|
| 5 |
import warnings
|
| 6 |
+
import os
|
| 7 |
warnings.filterwarnings("ignore")
|
| 8 |
|
| 9 |
# 模型配置
|
|
|
|
| 65 |
return "❌ Model not loaded. Please check the logs or try again."
|
| 66 |
|
| 67 |
try:
|
| 68 |
+
# 格式化输入
|
| 69 |
formatted_prompt = prompt.strip()
|
| 70 |
|
| 71 |
# 编码输入
|
|
|
|
| 115 |
history.append((message, error_msg))
|
| 116 |
return history, ""
|
| 117 |
|
| 118 |
+
# 创建 Gradio 应用 - 简化版本以避免兼容性问题
|
| 119 |
+
def create_interface():
|
| 120 |
+
with gr.Blocks(title="Robot Task Planning - Llama 3.1 8B") as demo:
|
| 121 |
+
gr.Markdown("""
|
| 122 |
+
# 🤖 Llama 3.1 8B - Robot Task Planning
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
+
This is a fine-tuned version of Meta's Llama 3.1 8B model specialized for **robot task planning** using QLoRA technique.
|
| 125 |
+
|
| 126 |
+
**Model**: [YongdongWang/llama-3.1-8b-dart-qlora](https://huggingface.co/YongdongWang/llama-3.1-8b-dart-qlora)
|
| 127 |
+
|
| 128 |
+
⚠️ **Note**: Model loading may take a few minutes on first startup.
|
| 129 |
+
""")
|
| 130 |
+
|
| 131 |
+
# 聊天界面
|
| 132 |
+
chatbot = gr.Chatbot(label="Task Planning Results", height=400)
|
| 133 |
+
msg = gr.Textbox(
|
| 134 |
+
label="Robot Command",
|
| 135 |
+
placeholder="Enter robot task command (e.g., 'Deploy Excavator 1 to Soil Area 1')...",
|
| 136 |
+
lines=2
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
# 控制按钮
|
| 140 |
+
with gr.Row():
|
| 141 |
+
send_btn = gr.Button("Generate Tasks", variant="primary")
|
| 142 |
+
clear_btn = gr.Button("Clear")
|
| 143 |
+
|
| 144 |
+
# 生成参数 - 简化版本
|
| 145 |
+
with gr.Accordion("⚙️ Generation Settings", open=False):
|
| 146 |
+
max_tokens = gr.Slider(50, 500, 200, label="Max Tokens")
|
| 147 |
+
temperature = gr.Slider(0.1, 2.0, 0.7, label="Temperature")
|
| 148 |
+
top_p = gr.Slider(0.1, 1.0, 0.9, label="Top-p")
|
| 149 |
+
|
| 150 |
+
# 示例 - 简化版本
|
| 151 |
+
with gr.Accordion("💡 Example Commands", open=False):
|
| 152 |
+
examples = [
|
| 153 |
+
"Deploy Excavator 1 to Soil Area 1 for excavation.",
|
| 154 |
+
"Send Dump Truck 1 to collect material, then unload at storage area.",
|
| 155 |
+
"Move all robots to avoid Puddle 1 after inspection.",
|
| 156 |
+
"Deploy multiple excavators to different soil areas simultaneously.",
|
| 157 |
+
"Coordinate dump trucks to transport materials from excavation site to storage.",
|
| 158 |
+
]
|
| 159 |
|
| 160 |
+
for example in examples:
|
| 161 |
+
example_btn = gr.Button(example, size="sm")
|
| 162 |
+
example_btn.click(lambda x=example: x, outputs=msg)
|
| 163 |
+
|
| 164 |
+
# 事件处理
|
| 165 |
+
def submit_message(message, history, max_tokens, temperature, top_p):
|
| 166 |
+
return chat_interface(message, history, max_tokens, temperature, top_p)
|
| 167 |
+
|
| 168 |
+
msg.submit(submit_message, [msg, chatbot, max_tokens, temperature, top_p], [chatbot, msg])
|
| 169 |
+
send_btn.click(submit_message, [msg, chatbot, max_tokens, temperature, top_p], [chatbot, msg])
|
| 170 |
+
clear_btn.click(lambda: ([], ""), outputs=[chatbot, msg])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
+
return demo
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
if __name__ == "__main__":
|
| 175 |
+
demo = create_interface()
|
| 176 |
+
# 修复启动配置 - 关键修复!
|
| 177 |
+
demo.launch(
|
| 178 |
+
server_name="0.0.0.0",
|
| 179 |
+
server_port=7860,
|
| 180 |
+
share=True, # 这是关键!
|
| 181 |
+
show_error=True
|
| 182 |
+
)
|
requirements.txt
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
-
gradio==4.
|
| 2 |
transformers==4.44.2
|
| 3 |
torch==2.1.0
|
| 4 |
peft==0.7.1
|
| 5 |
bitsandbytes==0.41.3
|
| 6 |
accelerate==0.24.1
|
| 7 |
scipy==1.11.4
|
|
|
|
|
|
| 1 |
+
gradio==4.20.0
|
| 2 |
transformers==4.44.2
|
| 3 |
torch==2.1.0
|
| 4 |
peft==0.7.1
|
| 5 |
bitsandbytes==0.41.3
|
| 6 |
accelerate==0.24.1
|
| 7 |
scipy==1.11.4
|
| 8 |
+
packaging
|