Update utils.py
Browse files
utils.py
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
|
|
|
|
|
| 1 |
from typing import List, Tuple, Dict
|
| 2 |
import re
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
# History/message helpers
|
| 5 |
History = List[Tuple[str, str]]
|
|
@@ -15,7 +21,6 @@ def history_to_messages(history: History, system: str) -> Messages:
|
|
| 15 |
if isinstance(item, dict) and item.get("type") == "text":
|
| 16 |
text_content += item.get("text", "")
|
| 17 |
user_content = text_content if text_content else str(user_content)
|
| 18 |
-
|
| 19 |
messages.append({'role': 'user', 'content': user_content})
|
| 20 |
messages.append({'role': 'assistant', 'content': h[1]})
|
| 21 |
return messages
|
|
@@ -31,7 +36,6 @@ def messages_to_history(messages: Messages) -> Tuple[str, History]:
|
|
| 31 |
if isinstance(item, dict) and item.get("type") == "text":
|
| 32 |
text_content += item.get("text", "")
|
| 33 |
user_content = text_content if text_content else str(user_content)
|
| 34 |
-
|
| 35 |
history.append([user_content, r['content']])
|
| 36 |
return history
|
| 37 |
|
|
@@ -47,16 +51,10 @@ def update_image_input_visibility(model):
|
|
| 47 |
return gr.update(visible=is_ernie_vl or is_glm_vl)
|
| 48 |
|
| 49 |
def process_image_for_model(image):
|
| 50 |
-
import io, base64
|
| 51 |
-
import numpy as np
|
| 52 |
-
from PIL import Image
|
| 53 |
-
|
| 54 |
if image is None:
|
| 55 |
return None
|
| 56 |
-
|
| 57 |
if isinstance(image, np.ndarray):
|
| 58 |
image = Image.fromarray(image)
|
| 59 |
-
|
| 60 |
buffer = io.BytesIO()
|
| 61 |
image.save(buffer, format='PNG')
|
| 62 |
img_str = base64.b64encode(buffer.getvalue()).decode()
|
|
@@ -65,14 +63,13 @@ def process_image_for_model(image):
|
|
| 65 |
def create_multimodal_message(text, image=None):
|
| 66 |
if image is None:
|
| 67 |
return {"role": "user", "content": text}
|
| 68 |
-
|
| 69 |
content = [
|
| 70 |
{"type": "text", "text": text},
|
| 71 |
{"type": "image_url", "image_url": {"url": process_image_for_model(image)}}
|
| 72 |
]
|
| 73 |
return {"role": "user", "content": content}
|
| 74 |
|
| 75 |
-
# Code
|
| 76 |
def remove_code_block(text):
|
| 77 |
patterns = [
|
| 78 |
r'```(?:html|HTML)\n([\s\S]+?)\n```',
|
|
@@ -83,41 +80,18 @@ def remove_code_block(text):
|
|
| 83 |
match = re.search(pattern, text, re.DOTALL)
|
| 84 |
if match:
|
| 85 |
extracted = match.group(1).strip()
|
| 86 |
-
|
| 87 |
-
|
|
|
|
| 88 |
return extracted
|
| 89 |
-
if text.strip().startswith('<!DOCTYPE') or text.strip().startswith('<html'):
|
| 90 |
-
return text.strip()
|
| 91 |
-
if text.strip().startswith('```python'):
|
| 92 |
-
return text.strip()[9:-3].strip()
|
| 93 |
-
lines = text.strip().split('\n',1)
|
| 94 |
-
if lines[0].strip().lower() in ['python','html','css','javascript','json','c','cpp','markdown','latex','jinja2','typescript','yaml','dockerfile','shell','r','sql','sql-mssql','sql-mysql','sql-mariadb','sql-sqlite','sql-cassandra','sql-plsql','sql-hive','sql-pgsql','sql-gql','sql-gpsql','sql-sparksql','sql-esper']:
|
| 95 |
-
return lines[1] if len(lines)>1 else ''
|
| 96 |
return text.strip()
|
| 97 |
|
| 98 |
def parse_transformers_js_output(text):
|
| 99 |
-
files={'index.html':'','index.js':'','style.css':''}
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
if js_match: files['index.js']=js_match.group(1).strip()
|
| 104 |
-
css_match=re.search(r'```css\s*\n([\s\S]+?)\n```',text,re.IGNORECASE)
|
| 105 |
-
if css_match: files['style.css']=css_match.group(1).strip()
|
| 106 |
-
if not(files['index.html'] and files['index.js'] and files['style.css']):
|
| 107 |
-
html_fallback=re.search(r'===\s*index\.html\s*===\n([\s\S]+?)(?=\n===|$)',text,re.IGNORECASE)
|
| 108 |
-
js_fallback=re.search(r'===\s*index\.js\s*===\n([\s\S]+?)(?=\n===|$)',text,re.IGNORECASE)
|
| 109 |
-
css_fallback=re.search(r'===\s*style\.css\s*===\n([\s\S]+?)(?=\n===|$)',text,re.IGNORECASE)
|
| 110 |
-
if html_fallback: files['index.html']=html_fallback.group(1).strip()
|
| 111 |
-
if js_fallback: files['index.js']=js_fallback.group(1).strip()
|
| 112 |
-
if css_fallback: files['style.css']=css_fallback.group(1).strip()
|
| 113 |
return files
|
| 114 |
|
| 115 |
def format_transformers_js_output(files):
|
| 116 |
-
|
| 117 |
-
output.append("=== index.html ===")
|
| 118 |
-
output.append(files['index.html'])
|
| 119 |
-
output.append("\n=== index.js ===")
|
| 120 |
-
output.append(files['index.js'])
|
| 121 |
-
output.append("\n=== style.css ===")
|
| 122 |
-
output.append(files['style.css'])
|
| 123 |
-
return '\n'.join(output)
|
|
|
|
| 1 |
+
### `utils.py`
|
| 2 |
+
|
| 3 |
from typing import List, Tuple, Dict
|
| 4 |
import re
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import io, base64
|
| 7 |
+
import numpy as np
|
| 8 |
+
from PIL import Image
|
| 9 |
|
| 10 |
# History/message helpers
|
| 11 |
History = List[Tuple[str, str]]
|
|
|
|
| 21 |
if isinstance(item, dict) and item.get("type") == "text":
|
| 22 |
text_content += item.get("text", "")
|
| 23 |
user_content = text_content if text_content else str(user_content)
|
|
|
|
| 24 |
messages.append({'role': 'user', 'content': user_content})
|
| 25 |
messages.append({'role': 'assistant', 'content': h[1]})
|
| 26 |
return messages
|
|
|
|
| 36 |
if isinstance(item, dict) and item.get("type") == "text":
|
| 37 |
text_content += item.get("text", "")
|
| 38 |
user_content = text_content if text_content else str(user_content)
|
|
|
|
| 39 |
history.append([user_content, r['content']])
|
| 40 |
return history
|
| 41 |
|
|
|
|
| 51 |
return gr.update(visible=is_ernie_vl or is_glm_vl)
|
| 52 |
|
| 53 |
def process_image_for_model(image):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
if image is None:
|
| 55 |
return None
|
|
|
|
| 56 |
if isinstance(image, np.ndarray):
|
| 57 |
image = Image.fromarray(image)
|
|
|
|
| 58 |
buffer = io.BytesIO()
|
| 59 |
image.save(buffer, format='PNG')
|
| 60 |
img_str = base64.b64encode(buffer.getvalue()).decode()
|
|
|
|
| 63 |
def create_multimodal_message(text, image=None):
|
| 64 |
if image is None:
|
| 65 |
return {"role": "user", "content": text}
|
|
|
|
| 66 |
content = [
|
| 67 |
{"type": "text", "text": text},
|
| 68 |
{"type": "image_url", "image_url": {"url": process_image_for_model(image)}}
|
| 69 |
]
|
| 70 |
return {"role": "user", "content": content}
|
| 71 |
|
| 72 |
+
# Code-block parsing
|
| 73 |
def remove_code_block(text):
|
| 74 |
patterns = [
|
| 75 |
r'```(?:html|HTML)\n([\s\S]+?)\n```',
|
|
|
|
| 80 |
match = re.search(pattern, text, re.DOTALL)
|
| 81 |
if match:
|
| 82 |
extracted = match.group(1).strip()
|
| 83 |
+
lines = extracted.split('\n', 1)
|
| 84 |
+
if lines[0].strip().lower() in ['python', 'html', 'css', 'javascript']:
|
| 85 |
+
return lines[1] if len(lines) > 1 else ''
|
| 86 |
return extracted
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
return text.strip()
|
| 88 |
|
| 89 |
def parse_transformers_js_output(text):
|
| 90 |
+
files = {'index.html': '', 'index.js': '', 'style.css': ''}
|
| 91 |
+
files['index.html'] = re.search(r'```html\s*\n([\s\S]+?)\n```', text, re.IGNORECASE).group(1).strip() if re.search(r'```html\s*\n([\s\S]+?)\n```', text, re.IGNORECASE) else ''
|
| 92 |
+
files['index.js'] = re.search(r'```javascript\s*\n([\s\S]+?)\n```', text, re.IGNORECASE).group(1).strip() if re.search(r'```javascript\s*\n([\s\S]+?)\n```', text, re.IGNORECASE) else ''
|
| 93 |
+
files['style.css'] = re.search(r'```css\s*\n([\s\S]+?)\n```', text, re.IGNORECASE).group(1).strip() if re.search(r'```css\s*\n([\s\S]+?)\n```', text, re.IGNORECASE) else ''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
return files
|
| 95 |
|
| 96 |
def format_transformers_js_output(files):
|
| 97 |
+
return f"=== index.html ===\n{files['index.html']}\n=== index.js ===\n{files['index.js']}\n=== style.css ===\n{files['style.css']}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|