heigon77 commited on
Commit
6bfa3c7
Β·
1 Parent(s): b8ec716

Downloading LLM in HF

Browse files
Files changed (3) hide show
  1. Dockerfile +0 -1
  2. main.py +27 -33
  3. requirements.txt +3 -1
Dockerfile CHANGED
@@ -23,7 +23,6 @@ RUN pip install --no-cache-dir -r requirements.txt
23
  COPY main.py .
24
  COPY yolo_deart.onnx .
25
  COPY style_classifier.onnx .
26
- COPY tinyllama.gguf .
27
 
28
  # ─────────────────────────────────────────────
29
  # HF / LLM performance config
 
23
  COPY main.py .
24
  COPY yolo_deart.onnx .
25
  COPY style_classifier.onnx .
 
26
 
27
  # ─────────────────────────────────────────────
28
  # HF / LLM performance config
main.py CHANGED
@@ -12,6 +12,7 @@ from fastapi.responses import JSONResponse
12
 
13
  from ultralytics import YOLO
14
  from torchvision import transforms
 
15
  from llama_cpp import Llama
16
 
17
  from ctransformers import AutoModelForCausalLM
@@ -26,7 +27,11 @@ import re
26
 
27
  YOLO_PATH = Path("yolo_deart.onnx")
28
  STYLE_MODEL_PATH = Path("style_classifier.onnx")
29
- LLM_PATH = "phi3_mini_4k_q4.gguf"
 
 
 
 
30
 
31
  FRONTEND_ORIGIN = os.environ.get(
32
  "FRONTEND_ORIGIN",
@@ -75,13 +80,11 @@ STYLES = [
75
  # LLM (phi3 GGUF)
76
  # ─────────────────────────────────────────────
77
 
 
78
  llm = Llama(
79
  model_path=LLM_PATH,
80
- n_ctx=4096,
81
- n_threads=8,
82
- n_batch=256,
83
- verbose=False,
84
- use_mlock=False
85
  )
86
 
87
  def generate_poem(detections, style):
@@ -92,40 +95,31 @@ def generate_poem(detections, style):
92
  for k, v in counts.items()
93
  ) or "silence"
94
 
95
- prompt = f"""
96
- You are a world-class art poet.
97
-
98
- Write ONLY a poem based on an artwork with the following characteristics:
99
-
100
- Style: {style["style_name"]}
101
- Objects: {objects}
102
 
103
- Rules:
104
- - only poem
105
- - Shakespearean English Sonnet (14 lines, iambic pentameter, ABAB CDCD EFEF GG)
106
- - no explanation
107
- - no repetition of prompt
108
- - metaphorical, emotional, artistic
109
-
110
- Ouput expected:
111
-
112
- - Response: formatted poem text only, no markdown, no code blocks, no extra text.
113
-
114
- """
115
-
116
- print("LLM PROMPT:\n", prompt)
117
 
118
  output = llm(
119
  prompt,
120
- max_tokens=200,
121
- temperature=0.8,
122
  top_p=0.9,
123
- repeat_penalty=1.2
 
124
  )
125
 
126
- print("LLM RAW OUTPUT:", output)
127
-
128
- return output["choices"][0]["text"]
129
 
130
  def format_poem(text: str, max_lines: int = 17):
131
  # ── 1. Remove prefixos tipo [response]: ou "response:"
 
12
 
13
  from ultralytics import YOLO
14
  from torchvision import transforms
15
+ from huggingface_hub import hf_hub_download
16
  from llama_cpp import Llama
17
 
18
  from ctransformers import AutoModelForCausalLM
 
27
 
28
  YOLO_PATH = Path("yolo_deart.onnx")
29
  STYLE_MODEL_PATH = Path("style_classifier.onnx")
30
+
31
+ LLM_PATH = hf_hub_download(
32
+ repo_id="Qwen/Qwen2.5-1.5B-Instruct-GGUF",
33
+ filename="qwen2.5-1.5b-instruct-fp16.gguf"
34
+ )
35
 
36
  FRONTEND_ORIGIN = os.environ.get(
37
  "FRONTEND_ORIGIN",
 
80
  # LLM (phi3 GGUF)
81
  # ─────────────────────────────────────────────
82
 
83
+ # Inicializando o modelo
84
  llm = Llama(
85
  model_path=LLM_PATH,
86
+ n_ctx=2048, # Tamanho da janela de contexto
87
+ n_threads=2 # O Space free tem 2 vCPUs
 
 
 
88
  )
89
 
90
  def generate_poem(detections, style):
 
95
  for k, v in counts.items()
96
  ) or "silence"
97
 
98
+ prompt = f"""<|im_start|>system
99
+ You are a master of classical poetry. You write only the poem requested, following strict metrical and rhyme rules. No titles, no explanations, no markdown.<|im_end|>
100
+ <|im_start|>user
101
+ Write a Shakespearean sonnet (14 lines, ABAB CDCD EFEF GG) about: {objects}.
102
+ Style of the artwork: {style["style_name"]}.
 
 
103
 
104
+ Directives:
105
+ - Strictly 14 lines.
106
+ - No markdown (no bold, no italics).
107
+ - No title.
108
+ - Output only the poem text.<|im_end|>
109
+ <|im_start|>assistant
110
+ """
 
 
 
 
 
 
 
111
 
112
  output = llm(
113
  prompt,
114
+ max_tokens=300,
115
+ temperature=0.7, # EquilΓ­brio entre criatividade e ordem
116
  top_p=0.9,
117
+ repeat_penalty=1.1, # Reduzido, pois o FP16 Γ© naturalmente mais coerente
118
+ stop=["<|im_end|>", "User:", "\n\n\n\n"]
119
  )
120
 
121
+ poem_text = output["choices"][0]["text"].strip()
122
+ return poem_text
 
123
 
124
  def format_poem(text: str, max_lines: int = 17):
125
  # ── 1. Remove prefixos tipo [response]: ou "response:"
requirements.txt CHANGED
@@ -10,4 +10,6 @@ python-multipart==0.0.9
10
  numpy==1.26.4
11
 
12
  # ── LLM (TinyLlama GGUF) ─────────────────────
13
- ctransformers==0.2.27
 
 
 
10
  numpy==1.26.4
11
 
12
  # ── LLM (TinyLlama GGUF) ─────────────────────
13
+ ctransformers==0.2.27
14
+ huggingface-hub==0.23.0
15
+ llama-cpp-python==0.2.90