File size: 3,898 Bytes
031fe48
 
 
35c6d7f
 
031fe48
0c7ca3d
3d6b370
9761b4a
031fe48
 
0c7ca3d
031fe48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0ece09
 
e404d09
 
 
 
 
 
 
 
 
 
35c6d7f
 
 
 
 
 
 
 
 
 
 
031fe48
 
e404d09
 
031fe48
e404d09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35c6d7f
9761b4a
e404d09
 
d0ece09
 
 
 
 
 
 
 
 
e404d09
d0ece09
e404d09
 
 
 
 
35c6d7f
e404d09
 
 
031fe48
d0ece09
 
 
 
 
 
031fe48
 
 
e404d09
 
d0ece09
 
 
031fe48
e404d09
031fe48
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import streamlit as st
import requests
import json
from PIL import Image
from io import BytesIO

def create_template(name, image_url, category, prompt):
    url = "https://ai-agent-backend-v50m.onrender.com/api/v1/templates/" 
    
    payload = {
        "name": name,
        "image": image_url,
        "category": category,
        "prompt": prompt
    }
    
    headers = {
        "Content-Type": "application/json"
    }
    
    response = requests.post(url, json=payload, headers=headers)
    
    if response.status_code == 200:
        return response.json()
    elif response.status_code == 404:
        return "Not found"
    elif response.status_code == 422:
        return response.json()
    else:
        return f"Unexpected error: {response.status_code}"

def get_templates(skip=0, limit=10):
    url = f"https://ai-agent-backend-v50m.onrender.com/api/v1/templates/?skip={skip}&limit={limit}"
    
    try:
        response = requests.get(url)
        if response.status_code == 200:
            return response.json()
        else:
            return f"Error fetching templates: {response.status_code}"
    except requests.RequestException as e:
        return f"Request failed: {str(e)}"

def display_image_from_url(image_url):
    try:
        response = requests.get(image_url)
        if response.status_code == 200:
            image = Image.open(BytesIO(response.content))
            st.image(image, caption="Template Image", use_column_width=True)
        else:
            st.error(f"Failed to load image from URL: {image_url}")
    except Exception as e:
        st.error(f"Error displaying image: {str(e)}")

st.title("AI Agent Template Creator")

# Create tabs for different functionalities
tab1, tab2 = st.tabs(["Create Template", "View Templates"])

with tab1:
    st.header("Create New Template")
    name = st.text_input("Name")
    image_url = st.text_input("Image URL")
    category = st.text_input("Category")
    prompt = st.text_area("Prompt")

    if st.button("Create Template"):
        if name and image_url and category and prompt:
            result = create_template(name, image_url, category, prompt)
            st.json(result)
        else:
            st.error("Please fill in all fields including the image URL.")

    if image_url:
        display_image_from_url(image_url)

with tab2:
    st.header("Existing Templates")
    
    col1, col2 = st.columns(2)
    with col1:
        skip = st.number_input("Skip", min_value=0, value=0, step=10)
    with col2:
        limit = st.number_input("Limit", min_value=1, value=10, max_value=100, step=5)

    if st.button("Fetch Templates"):
        templates = get_templates(skip=skip, limit=limit)
        if isinstance(templates, list):
            st.write(f"Showing templates {skip+1}-{skip+len(templates)}")
            for template in templates:
                with st.expander(f"Template: {template.get('name', 'Unnamed')}"):
                    st.write(f"Category: {template.get('category', 'N/A')}")
                    st.write(f"Prompt: {template.get('prompt', 'N/A')}")
                    if template.get('image'):
                        display_image_from_url(template['image'])
                    st.write("---")
        else:
            st.error(f"Failed to fetch templates: {templates}")

    st.markdown("""
    ### Pagination Controls
    - **Skip**: Number of templates to skip (default: 0)
    - **Limit**: Maximum number of templates to return (default: 10)
    """)

st.markdown("---")
st.subheader("API Information")
st.markdown("""
- POST Endpoint: https://ai-agent-backend-v50m.onrender.com/api/v1/templates/
- GET Endpoint: https://ai-agent-backend-v50m.onrender.com/api/v1/templates/
- Query Parameters:
  - skip (integer, default: 0)
  - limit (integer, default: 10)
- Content-Type: application/json
- Required fields for POST: name, image (URL), category, prompt
""")