Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,77 +3,96 @@ import gradio as gr
|
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
# ------------------------------------------------------------------------------
|
| 6 |
-
# NetKurd Engineering AI - وەشانی
|
| 7 |
# ------------------------------------------------------------------------------
|
| 8 |
|
| 9 |
-
# بەکارهێنانی مۆدێلی
|
| 10 |
client = InferenceClient(
|
| 11 |
-
model="
|
| 12 |
token=os.environ.get("HF_TOKEN"),
|
| 13 |
)
|
| 14 |
|
|
|
|
| 15 |
SYSTEM_PROMPT = (
|
| 16 |
-
"تۆ پڕۆفیسۆر و ئەندازیارێکی
|
| 17 |
"تەنیا بە زمانی کوردی سۆرانی وەڵامی پرسیارەکان دەدەیتەوە. "
|
| 18 |
-
"وەڵامەکانت دەبێت زانستی، ورد، و پ
|
| 19 |
-
"
|
| 20 |
)
|
| 21 |
|
| 22 |
-
|
| 23 |
-
#
|
| 24 |
-
#header h1 { font-size: 2.8em; color: #1e40af; font-weight: bold; }
|
| 25 |
-
.sidebar { background: #f1f5f9; padding: 20px; border-radius: 12px; border: 1px solid #cbd5e1; }
|
| 26 |
-
"""
|
| 27 |
-
|
| 28 |
-
def respond_generator(message, history):
|
| 29 |
-
# ناردنی پێناسەی سیستەم و مێژووی چات
|
| 30 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
if assistant_msg: messages.append({"role": "assistant", "content": assistant_msg})
|
| 35 |
-
|
| 36 |
-
messages.append({"role": "user", "content": message})
|
| 37 |
|
| 38 |
full_response = ""
|
| 39 |
try:
|
| 40 |
stream = client.chat_completion(
|
| 41 |
messages=messages,
|
| 42 |
max_tokens=2048,
|
| 43 |
-
temperature=0.
|
|
|
|
| 44 |
stream=True,
|
| 45 |
)
|
| 46 |
for chunk in stream:
|
| 47 |
delta = chunk.choices[0].delta.content
|
| 48 |
if delta:
|
| 49 |
full_response += delta
|
| 50 |
-
yield full_response
|
|
|
|
|
|
|
| 51 |
except Exception as e:
|
| 52 |
-
yield f"ببوورە فەرماندە، کێشەیەک ڕوویدا: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
| 58 |
|
| 59 |
with gr.Row():
|
| 60 |
-
with gr.Column(scale=1
|
| 61 |
gr.Markdown("""
|
| 62 |
-
### 🛠️
|
| 63 |
-
* **Godot 4**: یارمەتی لە دروستکردنی یاریی.
|
| 64 |
-
* **
|
| 65 |
-
* **
|
| 66 |
""")
|
| 67 |
clear_btn = gr.Button("🗑️ پاککردنەوەی چات", variant="secondary")
|
| 68 |
|
| 69 |
with gr.Column(scale=3):
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
theme="soft", # گواستنەوەی تێم بۆ ئێرە
|
| 76 |
-
)
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
# ------------------------------------------------------------------------------
|
| 6 |
+
# NetKurd Engineering AI - وەشانی پڕۆفیشناڵ (Gradio 6.0 Optimized)
|
| 7 |
# ------------------------------------------------------------------------------
|
| 8 |
|
| 9 |
+
# بەکارهێنانی مۆدێلی Llama-3 کە بۆ زانست و ئەندازیاری بێوێنەیە
|
| 10 |
client = InferenceClient(
|
| 11 |
+
model="meta-llama/Meta-Llama-3-8B-Instruct",
|
| 12 |
token=os.environ.get("HF_TOKEN"),
|
| 13 |
)
|
| 14 |
|
| 15 |
+
# پێناسی نێتکورد بە زمانی سۆرانی (دەستکاری کراوە بۆ ئەندازیاری)
|
| 16 |
SYSTEM_PROMPT = (
|
| 17 |
+
"تۆ پڕۆفیسۆر و ئەندازیارێکی زیرەکیت بە ناوی NetKurd. "
|
| 18 |
"تەنیا بە زمانی کوردی سۆرانی وەڵامی پرسیارەکان دەدەیتەوە. "
|
| 19 |
+
"وەڵامەکانت دەبێت زانستی، ورد، و ڕێکوپێک بن. "
|
| 20 |
+
"لە بوارەکانی فیزیک، میکانیک، و گەشەپێدانی یارییەکاندا (بەتایبەت Godot 4) زۆر شارەزایت."
|
| 21 |
)
|
| 22 |
|
| 23 |
+
def respond(user_message: str, history_state: list):
|
| 24 |
+
# دروستکردنی مێژووی چات
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 26 |
+
for msg in history_state:
|
| 27 |
+
messages.append(msg)
|
| 28 |
|
| 29 |
+
messages.append({"role": "user", "content": user_message})
|
| 30 |
+
history_state.append({"role": "user", "content": user_message})
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
full_response = ""
|
| 33 |
try:
|
| 34 |
stream = client.chat_completion(
|
| 35 |
messages=messages,
|
| 36 |
max_tokens=2048,
|
| 37 |
+
temperature=0.4,
|
| 38 |
+
top_p=0.9,
|
| 39 |
stream=True,
|
| 40 |
)
|
| 41 |
for chunk in stream:
|
| 42 |
delta = chunk.choices[0].delta.content
|
| 43 |
if delta:
|
| 44 |
full_response += delta
|
| 45 |
+
yield full_response, history_state
|
| 46 |
+
|
| 47 |
+
history_state.append({"role": "assistant", "content": full_response})
|
| 48 |
except Exception as e:
|
| 49 |
+
yield f"ببوورە فەرماندە، کێشەیەک ڕوویدا: {str(e)}", history_state
|
| 50 |
+
|
| 51 |
+
# دروستکردنی ڕووکاری ئەندازیاری
|
| 52 |
+
with gr.Blocks() as demo:
|
| 53 |
+
messages_state = gr.State(value=[])
|
| 54 |
|
| 55 |
+
gr.HTML("""
|
| 56 |
+
<div style="text-align: center; padding: 20px; border-bottom: 2px solid #e2e8f0;">
|
| 57 |
+
<h1 style="color: #1e40af; font-size: 2.5em;">🛠️ NetKurd Engineering AI</h1>
|
| 58 |
+
<p style="color: #475569; font-size: 1.1em;">یاریدەدەری زیرەکی ئەندازیاری - وەشانی تایبەت بە فەرماندە</p>
|
| 59 |
+
</div>
|
| 60 |
+
""")
|
| 61 |
|
| 62 |
with gr.Row():
|
| 63 |
+
with gr.Column(scale=1):
|
| 64 |
gr.Markdown("""
|
| 65 |
+
### 🛠️ پسپۆڕییەکانی نێتکورد
|
| 66 |
+
* **Godot 4**: یارمەتی لە دروستکردنی یاریی و نووسینی سکریپت.
|
| 67 |
+
* **فیزیک و میکانیک**: شیکارکردنی کێشە زانستییەکان.
|
| 68 |
+
* **ئەندازیاری نەرمەکاڵا**: باشترکردنی کۆد و ئەلگۆریتم.
|
| 69 |
""")
|
| 70 |
clear_btn = gr.Button("🗑️ پاککردنەوەی چات", variant="secondary")
|
| 71 |
|
| 72 |
with gr.Column(scale=3):
|
| 73 |
+
chatbot = gr.Chatbot(label="NetKurd Terminal", height=550)
|
| 74 |
+
user_input = gr.Textbox(placeholder="پرسیارەکەت لێرە بنووسە...", show_label=False)
|
| 75 |
+
with gr.Row():
|
| 76 |
+
submit_btn = gr.Button("ناردن 🚀", variant="primary")
|
| 77 |
+
stop_btn = gr.Button("ڕاگرتن", variant="stop")
|
|
|
|
|
|
|
| 78 |
|
| 79 |
+
def handle_submit(user_msg, msg_state):
|
| 80 |
+
# Generator بۆ وەڵامدانەوەی خێرا
|
| 81 |
+
for response, updated_state in respond(user_msg, msg_state):
|
| 82 |
+
# گۆڕینی وەڵام بۆ فۆرماتی Chatbot
|
| 83 |
+
chat_history = []
|
| 84 |
+
for i in range(0, len(updated_state), 2):
|
| 85 |
+
user = updated_state[i]["content"]
|
| 86 |
+
assistant = updated_state[i+1]["content"] if i+1 < len(updated_state) else response
|
| 87 |
+
chat_history.append((user, assistant))
|
| 88 |
+
yield chat_history, updated_state
|
| 89 |
+
|
| 90 |
+
user_input.submit(handle_submit, [user_input, messages_state], [chatbot, messages_state]).then(lambda: "", None, user_input)
|
| 91 |
+
submit_btn.click(handle_submit, [user_input, messages_state], [chatbot, messages_state]).then(lambda: "", None, user_input)
|
| 92 |
+
clear_btn.click(lambda: ([], []), None, [chatbot, messages_state])
|
| 93 |
+
|
| 94 |
+
# لێرەدا ڕەنگ و ستایلەکان دیاری دەکەین (تایبەت بە Gradio 6.0)
|
| 95 |
+
demo.launch(
|
| 96 |
+
theme=gr.themes.Soft(primary_hue="blue", secondary_hue="slate"),
|
| 97 |
+
css=".gradio-container { background-color: #f8fafc; } .sidebar { padding: 15px; }"
|
| 98 |
+
)
|