joelorellana commited on
Commit
c89879a
·
1 Parent(s): 05efb8e

using another version of demo

Browse files
Files changed (1) hide show
  1. app.py +43 -40
app.py CHANGED
@@ -6,24 +6,23 @@ from stability_generate_img import generate_image_with_stability
6
  from finetune_generate_img import generate_finetuned_img
7
  from midjourney_generate_img import midjourney_generate_img
8
 
9
- # Page configuration
 
10
  st.set_page_config(layout="wide")
11
  st.sidebar.title("API Keys")
12
  st.markdown("<h1 style='text-align: center; color: grey;'>Image Generation App</h1>", unsafe_allow_html=True)
13
  st.text("Prepared by [email protected] for fomo.ai")
14
 
15
- # List of API key names
16
  api_key_names = ["OPENAI_API_KEY", "MIDJOURNEY_GOAPI_TOKEN", "REPLICATE_API_TOKEN", "STABILITY_API_KEY"]
17
 
18
- # Initialize session state if it does not exist
19
  if 'api_keys' not in st.session_state:
20
  st.session_state['api_keys'] = {key_name: "" for key_name in api_key_names}
21
  if 'editable_prompt' not in st.session_state:
22
  st.session_state['editable_prompt'] = ""
23
- if 'upload_completed' not in st.session_state:
24
- st.session_state['upload_completed'] = False
25
 
26
- # Define a function to request and update API keys
27
  def request_and_update_api_keys():
28
  all_keys_entered = True
29
  for key_name in api_key_names:
@@ -35,49 +34,53 @@ def request_and_update_api_keys():
35
 
36
  all_keys_entered = request_and_update_api_keys()
37
 
38
- # Check if all API keys have been entered
39
  if all_keys_entered:
40
- # Section to upload the image
41
- uploaded_file = st.file_uploader("Upload Image to analyze", type=['jpg', 'jpeg', 'png'], on_change=lambda: setattr(st.session_state, 'upload_completed', True))
42
  if uploaded_file is not None:
43
- st.session_state['upload_completed'] = True
44
- left_co, cent_co, _ = st.columns([1, 2, 1])
45
  with cent_co:
46
  st.image(uploaded_file, caption="Uploaded Image")
47
- _, right_co = st.columns([5, 1])
48
- if right_co.button("Generate Prompt"):
49
  with st.spinner("Generating Prompt..."):
50
  with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as temp_file:
51
  temp_file.write(uploaded_file.getvalue())
52
  temp_path = temp_file.name
53
  api_key = st.session_state['api_keys']['OPENAI_API_KEY']
54
  prompt = generate_prompt_with_vision(temp_path, api_key=api_key)
55
- st.session_state['editable_prompt'] = prompt
56
- st.session_state['upload_completed'] = False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
- if st.session_state['upload_completed']:
59
- # Maintain the content of the editable prompt after generating images
60
- editable_prompt = st.text_area("Edit the prompt as needed:", value=st.session_state['editable_prompt'], placeholder="Enter your prompt here...", height=150, key='editable_prompt', on_change=lambda: st.session_state.update(editable_prompt=editable_prompt))
61
- if st.button("Generate New Image", key='generate_image_btn'):
62
- col1, col2, col3, col4 = st.columns(4)
63
- with col1:
64
- with st.spinner("Generating DALL·E Image..."):
65
- result_path_1 = generate_img_with_dalle(st.session_state['editable_prompt'], api_key=st.session_state['api_keys']['OPENAI_API_KEY'])
66
- st.image(result_path_1, caption="DALL·E Image")
67
- with col2:
68
- with st.spinner("Generating Stable Diffusion Image..."):
69
- result_path_2 = generate_image_with_stability(st.session_state['editable_prompt'], api_key=st.session_state['api_keys']['STABILITY_API_KEY'])
70
- st.image(result_path_2, caption="Stable Diffusion Image")
71
- with col3:
72
- with st.spinner("Generating Finetuning Image..."):
73
- result_path_3 = generate_finetuned_img(st.session_state['editable_prompt'], api_key=st.session_state['api_keys']['REPLICATE_API_TOKEN'])
74
- st.image(result_path_3, caption="Finetuned SDXL Image")
75
- with col4:
76
- with st.spinner("Generating Midjourney Image..."):
77
- result_path_4 = midjourney_generate_img(st.session_state['editable_prompt'], api_key=st.session_state['api_keys']['MIDJOURNEY_GOAPI_TOKEN'])
78
- st.image(result_path_4, caption="Midjourney Image")
79
- # Update the prompt in session state to keep the text
80
- st.session_state['editable_prompt'] = editable_prompt
81
- st.session_state['upload_completed'] = False # Disable the text area and button after generating images
82
  else:
