Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
| 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(
|
| 63 |
gr.Markdown("# ๐ ONYX AI - Turbo CTranslate2 Mode")
|
| 64 |
-
gr.Markdown(f"**Eng. Rawan**, ONYX is now optimized
|
| 65 |
gr.ChatInterface(fn=chat_onyx)
|
| 66 |
|
| 67 |
if __name__ == "__main__":
|
| 68 |
-
|
|
|
|
|
|
| 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())
|