Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import yaml
|
| 4 |
+
import requests
|
| 5 |
+
import os
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
import json
|
| 8 |
+
|
| 9 |
+
# Function to load YAML files
|
| 10 |
+
def load_yaml_file(file_path):
|
| 11 |
+
with open(file_path, "r") as file:
|
| 12 |
+
return yaml.safe_load(file)
|
| 13 |
+
|
| 14 |
+
# Initialize app configuration
|
| 15 |
+
st.set_page_config(page_title="Multi-tab Image App", layout="wide")
|
| 16 |
+
|
| 17 |
+
# Output folder
|
| 18 |
+
output_folder = "output"
|
| 19 |
+
os.makedirs(output_folder, exist_ok=True)
|
| 20 |
+
|
| 21 |
+
# Placeholder for the image display
|
| 22 |
+
image_placeholder = st.empty()
|
| 23 |
+
|
| 24 |
+
# Load Chatbot system prompts
|
| 25 |
+
chatbot_prompts = load_yaml_file("system_prompts.yaml")
|
| 26 |
+
|
| 27 |
+
# Tabs
|
| 28 |
+
tab_names = ["Image Generation", "Chat/Prompt Enhancement", "AIvatar ChatBot", "Story Generation", "Gallery", "System Prompts Editor"]
|
| 29 |
+
tabs = st.tabs(tab_names)
|
| 30 |
+
|
| 31 |
+
# Image Generation Tab
|
| 32 |
+
with tabs[0]:
|
| 33 |
+
st.header("Image Generation")
|
| 34 |
+
|
| 35 |
+
# Input fields
|
| 36 |
+
prompt = st.text_input("Prompt")
|
| 37 |
+
seed = st.slider("Seed", min_value=0, max_value=1000000, value=0)
|
| 38 |
+
randomize_seed = st.checkbox("Randomize seed", value=True)
|
| 39 |
+
width = st.slider("Width", min_value=256, max_value=1024, value=1024)
|
| 40 |
+
height = st.slider("Height", min_value=256, max_value=1024, value=1024)
|
| 41 |
+
steps = st.slider("Inference steps", min_value=1, max_value=50, value=4)
|
| 42 |
+
|
| 43 |
+
# Generate Image button
|
| 44 |
+
if st.button("Generate Image"):
|
| 45 |
+
st.info("Generating image...")
|
| 46 |
+
# Simulate the image generation process
|
| 47 |
+
try:
|
| 48 |
+
# Replace with your own API call
|
| 49 |
+
result = {"image_path": "sample_image.png", "generated_seed": seed}
|
| 50 |
+
|
| 51 |
+
image_path = result['image_path']
|
| 52 |
+
image = Image.open(image_path)
|
| 53 |
+
image_placeholder.image(image)
|
| 54 |
+
|
| 55 |
+
# Save image
|
| 56 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 57 |
+
prompt_slug = "_".join(prompt.replace(':', '').replace("'", '').replace('"', '').replace(',', '').replace('.', '').replace('*', '##').strip().split()[:10]).lower()
|
| 58 |
+
filename = f"{timestamp}_{prompt_slug}.png"
|
| 59 |
+
file_path = os.path.join(output_folder, filename)
|
| 60 |
+
image.save(file_path)
|
| 61 |
+
st.success(f"Image saved as {file_path}")
|
| 62 |
+
except Exception as e:
|
| 63 |
+
st.error(f"An error occurred: {str(e)}")
|
| 64 |
+
|
| 65 |
+
# Chat/Prompt Enhancement Tab
|
| 66 |
+
with tabs[1]:
|
| 67 |
+
st.header("Chat/Prompt Enhancement")
|
| 68 |
+
|
| 69 |
+
# Chat history and user input
|
| 70 |
+
chat_history = st.text_area("Chat History", height=300)
|
| 71 |
+
user_input = st.text_input("Your Input")
|
| 72 |
+
|
| 73 |
+
# Buttons for sending message or listening
|
| 74 |
+
if st.button("Send"):
|
| 75 |
+
st.info("Sending message...")
|
| 76 |
+
# Simulate sending a message to the chatbot
|
| 77 |
+
if st.button("Listen"):
|
| 78 |
+
st.info("Listening...")
|
| 79 |
+
|
| 80 |
+
# AIvatar ChatBot Tab
|
| 81 |
+
with tabs[2]:
|
| 82 |
+
st.header("AIvatar ChatBot")
|
| 83 |
+
|
| 84 |
+
# Chat history and user input
|
| 85 |
+
chat_history_ai = st.text_area("Chat History", height=300)
|
| 86 |
+
user_input_ai = st.text_input("Your Input")
|
| 87 |
+
|
| 88 |
+
# Send button
|
| 89 |
+
if st.button("Send AI Message"):
|
| 90 |
+
st.info("Sending message to AIvatar ChatBot...")
|
| 91 |
+
# Simulate sending a message to the chatbot
|
| 92 |
+
|
| 93 |
+
# Story Generation Tab
|
| 94 |
+
with tabs[3]:
|
| 95 |
+
st.header("Story Generation")
|
| 96 |
+
|
| 97 |
+
# Input fields for story generation
|
| 98 |
+
story_prompt = st.text_input("Story Prompt")
|
| 99 |
+
story_style = st.text_input("Story Style")
|
| 100 |
+
|
| 101 |
+
# Generate Story button
|
| 102 |
+
if st.button("Generate Story"):
|
| 103 |
+
st.info("Generating story...")
|
| 104 |
+
# Simulate story generation process
|
| 105 |
+
st.text_area("Story Output", "Generated story will appear here...", height=300)
|
| 106 |
+
|
| 107 |
+
# Save to PDF button
|
| 108 |
+
if st.button("Save to PDF"):
|
| 109 |
+
st.info("Saving story to PDF...")
|
| 110 |
+
|
| 111 |
+
# Gallery Tab
|
| 112 |
+
with tabs[4]:
|
| 113 |
+
st.header("Gallery")
|
| 114 |
+
|
| 115 |
+
# Load and display images from the output folder
|
| 116 |
+
images = [f for f in os.listdir(output_folder) if f.endswith(".png")]
|
| 117 |
+
if images:
|
| 118 |
+
st.image([os.path.join(output_folder, img) for img in images], width=150)
|
| 119 |
+
else:
|
| 120 |
+
st.info("No images found in the gallery.")
|
| 121 |
+
|
| 122 |
+
# System Prompts Editor Tab
|
| 123 |
+
with tabs[5]:
|
| 124 |
+
st.header("System Prompts Editor")
|
| 125 |
+
|
| 126 |
+
# Select and edit prompts
|
| 127 |
+
prompt_keys = list(chatbot_prompts.keys())
|
| 128 |
+
selected_prompt_key = st.selectbox("Select a prompt to edit", prompt_keys)
|
| 129 |
+
selected_prompt_text = st.text_area("Prompt Text", chatbot_prompts[selected_prompt_key], height=200)
|
| 130 |
+
|
| 131 |
+
# Save prompt button
|
| 132 |
+
if st.button("Save Prompt"):
|
| 133 |
+
chatbot_prompts[selected_prompt_key] = selected_prompt_text
|
| 134 |
+
# Save the updated prompts back to the YAML file
|
| 135 |
+
with open("system_prompts.yaml", "w") as file:
|
| 136 |
+
yaml.safe_dump(chatbot_prompts, file)
|
| 137 |
+
st.success(f"Prompt '{selected_prompt_key}' saved successfully!")
|