Akshayram1 commited on
Commit
b2b4354
·
verified ·
1 Parent(s): 8cbefa9

Create app3.py

Browse files
Files changed (1) hide show
  1. app3.py +61 -0
app3.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from smolagents.agents import ToolCallingAgent
3
+ from smolagents import tool, LiteLLMModel
4
+ from typing import Optional
5
+ import cv2
6
+ import pytesseract
7
+ from PIL import Image
8
+ import io
9
+
10
+ # Define the LiteLLMModel
11
+ model = LiteLLMModel(model_id="gpt-4o")
12
+
13
+ @tool
14
+ def extract_components(image_data: bytes) -> str:
15
+ """
16
+ Extract components from a web design image.
17
+
18
+ Args:
19
+ image_data: The image data in bytes.
20
+
21
+ Returns:
22
+ A string describing the components found in the image.
23
+ """
24
+ image = Image.open(io.BytesIO(image_data))
25
+ gray = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2GRAY)
26
+ components = pytesseract.image_to_string(gray)
27
+ return components
28
+
29
+ @tool
30
+ def generate_code(components: str) -> str:
31
+ """
32
+ Generate code for the given components.
33
+
34
+ Args:
35
+ components: A string describing the components.
36
+
37
+ Returns:
38
+ The generated code for the components.
39
+ """
40
+ # This is a placeholder implementation. You can replace it with actual code generation logic.
41
+ return f"Generated code for components: {components}"
42
+
43
+ # Define the ToolCallingAgent
44
+ agent = ToolCallingAgent(tools=[extract_components, generate_code], model=model)
45
+
46
+ # Streamlit app title
47
+ st.title("Web Design Component Extractor")
48
+
49
+ # File uploader for the web design image
50
+ uploaded_file = st.file_uploader("Upload a web design image", type=["png", "jpg", "jpeg"])
51
+
52
+ # Button to run the agent
53
+ if st.button("Extract and Generate Code"):
54
+ if uploaded_file is not None:
55
+ image_data = uploaded_file.read()
56
+ components = agent.run("extract_components", image_data=image_data)
57
+ code = agent.run("generate_code", components=components)
58
+ st.write("Extracted Components:", components)
59
+ st.write("Generated Code:", code)
60
+ else:
61
+ st.write("Please upload an image.")