Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,48 +3,58 @@ import google.generativeai as genai
|
|
| 3 |
from PIL import Image
|
| 4 |
import os
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
model
|
| 9 |
input_prompt = """
|
| 10 |
As an HTML and CSS expert, your task is to create complete HTML and CSS code based on the provided screenshot, ensuring clear and functional markup. Provide a main.html file with inline CSS that replicates the exact color and style as shown in the given screenshot.
|
| 11 |
Output structure:
|
| 12 |
-
Start and end with (```)
|
| 13 |
"""
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
return response.text
|
| 19 |
|
|
|
|
| 20 |
def input_image_setup(uploaded_file):
|
| 21 |
-
# Check if a file has been uploaded
|
| 22 |
if uploaded_file is not None:
|
| 23 |
-
# Read the file into bytes
|
| 24 |
bytes_data = uploaded_file.getvalue()
|
| 25 |
-
|
| 26 |
image_parts = [
|
| 27 |
{
|
| 28 |
-
"mime_type": uploaded_file.type,
|
| 29 |
"data": bytes_data
|
| 30 |
}
|
| 31 |
]
|
| 32 |
return image_parts
|
| 33 |
else:
|
| 34 |
raise FileNotFoundError("No file uploaded")
|
| 35 |
-
|
| 36 |
-
st.title("SCREENSHOT - HTML CODE📃")
|
| 37 |
-
st.text("Uploade your demo webpage image Here:")
|
| 38 |
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
if upload_file is not None:
|
| 41 |
image = Image.open(upload_file)
|
| 42 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 43 |
-
|
| 44 |
-
submit = st.button('Create a
|
| 45 |
if submit:
|
| 46 |
image_data = input_image_setup(uploaded_file=upload_file)
|
| 47 |
with st.spinner("Building the Webpage..."):
|
| 48 |
response = generate_response(input_prompt, image_data)
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
import os
|
| 5 |
|
| 6 |
+
# Configure GenAI with API Key
|
| 7 |
+
genai.configure(api_key=os.getenv('GOOGLE_API_KEY'))
|
| 8 |
+
model = genai.GenerativeModel('gemini-1.5-pro-latest')
|
| 9 |
|
| 10 |
+
# Input prompt for model
|
| 11 |
input_prompt = """
|
| 12 |
As an HTML and CSS expert, your task is to create complete HTML and CSS code based on the provided screenshot, ensuring clear and functional markup. Provide a main.html file with inline CSS that replicates the exact color and style as shown in the given screenshot.
|
| 13 |
Output structure:
|
| 14 |
+
Start and end with (```).
|
| 15 |
"""
|
| 16 |
|
| 17 |
+
# Function to generate response from model
|
| 18 |
+
def generate_response(input_prompt, image):
|
| 19 |
+
response = model.generate_content([input_prompt, image[0]])
|
| 20 |
return response.text
|
| 21 |
|
| 22 |
+
# Function to process uploaded image
|
| 23 |
def input_image_setup(uploaded_file):
|
|
|
|
| 24 |
if uploaded_file is not None:
|
|
|
|
| 25 |
bytes_data = uploaded_file.getvalue()
|
|
|
|
| 26 |
image_parts = [
|
| 27 |
{
|
| 28 |
+
"mime_type": uploaded_file.type,
|
| 29 |
"data": bytes_data
|
| 30 |
}
|
| 31 |
]
|
| 32 |
return image_parts
|
| 33 |
else:
|
| 34 |
raise FileNotFoundError("No file uploaded")
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
# Streamlit App Interface
|
| 37 |
+
st.title("Screenshot to HTML Code 📃")
|
| 38 |
+
st.text("Upload your demo webpage image here:")
|
| 39 |
+
|
| 40 |
+
upload_file = st.file_uploader('', type=['jpg', 'jpeg', 'png'])
|
| 41 |
if upload_file is not None:
|
| 42 |
image = Image.open(upload_file)
|
| 43 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 44 |
+
|
| 45 |
+
submit = st.button('Create a Webpage')
|
| 46 |
if submit:
|
| 47 |
image_data = input_image_setup(uploaded_file=upload_file)
|
| 48 |
with st.spinner("Building the Webpage..."):
|
| 49 |
response = generate_response(input_prompt, image_data)
|
| 50 |
+
|
| 51 |
+
# Extract code from the response
|
| 52 |
+
code = response.strip("```")
|
| 53 |
+
|
| 54 |
+
# Display the code
|
| 55 |
+
st.subheader("Generated Code:")
|
| 56 |
+
st.code(code, language='html')
|
| 57 |
+
|
| 58 |
+
# Render the HTML Output
|
| 59 |
+
st.subheader("Rendered HTML Output:")
|
| 60 |
+
st.components.v1.html(code, height=500, scrolling=True)
|