提交 f34f457c 编写于 作者: Q qq_44798533

Auto Commit

上级 0520122c
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()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册