83
- st.warning('Please enter all required API keys to proceed.', icon="⚠️")
 
6
  from finetune_generate_img import generate_finetuned_img
7
  from midjourney_generate_img import midjourney_generate_img
8
 
9
+
10
+ # Configuración de la página
11
  st.set_page_config(layout="wide")
12
  st.sidebar.title("API Keys")
13
  st.markdown("<h1 style='text-align: center; color: grey;'>Image Generation App</h1>", unsafe_allow_html=True)
14
  st.text("Prepared by [email protected] for fomo.ai")
15
 
16
+ # Lista de nombres de las API keys
17
  api_key_names = ["OPENAI_API_KEY", "MIDJOURNEY_GOAPI_TOKEN", "REPLICATE_API_TOKEN", "STABILITY_API_KEY"]
18
 
19
+ # Inicializar el estado de la sesión si no existe
20
  if 'api_keys' not in st.session_state:
21
  st.session_state['api_keys'] = {key_name: "" for key_name in api_key_names}
22
  if 'editable_prompt' not in st.session_state:
23
  st.session_state['editable_prompt'] = ""
 
 
24
 
25
+ # Definir una función para solicitar y actualizar las API keys
26
  def request_and_update_api_keys():
27
  all_keys_entered = True
28
  for key_name in api_key_names:
 
34
 
35
  all_keys_entered = request_and_update_api_keys()
36
 
37
+ # Revisar si todas las API keys han sido ingresadas
38
  if all_keys_entered:
39
+ # Sección para subir la imagen
40
+ uploaded_file = st.file_uploader("Upload Image to analyze", type=['jpg', 'jpeg', 'png'])
41
  if uploaded_file is not None:
42
+ left_co, cent_co,last_co = st.columns(3)
 
43
  with cent_co:
44
  st.image(uploaded_file, caption="Uploaded Image")
45
+ # Botón para generar el prompt solo si hay una imagen subida
46
+ if st.button("Generate Prompt"):
47
  with st.spinner("Generating Prompt..."):
48
  with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as temp_file:
49
  temp_file.write(uploaded_file.getvalue())
50
  temp_path = temp_file.name
51
  api_key = st.session_state['api_keys']['OPENAI_API_KEY']
52
  prompt = generate_prompt_with_vision(temp_path, api_key=api_key)
53
+ st.success("Done!")
54
+ st.session_state['editable_prompt'] = prompt # Actualizar el prompt en el estado de la sesión
55
+
56
+ editable_prompt = st.text_area("Edit the prompt as needed:", placeholder="Enter your prompt here...", height=150, key='editable_prompt', label_visibility='hidden')
57
+
58
+ col1, col2, col3, col4 = st.columns(4)
59
+
60
+ if st.session_state['editable_prompt'] and st.button("Generate New Image"):
61
+ with col1:
62
+ with st.spinner("Generating DALL·E Image..."):
63
+ result_path_1 = generate_img_with_dalle(st.session_state['editable_prompt'], api_key=st.session_state['api_keys']['OPENAI_API_KEY'])
64
+ st.success("Generated DALL·E Image!")
65
+ st.image(result_path_1, caption="DALL·E Image")
66
+
67
+ with col2:
68
+ with st.spinner("Generating Stable Diffusion Image..."):
69
+ result_path_2 = generate_image_with_stability(st.session_state['editable_prompt'], api_key=st.session_state['api_keys']['STABILITY_API_KEY'])
70
+ st.success("Generated Stable Diffusion Image!")
71
+ st.image(result_path_2, caption="Stable Diffusion Image")
72
+
73
+ with col3:
74
+ with st.spinner("Generating Finetuning Image..."):
75
+ result_path_3 = generate_finetuned_img(st.session_state['editable_prompt'], api_key=st.session_state['api_keys']['REPLICATE_API_TOKEN'])
76
+ st.success("Generated Image using a finetuned model!")
77
+ st.image(result_path_3, caption="Finetuned SDXL Image")
78
+
79
+ with col4:
80
+ with st.spinner("Generating Midjourney Image..."):
81
+ result_path_4 = midjourney_generate_img(st.session_state['editable_prompt'], api_key=st.session_state['api_keys']['MIDJOURNEY_GOAPI_TOKEN'])
82
+ st.success("Generated Midjourney Image!")
83
+ st.image(result_path_4, caption="Midjourney Image")
84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  else:
86
+ st.warning('Please enter all required API keys to proceed.', icon="⚠️")