ONYX-APP commited on
Commit
275ffa9
ยท
verified ยท
1 Parent(s): 171e659

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -9
app.py CHANGED
@@ -20,11 +20,12 @@ except Exception as e:
20
  print(f"--- โŒ Download Failed: {e} ---")
21
  model_path = kagglehub.model_download("ruicompany/onyx-gpt2large/transformers/default")
22
 
23
- # ๐Ÿ›  ุชุญูˆูŠู„ ุงู„ู…ูˆุฏูŠู„ ู„ุตูŠุบุฉ CTranslate2 (ุชู†ูุฐ ู…ุฑุฉ ูˆุงุญุฏุฉ ุนู†ุฏ ุงู„ุชุดุบูŠู„)
24
  ct2_model_path = "onyx_ct2_model"
25
  if not os.path.exists(ct2_model_path):
26
  print("--- โš™๏ธ Converting model to CTranslate2 format (int8) ---")
27
  converter = ctranslate2.converters.TransformersConverter(model_path)
 
28
  converter.convert(ct2_model_path, quantization="int8", force=True)
29
 
30
  # ุชุญู…ูŠู„ ุงู„ู€ Tokenizer ูˆุงู„ู€ Generator
@@ -32,7 +33,7 @@ tokenizer = transformers.AutoTokenizer.from_pretrained(model_path)
32
  generator = ctranslate2.Generator(ct2_model_path, device="cpu")
33
 
34
  def chat_onyx(message, history):
35
- # ุจู†ุงุก ุงู„ู€ Prompt ู…ุน ุชู‚ู„ูŠุต ุงู„ุฐุงูƒุฑุฉ ู„ุณุฑุนุฉ ุงู„ุงุณุชุฌุงุจุฉ
36
  recent_history = history[-3:] if len(history) > 3 else history
37
  prompt = ""
38
  for user_msg, assistant_msg in recent_history:
@@ -42,14 +43,13 @@ def chat_onyx(message, history):
42
  # ุชุญูˆูŠู„ ุงู„ู†ุต ู„ู€ Tokens
43
  tokens = tokenizer.convert_ids_to_tokens(tokenizer.encode(prompt))
44
 
45
- # ุงู„ู€ Streaming ุจุงุณุชุฎุฏุงู… Generator ุงู„ุฎุงุต ุจู€ CTranslate2
46
  step_results = generator.generate_tokens(
47
  tokens,
48
  max_length=150,
49
  sampling_topk=40,
50
  sampling_temperature=0.7,
51
- end_token=tokenizer.eos_token,
52
- include_prompt_in_result=False
53
  )
54
 
55
  partial_text = ""
@@ -58,11 +58,12 @@ def chat_onyx(message, history):
58
  partial_text += word
59
  yield partial_text
60
 
61
- # ูˆุงุฌู‡ุฉ Gradio ุงู„ุงุญุชุฑุงููŠุฉ
62
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
63
  gr.Markdown("# ๐Ÿ’Ž ONYX AI - Turbo CTranslate2 Mode")
64
- gr.Markdown(f"**Eng. Rawan**, ONYX is now optimized with CTranslate2 & INT8 Quantization.")
65
  gr.ChatInterface(fn=chat_onyx)
66
 
67
  if __name__ == "__main__":
68
- demo.launch()
 
 
20
  print(f"--- โŒ Download Failed: {e} ---")
21
  model_path = kagglehub.model_download("ruicompany/onyx-gpt2large/transformers/default")
22
 
23
+ # ๐Ÿ›  ุชุญูˆูŠู„ ุงู„ู…ูˆุฏูŠู„ ู„ุตูŠุบุฉ CTranslate2
24
  ct2_model_path = "onyx_ct2_model"
25
  if not os.path.exists(ct2_model_path):
26
  print("--- โš™๏ธ Converting model to CTranslate2 format (int8) ---")
27
  converter = ctranslate2.converters.TransformersConverter(model_path)
28
+ # ุชุญุฏูŠุซ ุงู„ู€ compute_type ู„ู€ int8 ู„ูŠุชู†ุงุณุจ ู…ุน ุงู„ู€ CPU ูˆูŠุฎุชููŠ ุงู„ู€ Warning
29
  converter.convert(ct2_model_path, quantization="int8", force=True)
30
 
31
  # ุชุญู…ูŠู„ ุงู„ู€ Tokenizer ูˆุงู„ู€ Generator
 
33
  generator = ctranslate2.Generator(ct2_model_path, device="cpu")
34
 
35
  def chat_onyx(message, history):
36
+ # ุจู†ุงุก ุงู„ู€ Prompt ู…ุน ุชู‚ู„ูŠุต ุงู„ุฐุงูƒุฑุฉ
37
  recent_history = history[-3:] if len(history) > 3 else history
38
  prompt = ""
39
  for user_msg, assistant_msg in recent_history:
 
43
  # ุชุญูˆูŠู„ ุงู„ู†ุต ู„ู€ Tokens
44
  tokens = tokenizer.convert_ids_to_tokens(tokenizer.encode(prompt))
45
 
46
+ # ๐Ÿš€ ุงู„ุชุนุฏูŠู„ ู‡ู†ุง: ุญุฐู include_prompt_in_result ู„ุฃู†ู‡ ุบูŠุฑ ู…ุฏุนูˆู… ููŠ generate_tokens
47
  step_results = generator.generate_tokens(
48
  tokens,
49
  max_length=150,
50
  sampling_topk=40,
51
  sampling_temperature=0.7,
52
+ end_token=tokenizer.eos_token
 
53
  )
54
 
55
  partial_text = ""
 
58
  partial_text += word
59
  yield partial_text
60
 
61
+ # ูˆุงุฌู‡ุฉ Gradio
62
+ with gr.Blocks() as demo: # ุดู„ู†ุง ุงู„ุซูŠู… ู…ู† ู‡ูˆู† ู„ุชุฌู†ุจ ุงู„ุชู†ุจูŠู‡
63
  gr.Markdown("# ๐Ÿ’Ž ONYX AI - Turbo CTranslate2 Mode")
64
+ gr.Markdown(f"**Eng. Rawan**, ONYX is now optimized and stable.")
65
  gr.ChatInterface(fn=chat_onyx)
66
 
67
  if __name__ == "__main__":
68
+ # ู†ู‚ู„ู†ุง ุงู„ุซูŠู… ู„ู‡ูˆู† ุญุณุจ ุชุนู„ูŠู…ุงุช Gradio 6.0
69
+ demo.launch(theme=gr.themes.Soft())