samadi10 commited on
Commit
2c84e5f
1 Parent(s): 5c44aa4

chore: App file generated

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import streamlit.components.v1 as components
3
+ import subprocess
4
+ import os
5
+ import glob
6
+
7
+ # Function to run the model and generate HTML
8
+ def generate_html(text_input):
9
+ command = [
10
+ "python", "generate.py",
11
+ "--resume-pth", "output/vq/2023-07-19-04-17-17_12_VQVAE_20batchResetNRandom_8192_32/net_last.pth",
12
+ "--resume-trans", "output/t2m/2023-10-10-03-17-01_HML3D_44_crsAtt2lyr_mask0.5-1/net_last.pth",
13
+ "--text", text_input,
14
+ "--length", str(st.session_state.length)
15
+ ]
16
+ # Run the command
17
+ subprocess.run(command, check=True)
18
+ # Return the path of the latest HTML file in the output directory
19
+ return find_latest_html_file('output')
20
+
21
+ # Function to find the latest HTML file in the specified directory
22
+ def find_latest_html_file(base_path):
23
+ # List all HTML files in the output directory and find the latest one
24
+ list_of_files = glob.glob(f'{base_path}/*.html')
25
+ if not list_of_files:
26
+ return None
27
+ latest_file = max(list_of_files, key=os.path.getctime)
28
+ return latest_file
29
+
30
+ # Streamlit app
31
+ components.html("""
32
+ <h1 style='text-align: center; color: white;'>MMM Model Demo</h1>
33
+ """,
34
+ height=100)
35
+
36
+ # Initialize session state variable for text input
37
+ text_input = st.text_area("Enter text here:", value="", key="text_input")
38
+ length = st.number_input("Length of the generated text:", value=156, key="length")
39
+
40
+ # Store the input text in session state
41
+ st.session_state.input_text = text_input
42
+ st.session_state.length = length
43
+
44
+ # Button trigger to generate HTML
45
+ if st.button("Generate HTML"):
46
+ st.session_state.generate_html = True
47
+ else:
48
+ st.session_state.generate_html = False
49
+
50
+ # Check if HTML generation was triggered and input is not empty
51
+ if st.session_state.text_input and st.session_state.generate_html:
52
+ html_file_path = generate_html(st.session_state.text_input) # Uses session state
53
+ if html_file_path and os.path.exists(html_file_path):
54
+ with open(html_file_path, 'r') as file:
55
+ html_content = file.read()
56
+
57
+ # Embed HTML content in an iframe
58
+ iframe = f"<iframe srcdoc='{html_content}' style='width:100%; height:800px; border:none;'></iframe>"
59
+ components.html(iframe, height=800)
60
+ else:
61
+ st.error("Generated HTML file not found.")