Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,57 +1,67 @@
|
|
| 1 |
import os
|
| 2 |
-
from collections.abc import Iterator
|
| 3 |
-
from threading import Thread
|
| 4 |
-
|
| 5 |
-
import gradio as gr
|
| 6 |
-
import spaces
|
| 7 |
import torch
|
|
|
|
| 8 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 9 |
|
|
|
|
| 10 |
DESCRIPTION = """\
|
| 11 |
-
# Llama 3.2 3B Instruct
|
| 12 |
-
|
| 13 |
-
Llama 3.2 3B is Meta's latest iteration of open LLMs.
|
| 14 |
-
This is a demo of [`meta-llama/Llama-3.2-3B-Instruct`](https://huggingface.co/meta-llama/Llama-3.2-3B-Instruct), fine-tuned for instruction following.
|
| 15 |
-
For more details, please check [our post](https://huggingface.co/blog/llama32).
|
| 16 |
"""
|
| 17 |
|
|
|
|
| 18 |
MAX_MAX_NEW_TOKENS = 2048
|
| 19 |
-
DEFAULT_MAX_NEW_TOKENS =
|
| 20 |
-
MAX_INPUT_TOKEN_LENGTH =
|
| 21 |
|
| 22 |
-
|
|
|
|
| 23 |
|
|
|
|
| 24 |
model_id = "meta-llama/Llama-3.2-3B-Instruct"
|
|
|
|
|
|
|
| 25 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
|
|
|
|
|
|
| 26 |
model = AutoModelForCausalLM.from_pretrained(
|
| 27 |
model_id,
|
| 28 |
-
device_map=
|
| 29 |
-
torch_dtype=torch.
|
| 30 |
)
|
| 31 |
model.eval()
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
@spaces.GPU(duration=90)
|
| 35 |
def generate(
|
| 36 |
message: str,
|
| 37 |
chat_history: list[dict],
|
| 38 |
-
max_new_tokens: int =
|
| 39 |
temperature: float = 0.6,
|
| 40 |
top_p: float = 0.9,
|
| 41 |
top_k: int = 50,
|
| 42 |
repetition_penalty: float = 1.2,
|
| 43 |
-
)
|
|
|
|
| 44 |
conversation = [*chat_history, {"role": "user", "content": message}]
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
|
| 48 |
input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
|
| 49 |
-
|
| 50 |
-
input_ids = input_ids.to(
|
| 51 |
|
|
|
|
| 52 |
streamer = TextIteratorStreamer(tokenizer, timeout=20.0, skip_prompt=True, skip_special_tokens=True)
|
| 53 |
generate_kwargs = dict(
|
| 54 |
-
|
| 55 |
streamer=streamer,
|
| 56 |
max_new_tokens=max_new_tokens,
|
| 57 |
do_sample=True,
|
|
@@ -61,15 +71,17 @@ def generate(
|
|
| 61 |
num_beams=1,
|
| 62 |
repetition_penalty=repetition_penalty,
|
| 63 |
)
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
| 67 |
outputs = []
|
|
|
|
| 68 |
for text in streamer:
|
| 69 |
outputs.append(text)
|
| 70 |
yield "".join(outputs)
|
| 71 |
|
| 72 |
-
|
| 73 |
demo = gr.ChatInterface(
|
| 74 |
fn=generate,
|
| 75 |
additional_inputs=[
|
|
@@ -120,10 +132,10 @@ demo = gr.ChatInterface(
|
|
| 120 |
cache_examples=False,
|
| 121 |
type="messages",
|
| 122 |
description=DESCRIPTION,
|
| 123 |
-
css_paths="style.css",
|
| 124 |
fill_height=True,
|
| 125 |
)
|
| 126 |
|
| 127 |
-
|
| 128 |
if __name__ == "__main__":
|
|
|
|
| 129 |
demo.queue(max_size=20).launch()
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import torch
|
| 3 |
+
import gradio as gr
|
| 4 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 5 |
|
| 6 |
+
# وصف التطبيق
|
| 7 |
DESCRIPTION = """\
|
| 8 |
+
# Llama 3.2 3B Instruct (CPU-Only)
|
| 9 |
+
هذا نموذج توضيحي لـ [`meta-llama/Llama-3.2-3B-Instruct`](https://huggingface.co/meta-llama/Llama-3.2-3B-Instruct) يعمل باستخدام الـ CPU فقط.
|
|
|
|
|
|
|
|
|
|
| 10 |
"""
|
| 11 |
|
| 12 |
+
# إعداد الثوابت
|
| 13 |
MAX_MAX_NEW_TOKENS = 2048
|
| 14 |
+
DEFAULT_MAX_NEW_TOKENS = 512
|
| 15 |
+
MAX_INPUT_TOKEN_LENGTH = 4096 # الحد الأقصى لعدد التوكنات في المدخلات
|
| 16 |
|
| 17 |
+
# تحديد الجهاز: استخدام CPU فقط
|
| 18 |
+
device = torch.device("cpu")
|
| 19 |
|
| 20 |
+
# تحديد معرف النموذج وتحميله
|
| 21 |
model_id = "meta-llama/Llama-3.2-3B-Instruct"
|
| 22 |
+
|
| 23 |
+
# تحميل التوكن الخاص بالنموذج
|
| 24 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 25 |
+
|
| 26 |
+
# تحميل النموذج على CPU مع استخدام torch.float32
|
| 27 |
model = AutoModelForCausalLM.from_pretrained(
|
| 28 |
model_id,
|
| 29 |
+
device_map=None, # عدم استخدام GPU
|
| 30 |
+
torch_dtype=torch.float32
|
| 31 |
)
|
| 32 |
model.eval()
|
| 33 |
+
model.to(device)
|
| 34 |
|
|
|
|
|
|
|
| 35 |
def generate(
|
| 36 |
message: str,
|
| 37 |
chat_history: list[dict],
|
| 38 |
+
max_new_tokens: int = DEFAULT_MAX_NEW_TOKENS,
|
| 39 |
temperature: float = 0.6,
|
| 40 |
top_p: float = 0.9,
|
| 41 |
top_k: int = 50,
|
| 42 |
repetition_penalty: float = 1.2,
|
| 43 |
+
):
|
| 44 |
+
# دمج سجل المحادثة مع الرسالة الجديدة
|
| 45 |
conversation = [*chat_history, {"role": "user", "content": message}]
|
| 46 |
+
|
| 47 |
+
# تحويل المحادثة إلى مدخلات للنموذج
|
| 48 |
+
inputs = tokenizer.apply_chat_template(
|
| 49 |
+
conversation,
|
| 50 |
+
add_generation_prompt=True,
|
| 51 |
+
return_tensors="pt"
|
| 52 |
+
)
|
| 53 |
+
input_ids = inputs["input_ids"]
|
| 54 |
+
|
| 55 |
+
# قص التوكنز إذا تجاوز طولها الحد المسموح
|
| 56 |
if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
|
| 57 |
input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
|
| 58 |
+
|
| 59 |
+
input_ids = input_ids.to(device)
|
| 60 |
|
| 61 |
+
# إعداد البث التدريجي للنص باستخدام TextIteratorStreamer
|
| 62 |
streamer = TextIteratorStreamer(tokenizer, timeout=20.0, skip_prompt=True, skip_special_tokens=True)
|
| 63 |
generate_kwargs = dict(
|
| 64 |
+
input_ids=input_ids,
|
| 65 |
streamer=streamer,
|
| 66 |
max_new_tokens=max_new_tokens,
|
| 67 |
do_sample=True,
|
|
|
|
| 71 |
num_beams=1,
|
| 72 |
repetition_penalty=repetition_penalty,
|
| 73 |
)
|
| 74 |
+
|
| 75 |
+
# تشغيل عملية التوليد على نفس الخيط (CPU)
|
| 76 |
+
model.generate(**generate_kwargs)
|
| 77 |
+
|
| 78 |
outputs = []
|
| 79 |
+
# بث النص تدريجيًا أثناء توليد النموذج
|
| 80 |
for text in streamer:
|
| 81 |
outputs.append(text)
|
| 82 |
yield "".join(outputs)
|
| 83 |
|
| 84 |
+
# إنشاء واجهة الدردشة باستخدام Gradio
|
| 85 |
demo = gr.ChatInterface(
|
| 86 |
fn=generate,
|
| 87 |
additional_inputs=[
|
|
|
|
| 132 |
cache_examples=False,
|
| 133 |
type="messages",
|
| 134 |
description=DESCRIPTION,
|
| 135 |
+
css_paths="style.css", # تأكدي من رفع ملف style.css إذا كان موجوداً
|
| 136 |
fill_height=True,
|
| 137 |
)
|
| 138 |
|
|
|
|
| 139 |
if __name__ == "__main__":
|
| 140 |
+
# استخدام queue() لإدارة الطلبات المتزامنة
|
| 141 |
demo.queue(max_size=20).launch()
|