Akshayram1 commited on
Commit
6a88005
·
verified ·
1 Parent(s): 7ef2fb4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -0
app.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.schema import BaseMessage, HumanMessage, SystemMessage
2
+ from langchain.chat_models import ChatOpenAI
3
+ import os
4
+ from bs4 import BeautifulSoup
5
+ import streamlit as st
6
+
7
+ # Streamlit App
8
+ st.title("HTML to Twig and SCSS Converter")
9
+
10
+ # Input OpenAI API Key
11
+ openai_api_key = st.text_input("Enter your OpenAI API Key", type="password")
12
+
13
+ if openai_api_key:
14
+ os.environ["OPENAI_API_KEY"] = openai_api_key
15
+ llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
16
+
17
+ def extract_css_and_replace_with_variables(css_content, variables_content):
18
+ """Converts CSS styles to SCSS and replaces values with SCSS variables."""
19
+ try:
20
+ # Create a message for SCSS conversion and variable replacement
21
+ message = [
22
+ SystemMessage(
23
+ content=f"""You are a CSS-to-SCSS converter and variable replacer. Here is the task:
24
+ 1. Use the following SCSS variables: \n{variables_content}
25
+ 2. Replace all matching colors and fonts in the CSS with the SCSS variables.
26
+ 3. Ensure the output SCSS is clean, modular, and maintains its functionality."""
27
+ ),
28
+ HumanMessage(content=css_content)
29
+ ]
30
+
31
+ # Generate SCSS response
32
+ scss_response = llm(message)
33
+ return scss_response.content
34
+ except Exception as e:
35
+ print(f"Error processing CSS to SCSS: {e}")
36
+ return None
37
+
38
+ def create_html_to_twig_conversion_message(html_content):
39
+ """Generates a message to convert HTML content into Twig."""
40
+ return [
41
+ SystemMessage(
42
+ content="""You are an HTML-to-Twig converter. I will provide you with an HTML file's content, and your task is to convert it into a Twig file. Ensure that:
43
+ - All static text is replaced with meaningful Twig variables.
44
+ - Dynamic content is wrapped using Twig's syntax (e.g., {{ variable }}).
45
+ - Repeated elements use loops (e.g., {% for item in collection %}).
46
+ - Conditional elements use Twig logic (e.g., {% if condition %}).
47
+ - All attributes (e.g., href, src, alt) are dynamically handled where applicable.
48
+ - Twig syntax is correctly validated and error-free.
49
+ - The overall structure and styling of the HTML file remain intact.
50
+ - Provide explanations of Twig variables in the following commented format:
51
+ <!--
52
+ ### Explanation of Twig Variables:
53
+ - `{{ variable_name }}`: Description of the variable.
54
+ -->"""
55
+ ),
56
+ HumanMessage(content=html_content)
57
+ ]
58
+
59
+ def convert_html_to_twig_with_scss(html_content, variables_content):
60
+ """Converts an HTML file content into a Twig file and extracts SCSS styles."""
61
+ # Extract SCSS from the HTML content
62
+ scss_content = extract_css_and_replace_with_variables(html_content, variables_content)
63
+
64
+ # Convert HTML to Twig
65
+ messages = create_html_to_twig_conversion_message(html_content)
66
+ try:
67
+ twig_response = llm(messages) # Send the messages to the LLM
68
+ twig_content = twig_response.content
69
+ return twig_content, scss_content
70
+ except Exception as e:
71
+ print(f"Error generating Twig or SCSS file: {e}")
72
+ return None, None
73
+
74
+ # File uploaders
75
+ html_file = st.file_uploader("Upload HTML File", type=["html"], key="html_file")
76
+ css_file = st.file_uploader("Upload CSS File (Optional)", type=["css"], key="css_file")
77
+ variables_file = st.file_uploader("Upload SCSS Variables File", type=["scss"], key="variables_file")
78
+
79
+ if (html_file or css_file) and variables_file:
80
+ variables_content = variables_file.read().decode("utf-8")
81
+
82
+ if html_file:
83
+ html_content = html_file.read().decode("utf-8")
84
+ twig_content, scss_content = convert_html_to_twig_with_scss(html_content, variables_content)
85
+
86
+ if twig_content and scss_content:
87
+ st.subheader("Twig Output")
88
+ st.code(twig_content, language="twig")
89
+
90
+ st.subheader("SCSS Output")
91
+ st.code(scss_content, language="scss")
92
+
93
+ st.download_button("Download Twig File", data=twig_content, file_name="output.twig", mime="text/plain")
94
+ st.download_button("Download SCSS File", data=scss_content, file_name="styles.scss", mime="text/plain")
95
+
96
+ if css_file:
97
+ css_content = css_file.read().decode("utf-8")
98
+ scss_content = extract_css_and_replace_with_variables(css_content, variables_content)
99
+
100
+ if scss_content:
101
+ st.subheader("SCSS Output from CSS")
102
+ st.code(scss_content, language="scss")
103
+
104
+ st.download_button("Download SCSS File", data=scss_content, file_name="styles_from_css.scss", mime="text/plain")