ositamiles's picture
Update app.py
35c6d7f verified
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
""")