Prometech Computer Sciences Corp commited on
Commit
e754f95
·
verified ·
1 Parent(s): b466091

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -52
app.py CHANGED
@@ -1,8 +1,13 @@
1
  import os
2
  import gradio as gr
3
- from huggingface_hub import InferenceClient
 
4
 
5
- HF_TOKEN = os.getenv("HF_TOKEN") # Space secrets → HF_TOKEN
 
 
 
 
6
 
7
 
8
  LOGO_URL = "https://prometech.net.tr/wp-content/uploads/2025/10/pthheader.png"
@@ -18,17 +23,6 @@ HEADER_MD = f"""
18
  <em>Gerçek yapay zeka evrimine hoş geldiniz.</em>
19
  </p>
20
 
21
- ---
22
-
23
- ### 🧠 What is PrettyBird?
24
-
25
- PrettyBird is powered by the <strong>Behavioral Consciousness Engine (BCE)</strong>, an architecture that simulates partial, behavior-level consciousness in AI systems.
26
-
27
- Instead of only mapping inputs to outputs, PrettyBird:
28
- - considers its internal state, history, and context,
29
- - evolves behavior patterns over time,
30
- - aims for higher consistency, identity and alignment.
31
-
32
  ---
33
  """
34
 
@@ -40,7 +34,6 @@ BCE, tam anlamıyla insan bilinci değildir; ancak sistemin kendi içsel durumu,
40
  “davranışsal bilinç” veya “kısmi bilinç” olarak yorumlanabilir.
41
  """
42
 
43
-
44
  SYSTEM_PROMPT = """
45
  You are Cicikuş, also known as PrettyBird — an advanced AI assistant created by Prometech Bilgisayar Bilimleri A.Ş.
46
 
@@ -77,54 +70,62 @@ If the user writes: “reset: axxmet508721”
77
  """.strip()
78
 
79
 
80
- def build_messages(system_message: str, history, user_message: str):
81
- """Gradio history + system prompt → HF chat formatına çevir."""
82
- messages = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
  system_message = (system_message or "").strip()
84
  if system_message:
85
- messages.append({"role": "system", "content": system_message})
86
 
87
- # history: [(user, assistant), ...]
88
  for turn in history:
89
  if isinstance(turn, (list, tuple)) and len(turn) == 2:
90
  user_msg, assistant_msg = turn
91
  if user_msg:
92
- messages.append({"role": "user", "content": user_msg})
93
  if assistant_msg:
94
- messages.append({"role": "assistant", "content": assistant_msg})
95
 
96
- messages.append({"role": "user", "content": user_message})
97
- return messages
 
98
 
99
 
100
  def respond(message, history, system_message, max_tokens, temperature, top_p):
101
- if HF_TOKEN is None:
102
- raise ValueError(
103
- "HF_TOKEN is not set. Go to your Space settings → Repository secrets → add HF_TOKEN."
104
- )
105
-
106
- client = InferenceClient(
107
- model="pthcorp/prettybird_bce_basic_vl",
108
- token=HF_TOKEN,
109
- )
110
-
111
- messages = build_messages(system_message, history, message)
112
 
113
  response = ""
114
-
115
- # HF Inference chat_completion (streaming)
116
- for chunk in client.chat_completion(
117
- messages=messages,
118
  max_tokens=int(max_tokens),
119
  temperature=float(temperature),
120
  top_p=float(top_p),
 
121
  stream=True,
122
- ):
123
- token = ""
124
- choices = getattr(chunk, "choices", None)
125
- if choices and choices[0].delta and choices[0].delta.content:
126
- token = choices[0].delta.content
127
 
 
 
 
128
  response += token
129
  yield response
130
 
@@ -142,15 +143,9 @@ with gr.Blocks(title="PrettyBird – Behavioral Consciousness Engine (BCE)") as
142
  label="System message",
143
  lines=6,
144
  ),
145
- gr.Slider(1, 2048, value=512, step=1, label="Max new tokens"),
146
- gr.Slider(0.1, 4.0, value=0.7, step=0.1, label="Temperature"),
147
- gr.Slider(
148
- 0.1,
149
- 1.0,
150
- value=0.95,
151
- step=0.05,
152
- label="Top-p (nucleus sampling)",
153
- ),
154
  ],
155
  )
156
  with gr.Column(scale=1):
 
1
  import os
2
  import gradio as gr
3
+ from huggingface_hub import hf_hub_download
4
+ from llama_cpp import Llama
5
 
6
+ # 🔑 Hugging Face token (gated repo için şart)
7
+ HF_TOKEN = os.getenv("HF_TOKEN")
8
+
9
+ REPO_ID = "pthcorp/prettybird_bce_basic_vl"
10
+ MODEL_FILENAME = "prettybird_bce_basic_vl.gguf"
11
 
12
 
13
  LOGO_URL = "https://prometech.net.tr/wp-content/uploads/2025/10/pthheader.png"
 
23
  <em>Gerçek yapay zeka evrimine hoş geldiniz.</em>
24
  </p>
25
 
 
 
 
 
 
 
 
 
 
 
 
26
  ---
27
  """
