kodetr commited on
Commit
9e26a79
·
verified ·
1 Parent(s): 39fb517

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -4
app.py CHANGED
@@ -69,6 +69,18 @@ def stream_chat(message: str, history: list, temperature: float, max_new_tokens:
69
  # -------------------------------------
70
  # ------- use model stunting V5 -------
71
  # -------------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
  terminators = [
74
  text_pipeline.tokenizer.eos_token_id,
@@ -77,7 +89,7 @@ def stream_chat(message: str, history: list, temperature: float, max_new_tokens:
77
 
78
  # Hasil dari pipeline akan berupa list dengan dictionary berisi text
79
  outputs = text_pipeline(
80
- conversation,
81
  max_new_tokens=max_new_tokens,
82
  eos_token_id=terminators,
83
  do_sample=True,
@@ -87,10 +99,12 @@ def stream_chat(message: str, history: list, temperature: float, max_new_tokens:
87
  repetition_penalty=penalty
88
  )
89
 
90
- # Karena pipeline tidak support streaming per token, kita bisa stream per kalimat atau per paragraf
91
- full_text = outputs[0]["generated_text"]
 
 
92
  buffer = ""
93
- for part in full_text.split(". "): # Stream berdasarkan kalimat
94
  buffer += part.strip() + ". "
95
  yield buffer
96
 
 
69
  # -------------------------------------
70
  # ------- use model stunting V5 -------
71
  # -------------------------------------
72
+
73
+ # Ubah ke format prompt-style string
74
+ conversation_text = ""
75
+ for turn in conversation:
76
+ role = turn["role"]
77
+ content = turn["content"]
78
+ if role == "system":
79
+ conversation_text += f"[SYSTEM]: {content}\n"
80
+ elif role == "user":
81
+ conversation_text += f"[USER]: {content}\n"
82
+ elif role == "assistant":
83
+ conversation_text += f"[ASSISTANT]: {content}\n"
84
 
85
  terminators = [
86
  text_pipeline.tokenizer.eos_token_id,
 
89
 
90
  # Hasil dari pipeline akan berupa list dengan dictionary berisi text
91
  outputs = text_pipeline(
92
+ conversation_text,
93
  max_new_tokens=max_new_tokens,
94
  eos_token_id=terminators,
95
  do_sample=True,
 
99
  repetition_penalty=penalty
100
  )
101
 
102
+ # 4. Ekstrak teks hasil dan stream per kalimat
103
+ generated_text = outputs[0].get("generated_text", "")
104
+ streamed_text = generated_text[len(conversation_text):].strip() # Hilangkan prompt awal
105
+
106
  buffer = ""
107
+ for part in streamed_text.split(". "):
108
  buffer += part.strip() + ". "
109
  yield buffer
110