Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,125 +1,60 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
"gemini-1.5-pro-latest",
|
| 27 |
-
"gemini-1.0-pro"
|
| 28 |
-
]
|
| 29 |
-
|
| 30 |
-
headers = {'Content-Type': 'application/json'}
|
| 31 |
-
payload = {
|
| 32 |
-
"contents": [{"parts": [{"text": prompt_text}]}]
|
| 33 |
-
}
|
| 34 |
-
|
| 35 |
-
last_error = ""
|
| 36 |
-
|
| 37 |
-
# LOOPING: Coba satu per satu
|
| 38 |
-
for model_name in models:
|
| 39 |
-
try:
|
| 40 |
-
# Coba Tembak URL Model Tersebut
|
| 41 |
-
url = f"https://generativelanguage.googleapis.com/v1beta/models/{model_name}:generateContent?key={MY_KEY}"
|
| 42 |
-
|
| 43 |
-
response = requests.post(url, headers=headers, data=json.dumps(payload), timeout=30)
|
| 44 |
-
|
| 45 |
-
# Jika Sukses (Kode 200)
|
| 46 |
-
if response.status_code == 200:
|
| 47 |
-
data = response.json()
|
| 48 |
-
# Ambil teks jawaban
|
| 49 |
-
return data['candidates'][0]['content']['parts'][0]['text']
|
| 50 |
-
|
| 51 |
-
# Jika Gagal, simpan errornya dan lanjut ke model berikutnya
|
| 52 |
-
else:
|
| 53 |
-
last_error = f"Model {model_name} Gagal: {response.text}"
|
| 54 |
-
continue # Coba model bawahnya
|
| 55 |
-
|
| 56 |
-
except Exception as e:
|
| 57 |
-
last_error = str(e)
|
| 58 |
-
continue
|
| 59 |
-
|
| 60 |
-
# Jika semua model gagal
|
| 61 |
-
return f"❌ SEMUA MODEL GOOGLE GAGAL.\nDetail Error Terakhir: {last_error}"
|
| 62 |
-
|
| 63 |
-
# ======================================================
|
| 64 |
-
# LOGIKA INSINYUR
|
| 65 |
-
# ======================================================
|
| 66 |
-
def engineer_mode(deskripsi_user):
|
| 67 |
-
if not deskripsi_user:
|
| 68 |
-
return "⚠️ Mohon isi deskripsi mesin dulu.", None
|
| 69 |
-
|
| 70 |
-
prompt_system = f"""
|
| 71 |
-
ROLE: Senior Industrial Automation Engineer.
|
| 72 |
-
TASK: Design a complete system for: "{deskripsi_user}"
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
2. CARA KERJA (Step-by-step + Safety).
|
| 77 |
-
3. WIRING (Pin-to-Pin).
|
| 78 |
-
4. KODE PROGRAM (Arduino/PLC).
|
| 79 |
-
5. DIAGRAM BLOK (DOT CODE ONLY):
|
| 80 |
-
- Start: digraph G {{
|
| 81 |
-
- End: }}
|
| 82 |
-
- Style: rankdir=LR; node [shape=box style="filled,rounded" fillcolor="#e1f5fe"];
|
| 83 |
-
"""
|
| 84 |
-
|
| 85 |
-
# Panggil Fungsi Sapu Jagat
|
| 86 |
-
hasil_teks = query_google_gemini(prompt_system)
|
| 87 |
-
|
| 88 |
-
# Cek jika hasil adalah pesan error
|
| 89 |
-
if "❌" in hasil_teks:
|
| 90 |
-
return hasil_teks, None
|
| 91 |
-
|
| 92 |
-
# --- PROSES GAMBAR DIAGRAM ---
|
| 93 |
-
dot_code = ""
|
| 94 |
-
if "digraph G {" in hasil_teks:
|
| 95 |
-
s = hasil_teks.find("digraph G {")
|
| 96 |
-
e = hasil_teks.rfind("}") + 1
|
| 97 |
-
dot_code = hasil_teks[s:e]
|
| 98 |
-
else:
|
| 99 |
-
dot_code = 'digraph G { Error -> "Diagram Tidak Tergenerate"; }'
|
| 100 |
-
|
| 101 |
-
try:
|
| 102 |
-
src = graphviz.Source(dot_code)
|
| 103 |
-
output_path = src.render("diagram_output", format="png")
|
| 104 |
-
return hasil_teks, output_path
|
| 105 |
-
except:
|
| 106 |
-
return hasil_teks, None
|
| 107 |
-
|
| 108 |
-
# ======================================================
|
| 109 |
-
# TAMPILAN UI
|
| 110 |
-
# ======================================================
|
| 111 |
-
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 112 |
-
gr.Markdown("# 🏭 AI Insinyur (Google Multi-Model)")
|
| 113 |
-
gr.Markdown("Sistem ini otomatis mencari jalur Google Gemini yang aktif (Flash/Pro/Latest).")
|
| 114 |
|
| 115 |
-
|
| 116 |
-
inp = gr.Textbox(lines=4, label="Deskripsi Mesin", placeholder="Jelaskan kebutuhan mesin Anda...")
|
| 117 |
-
btn = gr.Button("🚀 RANCANG (AUTO-CONNECT)", variant="primary")
|
| 118 |
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from llama_cpp import Llama
|
| 3 |
+
from huggingface_hub import hf_hub_download
|
| 4 |
+
|
| 5 |
+
# 1. Download Model Koding Terbaik (Qwen2.5-Coder 7B)
|
| 6 |
+
# Kita pakai versi GGUF Q4_K_M agar muat di RAM gratisan dan cepat
|
| 7 |
+
print("Sedang mendownload model... Mohon tunggu.")
|
| 8 |
+
model_path = hf_hub_download(
|
| 9 |
+
repo_id="Qwen/Qwen2.5-Coder-7B-Instruct-GGUF",
|
| 10 |
+
filename="qwen2.5-coder-7b-instruct-q4_k_m.gguf"
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
# 2. Load Model ke Memory (CPU)
|
| 14 |
+
llm = Llama(
|
| 15 |
+
model_path=model_path,
|
| 16 |
+
n_ctx=8192, # Context window besar untuk kode panjang
|
| 17 |
+
n_threads=2, # Menggunakan 2 core CPU
|
| 18 |
+
verbose=False
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
# 3. Fungsi Chat
|
| 22 |
+
def chat_response(message, history):
|
| 23 |
+
# Format prompt agar sesuai standar Qwen
|
| 24 |
+
system_prompt = "You are an expert coding assistant. Write clean, professional code."
|
| 25 |
+
prompt_str = f"<|im_start|>system\n{system_prompt}<|im_end|>\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
for user_input, ai_output in history:
|
| 28 |
+
prompt_str += f"<|im_start|>user\n{user_input}<|im_end|>\n<|im_start|>assistant\n{ai_output}<|im_end|>\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
prompt_str += f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
# Generate jawaban stream (agar teks muncul satu-satu tidak nge-lag)
|
| 33 |
+
output_stream = llm(
|
| 34 |
+
prompt_str,
|
| 35 |
+
max_tokens=2048,
|
| 36 |
+
stop=["<|im_end|>"],
|
| 37 |
+
stream=True
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
partial_message = ""
|
| 41 |
+
for chunk in output_stream:
|
| 42 |
+
delta = chunk['choices'][0]['text']
|
| 43 |
+
partial_message += delta
|
| 44 |
+
yield partial_message
|
| 45 |
+
|
| 46 |
+
# 4. Tampilan Web (Mobile Friendly)
|
| 47 |
+
custom_css = """
|
| 48 |
+
footer {visibility: hidden}
|
| 49 |
+
"""
|
| 50 |
+
|
| 51 |
+
demo = gr.ChatInterface(
|
| 52 |
+
fn=chat_response,
|
| 53 |
+
title="🤖 Qwen Android Coder",
|
| 54 |
+
description="Asisten Koding Profesional untuk Android. Copy kode & jalankan di Termux/Acode.",
|
| 55 |
+
theme=gr.themes.Soft(),
|
| 56 |
+
css=custom_css
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
if __name__ == "__main__":
|
| 60 |
+
demo.launch()
|