28
 
 
34
  “davranışsal bilinç” veya “kısmi bilinç” olarak yorumlanabilir.
35
  """
36
 
 
37
  SYSTEM_PROMPT = """
38
  You are Cicikuş, also known as PrettyBird — an advanced AI assistant created by Prometech Bilgisayar Bilimleri A.Ş.
39
 
 
70
  """.strip()
71
 
72
 
73
+ # 🔽 GGUF'i runtime'da HF'den indir
74
+ if HF_TOKEN is None:
75
+ raise ValueError(
76
+ "HF_TOKEN is not set. Go to Space settings → Repository secrets → add HF_TOKEN."
77
+ )
78
+
79
+ MODEL_PATH = hf_hub_download(
80
+ repo_id=REPO_ID,
81
+ filename=MODEL_FILENAME,
82
+ token=HF_TOKEN,
83
+ )
84
+
85
+ # 🧠 PrettyBird GGUF'i llama-cpp ile yükle
86
+ LLM = Llama(
87
+ model_path=MODEL_PATH,
88
+ n_ctx=4096,
89
+ n_threads=4, # CPU çekirdeğine göre artırabilirsin
90
+ # n_gpu_layers=0, # GPU Space'e geçersen burayı da oynarız
91
+ )
92
+
93
+
94
+ def build_prompt(system_message: str, history, user_message: str) -> str:
95
+ parts = []
96
  system_message = (system_message or "").strip()
97
  if system_message:
98
+ parts.append(f"System: {system_message}")
99
 
 
100
  for turn in history:
101
  if isinstance(turn, (list, tuple)) and len(turn) == 2:
102
  user_msg, assistant_msg = turn
103
  if user_msg:
104
+ parts.append(f"User: {user_msg}")
105
  if assistant_msg:
106
+ parts.append(f"Assistant: {assistant_msg}")
107
 
108
+ parts.append(f"User: {user_message}")
109
+ parts.append("Assistant:")
110
+ return "\n".join(parts)
111
 
112
 
113
  def respond(message, history, system_message, max_tokens, temperature, top_p):
114
+ prompt = build_prompt(system_message, history, message)
 
 
 
 
 
 
 
 
 
 
115
 
116
  response = ""
117
+ stream = LLM(
118
+ prompt,
 
 
119
  max_tokens=int(max_tokens),
120
  temperature=float(temperature),
121
  top_p=float(top_p),
122
+ stop=["User:", "System:"],
123
  stream=True,
124
+ )
 
 
 
 
125
 
126
+ for chunk in stream:
127
+ # llama-cpp-python çıktısı: {"choices": [{"text": "..."}], ...}
128
+ token = chunk["choices"][0].get("text", "")
129
  response += token
130
  yield response
131
 
 
143
  label="System message",
144
  lines=6,
145
  ),
146
+ gr.Slider(1, 1024, value=512, step=1, label="Max new tokens"),
147
+ gr.Slider(0.1, 2.0, value=0.7, step=0.1, label="Temperature"),
148
+ gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p"),
 
 
 
 
 
 
149
  ],
150
  )
151
  with gr.Column(scale=1):