DD8777 commited on
Commit
bf09cd6
·
verified ·
1 Parent(s): 25a5501

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -31
app.py CHANGED
@@ -1,52 +1,38 @@
1
  import gradio as gr
2
- from huggingface_hub import hf_hub_download
3
- from llama_cpp import Llama
4
 
5
- # 1. הגדרת המודל מ-Hugging Face (למשל מודל Qwen 3.5 או DictaLM בפורמט GGUF)
6
- REPO_ID = "Qwen/Qwen2.5-Coder-7B-Instruct-GGUF" # שם ה-Repository שבו נמצא קובץ ה-GGUF
7
- FILENAME = "qwen2.5-coder-7b-instruct-q4_k_m.gguf" # שם הקובץ הספציפי
8
 
9
- # הורדת קובץ ה-GGUF אל הזיכרון הטרמינלי של ה-Space
10
- model_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
 
11
 
12
- # 2. טעינת המודל לזיכרון ה-CPU
13
- llm = Llama(
14
- model_path=model_path,
15
- n_ctx=2048, # גודל חלון ההקשר (Context Window)
16
- n_threads=2, # ניצול 2 הליבות החינמיות של ה-Space
17
- )
18
-
19
- # 3. פונקציית המענה למשתמש
20
  def answer(message, history):
21
  messages = []
22
-
23
- # תמיכה במבנה ההיסטוריה
24
  for item in history:
25
  if isinstance(item, dict):
26
  messages.append(item)
27
  elif isinstance(item, (list, tuple)) and len(item) == 2:
28
  u_msg, b_msg = item
29
- if u_msg:
30
- messages.append({"role": "user", "content": u_msg})
31
- if b_msg:
32
- messages.append({"role": "assistant", "content": b_msg})
33
 
34
  messages.append({"role": "user", "content": message})
35
 
36
- # יצירת התשובה דרך llama.cpp
37
- response = llm.create_chat_completion(
38
- messages=messages,
39
- max_tokens=512,
40
- temperature=0.7
41
- )
42
 
43
- return response["choices"][0]["message"]["content"]
44
 
45
- # 4. ממשק צ'אט בסיסי
46
  demo = gr.ChatInterface(
47
  fn=answer,
48
- title="GGUF CPU Assistant",
49
- description="הרצת מודל GGUF מהירה על CPU בחינם"
50
  )
51
 
52
  if __name__ == "__main__":
 
1
  import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
 
3
 
4
+ # מזהה המאגר ושם קובץ ה-GGUF
5
+ MODEL_ID = "Qwen/Qwen2.5-Coder-7B-Instruct-GGUF"
6
+ GGUF_FILE = "qwen2.5-coder-7b-instruct-q4_k_m.gguf"
7
 
8
+ # טעינת הטוקנייזר והמודל בפורמט GGUF
9
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, gguf_file=GGUF_FILE)
10
+ model = AutoModelForCausalLM.from_pretrained(MODEL_ID, gguf_file=GGUF_FILE)
11
 
 
 
 
 
 
 
 
 
12
  def answer(message, history):
13
  messages = []
 
 
14
  for item in history:
15
  if isinstance(item, dict):
16
  messages.append(item)
17
  elif isinstance(item, (list, tuple)) and len(item) == 2:
18
  u_msg, b_msg = item
19
+ if u_msg: messages.append({"role": "user", "content": u_msg})
20
+ if b_msg: messages.append({"role": "assistant", "content": b_msg})
 
 
21
 
22
  messages.append({"role": "user", "content": message})
23
 
24
+ prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
25
+ inputs = tokenizer(prompt, return_tensors="pt")
26
+
27
+ outputs = model.generate(**inputs, max_new_tokens=512, temperature=0.7)
28
+ response = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
 
29
 
30
+ return response
31
 
 
32
  demo = gr.ChatInterface(
33
  fn=answer,
34
+ title="Qwen GGUF Assistant",
35
+ description="הרצת GGUF נקייה ומהירה על CPU"
36
  )
37
 
38
  if __name__ == "__main__":