File size: 2,009 Bytes
636a621
0ff9995
636a621
682ffcf
 
 
 
 
 
 
636a621
 
 
 
 
682ffcf
 
636a621
 
 
 
682ffcf
636a621
 
 
682ffcf
636a621
 
 
 
 
 
 
 
682ffcf
636a621
 
682ffcf
636a621
 
 
 
 
682ffcf
636a621
 
 
682ffcf
636a621
 
42d7039
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Utility.py

from typing import List, Tuple, Dict, Optional
import re

History = List[Tuple[str, str]]
Messages = List[Dict[str, str]]

def history_to_messages(history: History, system: str) -> Messages:
    messages = [{'role': 'system', 'content': system}]
    for user_msg, assistant_msg in history:
        messages.extend([
            {'role': 'user', 'content': user_msg},
            {'role': 'assistant', 'content': assistant_msg}
        ])
    return messages

def history_to_chatbot_messages(history: History) -> List[Dict[str, str]]:
    return [{'role': role, 'content': content} 
            for user_msg, assistant_msg in history 
            for role, content in [('user', user_msg), ('assistant', assistant_msg)]]

def remove_code_block(text: str) -> str:
    match = re.search(r'```(?:\w+)?\n(.+?)```', text, re.DOTALL)
    return match.group(1).strip() if match else text

def parse_transformers_js_output(text: str) -> Dict[str, str]:
    files = {'index.html': '', 'index.js': '', 'style.css': ''}
    for ext in files.keys():
        pattern = rf'```{ext.split(".")[1]}\s*\n(.+?)\n```'
        match = re.search(pattern, text, re.DOTALL | re.IGNORECASE)
        if match:
            files[ext] = match.group(1).strip()
    return files

def format_transformers_js_output(files: Dict[str, str]) -> str:
    return '\n\n'.join(f'=== {name} ===\n{content}' for name, content in files.items())

def apply_search_replace_changes(original: str, changes: str) -> str:
    search_pattern = r'<<<<<< SEARCH\n(.+?)\n=======\n(.+?)\n>>>>>>> REPLACE'
    for search, replace in re.findall(search_pattern, changes, re.DOTALL):
        original = original.replace(search.strip(), replace.strip())
    return original

def extract_text_from_file(filepath: str) -> str:
    with open(filepath, 'r', encoding='utf-8') as f:
        return f.read()

def extract_website_content(url: str) -> str:
    # Placeholder: Implement actual web scraping logic here
    return "Website content from URL: " + url