Spaces:
Running
Running
File size: 1,845 Bytes
b2b4354 |
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 53 54 55 56 57 58 59 60 61 |
import streamlit as st
from smolagents.agents import ToolCallingAgent
from smolagents import tool, LiteLLMModel
from typing import Optional
import cv2
import pytesseract
from PIL import Image
import io
# Define the LiteLLMModel
model = LiteLLMModel(model_id="gpt-4o")
@tool
def extract_components(image_data: bytes) -> str:
"""
Extract components from a web design image.
Args:
image_data: The image data in bytes.
Returns:
A string describing the components found in the image.
"""
image = Image.open(io.BytesIO(image_data))
gray = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2GRAY)
components = pytesseract.image_to_string(gray)
return components
@tool
def generate_code(components: str) -> str:
"""
Generate code for the given components.
Args:
components: A string describing the components.
Returns:
The generated code for the components.
"""
# This is a placeholder implementation. You can replace it with actual code generation logic.
return f"Generated code for components: {components}"
# Define the ToolCallingAgent
agent = ToolCallingAgent(tools=[extract_components, generate_code], model=model)
# Streamlit app title
st.title("Web Design Component Extractor")
# File uploader for the web design image
uploaded_file = st.file_uploader("Upload a web design image", type=["png", "jpg", "jpeg"])
# Button to run the agent
if st.button("Extract and Generate Code"):
if uploaded_file is not None:
image_data = uploaded_file.read()
components = agent.run("extract_components", image_data=image_data)
code = agent.run("generate_code", components=components)
st.write("Extracted Components:", components)
st.write("Generated Code:", code)
else:
st.write("Please upload an image.") |