Update utils.py
Browse files
utils.py
CHANGED
|
@@ -1,97 +1,51 @@
|
|
| 1 |
-
|
| 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]]
|
| 12 |
Messages = List[Dict[str, str]]
|
| 13 |
|
| 14 |
def history_to_messages(history: History, system: str) -> Messages:
|
| 15 |
messages = [{'role': 'system', 'content': system}]
|
| 16 |
-
for
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 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
|
| 27 |
|
| 28 |
-
def
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
user_content = q['content']
|
| 33 |
-
if isinstance(user_content, list):
|
| 34 |
-
text_content = ""
|
| 35 |
-
for item in user_content:
|
| 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 |
|
| 42 |
-
def
|
| 43 |
-
|
|
|
|
| 44 |
|
| 45 |
-
def
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 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()
|
| 61 |
-
return f"data:image/png;base64,{img_str}"
|
| 62 |
|
| 63 |
-
def
|
| 64 |
-
|
| 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 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
r'```([\s\S]+?)```'
|
| 78 |
-
]
|
| 79 |
-
for pattern in patterns:
|
| 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
|
| 90 |
-
|
| 91 |
-
|
| 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
|
| 97 |
-
|
|
|
|
|
|
| 1 |
+
# Utility.py
|
| 2 |
|
| 3 |
+
from typing import List, Tuple, Dict, Optional
|
| 4 |
import re
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
|
|
|
| 6 |
History = List[Tuple[str, str]]
|
| 7 |
Messages = List[Dict[str, str]]
|
| 8 |
|
| 9 |
def history_to_messages(history: History, system: str) -> Messages:
|
| 10 |
messages = [{'role': 'system', 'content': system}]
|
| 11 |
+
for user_msg, assistant_msg in history:
|
| 12 |
+
messages.extend([
|
| 13 |
+
{'role': 'user', 'content': user_msg},
|
| 14 |
+
{'role': 'assistant', 'content': assistant_msg}
|
| 15 |
+
])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
return messages
|
| 17 |
|
| 18 |
+
def history_to_chatbot_messages(history: History) -> List[Dict[str, str]]:
|
| 19 |
+
return [{'role': role, 'content': content}
|
| 20 |
+
for user_msg, assistant_msg in history
|
| 21 |
+
for role, content in [('user', user_msg), ('assistant', assistant_msg)]]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
def remove_code_block(text: str) -> str:
|
| 24 |
+
match = re.search(r'```(?:\w+)?\n(.+?)```', text, re.DOTALL)
|
| 25 |
+
return match.group(1).strip() if match else text
|
| 26 |
|
| 27 |
+
def parse_transformers_js_output(text: str) -> Dict[str, str]:
|
| 28 |
+
files = {'index.html': '', 'index.js': '', 'style.css': ''}
|
| 29 |
+
for ext in files.keys():
|
| 30 |
+
pattern = rf'```{ext.split(".")[1]}\s*\n(.+?)\n```'
|
| 31 |
+
match = re.search(pattern, text, re.DOTALL | re.IGNORECASE)
|
| 32 |
+
if match:
|
| 33 |
+
files[ext] = match.group(1).strip()
|
| 34 |
+
return files
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
def format_transformers_js_output(files: Dict[str, str]) -> str:
|
| 37 |
+
return '\n\n'.join(f'=== {name} ===\n{content}' for name, content in files.items())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
def apply_search_replace_changes(original: str, changes: str) -> str:
|
| 40 |
+
search_pattern = r'<<<<<< SEARCH\n(.+?)\n=======\n(.+?)\n>>>>>>> REPLACE'
|
| 41 |
+
for search, replace in re.findall(search_pattern, changes, re.DOTALL):
|
| 42 |
+
original = original.replace(search.strip(), replace.strip())
|
| 43 |
+
return original
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
def extract_text_from_file(filepath: str) -> str:
|
| 46 |
+
with open(filepath, 'r', encoding='utf-8') as f:
|
| 47 |
+
return f.read()
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
def extract_website_content(url: str) -> str:
|
| 50 |
+
# Placeholder: Implement actual web scraping logic here
|
| 51 |
+
return "Website content from URL: " + url
|