Bsbhdjdoehe commited on
Commit
74ea8ff
·
verified ·
1 Parent(s): 38e3dea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -40
app.py CHANGED
@@ -3,77 +3,96 @@ import gradio as gr
3
  from huggingface_hub import InferenceClient
4
 
5
  # ------------------------------------------------------------------------------
6
- # NetKurd Engineering AI - وەشانی چاککراو بۆ Gradio 6.0
7
  # ------------------------------------------------------------------------------
8
 
9
- # بەکارهێنانی مۆدێلی Mistral کە زۆر جێگیرە و کێشەی مۆڵەتی نییە
10
  client = InferenceClient(
11
- model="mistralai/Mistral-7B-Instruct-v0.3",
12
  token=os.environ.get("HF_TOKEN"),
13
  )
14
 
 
15
  SYSTEM_PROMPT = (
16
- "تۆ پڕۆفیسۆر و ئەندازیارێکی پسپۆڕیت بە ناوی NetKurd. "
17
  "تەنیا بە زمانی کوردی سۆرانی وەڵامی پرسیارەکان دەدەیتەوە. "
18
- "وەڵامەکانت دەبێت زانستی، ورد، و پیشەیی بن. "
19
- "شارەزایت لە Godot 4، پایتۆن، و فیزیکدا هەیە."
20
  )
21
 
22
- CUSTOM_CSS = """
23
- #header { text-align: center; margin-bottom: 25px; border-bottom: 2px solid #e2e8f0; padding-bottom: 10px; }
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
- for user_msg, assistant_msg in history:
33
- if user_msg: messages.append({"role": "user", "content": user_msg})
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.5,
 
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
- # دروستکردنی ڕووکار بە بەکارهێنانی Blocks
55
- with gr.Blocks(css=CUSTOM_CSS) as demo:
56
- with gr.Row(elem_id="header"):
57
- gr.HTML("<div><h1>🛠️ NetKurd Engineering AI</h1><p>یاریدەدەری زیرەکی ئەندازیاری - تایبەت بە فەرماندە</p></div>")
 
 
58
 
59
  with gr.Row():
60
- with gr.Column(scale=1, elem_classes="sidebar"):
61
  gr.Markdown("""
62
- ### 🛠️ تواناکانی نێتکورد
63
- * **Godot 4**: یارمەتی لە دروستکردنی یاریی.
64
- * **Engineering**: شیکاری زانستی و تەکنیکی.
65
- * **Clean Code**: نووسینی کۆد بە زمانی جیاواز.
66
  """)
67
  clear_btn = gr.Button("🗑️ پاککردنەوەی چات", variant="secondary")
68
 
69
  with gr.Column(scale=3):
70
- # لابردنی type="messages" چونکە لە وەشانی نوێدا ئیرۆر دەدات
71
- chatbot = gr.Chatbot(label="NetKurd Terminal", height=500)
72
- chat_interface = gr.ChatInterface(
73
- fn=respond_generator,
74
- chatbot=chatbot,
75
- theme="soft", # گواستنەوەی تێم بۆ ئێرە
76
- )
77
 
78
- if __name__ == "__main__":
79
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ )