Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,72 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import graphviz
|
| 3 |
import re
|
| 4 |
from huggingface_hub import InferenceClient
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
#
|
| 8 |
-
#
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
def engineer_mode(deskripsi_user):
|
| 12 |
if not deskripsi_user:
|
| 13 |
return "⚠️ Mohon isi deskripsi mesin dulu.", None
|
| 14 |
|
| 15 |
-
# Prompt Insinyur
|
| 16 |
prompt_system = """
|
| 17 |
-
|
| 18 |
-
|
| 19 |
|
| 20 |
-
OUTPUT WAJIB (
|
| 21 |
-
1.
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
-
|
| 27 |
-
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
"""
|
| 31 |
|
| 32 |
try:
|
| 33 |
-
# Mengirim pesan ke Model Open Source
|
| 34 |
messages = [
|
| 35 |
{"role": "system", "content": prompt_system},
|
| 36 |
-
{"role": "user", "content": f"Rancang
|
| 37 |
]
|
| 38 |
|
| 39 |
-
# Streaming respons (agar tidak timeout)
|
| 40 |
response_text = ""
|
| 41 |
-
for message in client.chat_completion(messages, max_tokens=
|
| 42 |
token = message.choices[0].delta.content
|
| 43 |
response_text += token
|
| 44 |
|
| 45 |
-
#
|
| 46 |
dot_code = ""
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
dot_code = match.group(0)
|
| 52 |
else:
|
| 53 |
-
|
| 54 |
-
if "digraph G {" in response_text:
|
| 55 |
-
s = response_text.find("digraph G {")
|
| 56 |
-
e = response_text.rfind("}") + 1
|
| 57 |
-
dot_code = response_text[s:e]
|
| 58 |
-
else:
|
| 59 |
-
dot_code = 'digraph G { Error -> "Diagram Gagal Dibuat"; }'
|
| 60 |
|
| 61 |
# Render Gambar
|
| 62 |
try:
|
|
@@ -67,19 +77,19 @@ def engineer_mode(deskripsi_user):
|
|
| 67 |
return response_text, None
|
| 68 |
|
| 69 |
except Exception as e:
|
| 70 |
-
return f"⚠️
|
| 71 |
|
| 72 |
-
#
|
| 73 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 74 |
-
gr.Markdown("# 🏭 AI Insinyur (
|
| 75 |
-
gr.Markdown("
|
| 76 |
|
| 77 |
with gr.Row():
|
| 78 |
-
inp = gr.Textbox(lines=
|
| 79 |
-
btn = gr.Button("🚀 RANCANG
|
| 80 |
|
| 81 |
with gr.Row():
|
| 82 |
-
out_txt = gr.Markdown(label="Dokumen
|
| 83 |
out_img = gr.Image(label="Diagram Sistem")
|
| 84 |
|
| 85 |
btn.click(engineer_mode, inputs=inp, outputs=[out_txt, out_img])
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
import graphviz
|
| 4 |
import re
|
| 5 |
from huggingface_hub import InferenceClient
|
| 6 |
|
| 7 |
+
# ======================================================
|
| 8 |
+
# SETUP AMAN (MENGAMBIL DARI SETTINGS)
|
| 9 |
+
# ======================================================
|
| 10 |
+
# Kita mengambil token dari "Secrets" yang baru Anda buat
|
| 11 |
+
# Jadi tidak ada kode rahasia yang terlihat di sini.
|
| 12 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 13 |
+
|
| 14 |
+
# Inisialisasi Model Qwen 2.5 (72B)
|
| 15 |
+
client = InferenceClient("Qwen/Qwen2.5-72B-Instruct", token=hf_token)
|
| 16 |
|
| 17 |
def engineer_mode(deskripsi_user):
|
| 18 |
if not deskripsi_user:
|
| 19 |
return "⚠️ Mohon isi deskripsi mesin dulu.", None
|
| 20 |
|
| 21 |
+
# Prompt Insinyur
|
| 22 |
prompt_system = """
|
| 23 |
+
PERAN: Senior Industrial Automation Engineer (20+ Tahun).
|
| 24 |
+
TUGAS: Terjemahkan ide user menjadi Dokumen Teknis Siap Rakit.
|
| 25 |
|
| 26 |
+
OUTPUT WAJIB (BAHASA INDONESIA):
|
| 27 |
+
1. BILL OF MATERIALS (BOM):
|
| 28 |
+
- Komponen NYATA (Omron, Siemens, Schneider).
|
| 29 |
+
- Tipe spesifik (Misal: MY2N-J Relay, E3Z-D61 Sensor).
|
| 30 |
+
|
| 31 |
+
2. LOGIKA OPERASIONAL (SEQUENCE):
|
| 32 |
+
- Step-by-step dari Start sampai Stop.
|
| 33 |
+
- WAJIB ADA: Logika Safety (Emergency Stop).
|
| 34 |
+
|
| 35 |
+
3. WIRING DIAGRAM GUIDE:
|
| 36 |
+
- Koneksi Pin-to-Pin yang detail.
|
| 37 |
+
- Pisahkan Power (24V) dan Signal (I/O).
|
| 38 |
+
|
| 39 |
+
4. PROGRAM CODE:
|
| 40 |
+
- Arduino C++ atau PLC Structured Text.
|
| 41 |
+
- Lengkap dengan komentar penjelasan.
|
| 42 |
+
|
| 43 |
+
5. VISUALISASI BLOK (GRAPHVIZ DOT):
|
| 44 |
+
- Hanya kode DOT.
|
| 45 |
+
- Mulai: digraph G {
|
| 46 |
+
- Akhiri: }
|
| 47 |
+
- Gunakan: rankdir=LR; node [shape=box, style="filled,rounded", fontname="Arial"];
|
| 48 |
+
- Warna: Input (Hijau Muda #c8e6c9), Proses (Kuning #fff9c4), Output (Merah #ffccbc).
|
| 49 |
"""
|
| 50 |
|
| 51 |
try:
|
|
|
|
| 52 |
messages = [
|
| 53 |
{"role": "system", "content": prompt_system},
|
| 54 |
+
{"role": "user", "content": f"Rancang detail untuk: {deskripsi_user}"}
|
| 55 |
]
|
| 56 |
|
|
|
|
| 57 |
response_text = ""
|
| 58 |
+
for message in client.chat_completion(messages, max_tokens=3000, stream=True):
|
| 59 |
token = message.choices[0].delta.content
|
| 60 |
response_text += token
|
| 61 |
|
| 62 |
+
# Ekstraksi Diagram
|
| 63 |
dot_code = ""
|
| 64 |
+
if "digraph G {" in response_text:
|
| 65 |
+
s = response_text.find("digraph G {")
|
| 66 |
+
e = response_text.rfind("}") + 1
|
| 67 |
+
dot_code = response_text[s:e]
|
|
|
|
| 68 |
else:
|
| 69 |
+
dot_code = 'digraph G { Error -> "Diagram Tidak Tergenerate"; }'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
# Render Gambar
|
| 72 |
try:
|
|
|
|
| 77 |
return response_text, None
|
| 78 |
|
| 79 |
except Exception as e:
|
| 80 |
+
return f"⚠️ ERROR TOKEN: Cek Settings > Secrets Anda.\nDetail: {str(e)}", None
|
| 81 |
|
| 82 |
+
# Tampilan UI
|
| 83 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 84 |
+
gr.Markdown("# 🏭 AI Insinyur (Secure Version)")
|
| 85 |
+
gr.Markdown("Model: **Qwen 2.5 (72B)** | Status: **Secure Token**")
|
| 86 |
|
| 87 |
with gr.Row():
|
| 88 |
+
inp = gr.Textbox(lines=4, label="Deskripsi Mesin", placeholder="Jelaskan kebutuhan mesin Anda...")
|
| 89 |
+
btn = gr.Button("🚀 RANCANG TEKNIS", variant="primary")
|
| 90 |
|
| 91 |
with gr.Row():
|
| 92 |
+
out_txt = gr.Markdown(label="Dokumen Spesifikasi")
|
| 93 |
out_img = gr.Image(label="Diagram Sistem")
|
| 94 |
|
| 95 |
btn.click(engineer_mode, inputs=inp, outputs=[out_txt, out_img])
|