Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -49,6 +49,9 @@ def generate_text_image_with_pil(old_text, input_token, new_token, image_width=4
|
|
49 |
paragraphs = old_text.split('\n')
|
50 |
|
51 |
# Add the token information to the last paragraph
|
|
|
|
|
|
|
52 |
if paragraphs:
|
53 |
paragraphs[-1] += f"[{input_token}]→[{new_token}]"
|
54 |
else:
|
@@ -72,7 +75,7 @@ def generate_text_image_with_pil(old_text, input_token, new_token, image_width=4
|
|
72 |
draw = ImageDraw.Draw(img)
|
73 |
|
74 |
# Load font
|
75 |
-
font_path = "NotoSansCJK-
|
76 |
font = ImageFont.truetype(font_path, font_size)
|
77 |
|
78 |
# Draw text
|
@@ -80,7 +83,7 @@ def generate_text_image_with_pil(old_text, input_token, new_token, image_width=4
|
|
80 |
token_marker = f"[{input_token}]→[{new_token}]"
|
81 |
|
82 |
for line in all_lines:
|
83 |
-
if token_marker in line:
|
84 |
parts = line.split(token_marker)
|
85 |
# Draw text before token
|
86 |
draw.text((10, y), parts[0], fill="black", font=font)
|
@@ -102,11 +105,12 @@ def generate_text_image_with_pil(old_text, input_token, new_token, image_width=4
|
|
102 |
x += draw.textlength(f"[{new_token}]", font=font)
|
103 |
draw.text((x, y), parts[1], fill="black", font=font)
|
104 |
else:
|
|
|
|
|
105 |
draw.text((10, y), line, fill="black", font=font)
|
106 |
|
107 |
# Move to next line, adding extra space between paragraphs
|
108 |
y += font_size + 8
|
109 |
-
|
110 |
return np.array(img)
|
111 |
|
112 |
|
@@ -163,7 +167,10 @@ def render_next_token_table_image(table_data, predict_token, image_width=500, ro
|
|
163 |
token_str = token_str
|
164 |
color = "red" if token_str == predict_token and col == 1 else "blue" if col == 1 else "black"
|
165 |
bold = token_str == predict_token and col == 1
|
166 |
-
|
|
|
|
|
|
|
167 |
draw_cell(x, y, f"{token_str_} ({prob:.1%})", color=color, bold=bold)
|
168 |
|
169 |
return np.array(img)
|
|
|
49 |
paragraphs = old_text.split('\n')
|
50 |
|
51 |
# Add the token information to the last paragraph
|
52 |
+
input_token = input_token.replace("\n","\\n")
|
53 |
+
new_token = new_token.replace("\n","\\n")
|
54 |
+
|
55 |
if paragraphs:
|
56 |
paragraphs[-1] += f"[{input_token}]→[{new_token}]"
|
57 |
else:
|
|
|
75 |
draw = ImageDraw.Draw(img)
|
76 |
|
77 |
# Load font
|
78 |
+
font_path = "/usr/share/fonts/truetype/noto/NotoSansCJK-Regular.ttc"
|
79 |
font = ImageFont.truetype(font_path, font_size)
|
80 |
|
81 |
# Draw text
|
|
|
83 |
token_marker = f"[{input_token}]→[{new_token}]"
|
84 |
|
85 |
for line in all_lines:
|
86 |
+
if token_marker in line:
|
87 |
parts = line.split(token_marker)
|
88 |
# Draw text before token
|
89 |
draw.text((10, y), parts[0], fill="black", font=font)
|
|
|
105 |
x += draw.textlength(f"[{new_token}]", font=font)
|
106 |
draw.text((x, y), parts[1], fill="black", font=font)
|
107 |
else:
|
108 |
+
print(token_marker)
|
109 |
+
print(line)
|
110 |
draw.text((10, y), line, fill="black", font=font)
|
111 |
|
112 |
# Move to next line, adding extra space between paragraphs
|
113 |
y += font_size + 8
|
|
|
114 |
return np.array(img)
|
115 |
|
116 |
|
|
|
167 |
token_str = token_str
|
168 |
color = "red" if token_str == predict_token and col == 1 else "blue" if col == 1 else "black"
|
169 |
bold = token_str == predict_token and col == 1
|
170 |
+
if token_str.count(" ") == 1 and len(token_str) != 1:
|
171 |
+
token_str_ = token_str.replace("\n", "\\n").replace("\t", "\\t")
|
172 |
+
else:
|
173 |
+
token_str_ = token_str.replace("\n", "\\n").replace(" ", "\\s").replace("\t", "\\t")
|
174 |
draw_cell(x, y, f"{token_str_} ({prob:.1%})", color=color, bold=bold)
|
175 |
|
176 |
return np.array(img)
|