diff --git a/main.py b/main.py index fb9ab949532464a1e3c78aad9e2c186be99e2890..71c7488790e0df9cfcd88d9433628cb048085011 100644 --- a/main.py +++ b/main.py @@ -1,24 +1,71 @@ import streamlit as st -from streamlit_option_menu import option_menu +import requests +import time -# 设置Streamlit应用程序的标题 -st.set_page_config(page_title="app name", layout="wide") +# Streamlit application +def main(): + st.title('Image Generation with Midjourney') -menu1="菜单1" -menu2="菜单2" + # User prompt input + user_prompt = st.text_input("Please enter the prompt for the image:") -with st.sidebar: - menu = option_menu("菜单", [menu1, menu2], - icons=['house', "list-task"], - menu_icon="cast", default_index=0) + # Only run the main code when the user entered a prompt + if user_prompt: -def main(): + # Call the image generation API + task_id = call_imagine_api(user_prompt) + + if task_id: + # Check the task status and display the result + check_and_display_status(task_id) + +# Function to call the imagine API and return task_id +def call_imagine_api(user_prompt): + url_imagine = "https://midjourney.chatgot.io/imagine" + + headers = { + "Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJsaTE3NzE5MzMzNzAyQGdtYWlsLmNvbSIsImV4cGlyZSI6MTcwMzU0ODgwMDAwMCwicmFuZG9tIjoiYTExNTUyZWUyMWU4MTFhOTFiZGFmZTUwMWE0YTA0MDgifQ==.gOnk9jo6LidZSRB/kip94HfwvLCiPUSuL2pbCk+pUzg=", # Replace with your actual token + "Content-Type": "application/json" + } + + params_imagine = {'prompt': user_prompt} + response_imagine = requests.get(url_imagine, params=params_imagine, headers=headers) + if response_imagine.status_code == 200: + task_data = response_imagine.json()['data'] + task_id = task_data['task_id'] + st.success(f"Task submitted with ID: {task_id}") + return task_id + else: + st.error("Failed to submit task.") + return None + +# Function to check task status and display the image when it's ready +def check_and_display_status(task_id): + url_task = "https://midjourney.chatgot.io/task" + headers = { + "Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJsaTE3NzE5MzMzNzAyQGdtYWlsLmNvbSIsImV4cGlyZSI6MTcwMzU0ODgwMDAwMCwicmFuZG9tIjoiYTExNTUyZWUyMWU4MTFhOTFiZGFmZTUwMWE0YTA0MDgifQ==.gOnk9jo6LidZSRB/kip94HfwvLCiPUSuL2pbCk+pUzg=", # Replace with your actual token + "Content-Type": "application/json" + } - if menu == menu1: - st.subheader(f"{menu1}") + task_status = 'pending' + with st.spinner('Waiting for image to be processed...'): + while task_status != 'finished': + task_info = check_task(task_id, url_task, headers) + task_status = task_info['data']['status'] + + if task_status == 'finished': + image_url = task_info['data']['image_url'] + st.success(f"Task completed: Image is available at {image_url}") + st.image(image_url) + break + else: + time.sleep(10) # This delay prevents spamming the API with requests - if menu == menu2: - st.subheader(f"{menu2}") +# Function to make request to check task status +def check_task(task_id, url, headers): + params_task = {'task_id': task_id} + response_task = requests.get(url, params=params_task, headers=headers) + return response_task.json() -if __name__ == '__main__': +if __name__ == "__main__": main()