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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import subprocess
|
3 |
+
import os
|
4 |
+
import logging
|
5 |
+
import json
|
6 |
+
import requests
|
7 |
+
from streamlit.components.v1 import html
|
8 |
+
|
9 |
+
# Configure logging
|
10 |
+
logging.basicConfig(filename='supercoder_launcher.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
11 |
+
|
12 |
+
# Define repository and project directories
|
13 |
+
REPO_DIR = os.path.join(os.getcwd(), "SuperCoder")
|
14 |
+
PROJECT_DIR = os.getcwd()
|
15 |
+
|
16 |
+
# Create requirements.txt
|
17 |
+
def create_requirements_txt():
|
18 |
+
requirements_content = "streamlit\n"
|
19 |
+
try:
|
20 |
+
with open(os.path.join(REPO_DIR, 'requirements.txt'), 'w') as f:
|
21 |
+
f.write(requirements_content.strip())
|
22 |
+
logging.info("requirements.txt created successfully.")
|
23 |
+
except Exception as e:
|
24 |
+
logging.error(f"Error creating requirements.txt: {str(e)}")
|
25 |
+
|
26 |
+
# Create Dockerfile
|
27 |
+
def create_dockerfile():
|
28 |
+
dockerfile_content = """
|
29 |
+
FROM python:3.9-slim
|
30 |
+
WORKDIR /app
|
31 |
+
# Install git
|
32 |
+
RUN apt-get update && apt-get install -y git
|
33 |
+
# Copy requirements first to leverage Docker cache
|
34 |
+
COPY requirements.txt .
|
35 |
+
RUN pip install -r requirements.txt
|
36 |
+
# Copy the rest of the application
|
37 |
+
COPY . .
|
38 |
+
# Expose the Streamlit port
|
39 |
+
EXPOSE 8501
|
40 |
+
# Health check to ensure the container is running properly
|
41 |
+
HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 CMD curl -f http://localhost:8501/_stcore/health || exit 1
|
42 |
+
# Command to run the application
|
43 |
+
ENTRYPOINT ["streamlit", "run", "app.py", "--server.address", "0.0.0.0"]
|
44 |
+
"""
|
45 |
+
try:
|
46 |
+
with open(os.path.join(REPO_DIR, 'Dockerfile'), 'w') as f:
|
47 |
+
f.write(dockerfile_content.strip())
|
48 |
+
logging.info("Dockerfile created successfully.")
|
49 |
+
except Exception as e:
|
50 |
+
logging.error(f"Error creating Dockerfile: {str(e)}")
|
51 |
+
|
52 |
+
# Function to handle Hugging Face models, datasets, and spaces
|
53 |
+
def handle_huggingface_request(link, instruction):
|
54 |
+
# Example logic for handling Hugging Face models, datasets, and spaces
|
55 |
+
if "huggingface.co" in link:
|
56 |
+
# Process the link based on the type (model, dataset, space)
|
57 |
+
if "models" in link:
|
58 |
+
# Logic to deploy or modify a model
|
59 |
+
logging.info(f"Processing Hugging Face model: {link}")
|
60 |
+
# Add your model handling logic here
|
61 |
+
elif "datasets" in link:
|
62 |
+
# Logic to parse or combine datasets
|
63 |
+
logging.info(f"Processing Hugging Face dataset: {link}")
|
64 |
+
# Add your dataset handling logic here
|
65 |
+
elif "spaces" in link:
|
66 |
+
# Logic to modify and deploy a space
|
67 |
+
logging.info(f"Processing Hugging Face space: {link}")
|
68 |
+
# Add your space handling logic here
|
69 |
+
elif "github.com" in link:
|
70 |
+
# Logic to handle GitHub repositories
|
71 |
+
logging.info(f"Processing GitHub repository: {link}")
|
72 |
+
# Add your GitHub handling logic here
|
73 |
+
else:
|
74 |
+
logging.warning(f"Unsupported link: {link}")
|
75 |
+
|
76 |
+
# Streamlit app interface
|
77 |
+
def main():
|
78 |
+
st.set_page_config(page_title="SuperCoder Launcher", page_icon="🚀")
|
79 |
+
st.title("🚀 SuperCoder Launcher")
|
80 |
+
st.markdown("""
|
81 |
+
This application helps you set up and run SuperCoder.
|
82 |
+
Follow the steps below to get started.
|
83 |
+
""")
|
84 |
+
|
85 |
+
# Initialize session state
|
86 |
+
if 'setup_complete' not in st.session_state:
|
87 |
+
st.session_state.setup_complete = False
|
88 |
+
if 'install_complete' not in st.session_state:
|
89 |
+
st.session_state.install_complete = False
|
90 |
+
if 'terminal_output' not in st.session_state:
|
91 |
+
st.session_state.terminal_output = []
|
92 |
+
|
93 |
+
terminal_container = st.empty()
|
94 |
+
|
95 |
+
def update_terminal(output):
|
96 |
+
st.session_state.terminal_output.append(output)
|
97 |
+
terminal_container.code('\n'.join(st.session_state.terminal_output))
|
98 |
+
|
99 |
+
# Function to execute shell commands
|
100 |
+
def execute_command(command: str, cwd: str = None) -> bool:
|
101 |
+
try:
|
102 |
+
process = subprocess.Popen(
|
103 |
+
command,
|
104 |
+
shell=True,
|
105 |
+
cwd=cwd,
|
106 |
+
stdout=subprocess.PIPE,
|
107 |
+
stderr=subprocess.PIPE,
|
108 |
+
text=True
|
109 |
+
)
|
110 |
+
|
111 |
+
while True:
|
112 |
+
output = process.stdout.readline()
|
113 |
+
if output == '' and process.poll() is not None:
|
114 |
+
break
|
115 |
+
if output:
|
116 |
+
update_terminal(output.strip())
|
117 |
+
logging.info(output.strip())
|
118 |
+
|
119 |
+
stderr = process.stderr.read()
|
120 |
+
if stderr:
|
121 |
+
update_terminal(f"Error: {stderr.strip()}")
|
122 |
+
logging.error(stderr.strip())
|
123 |
+
|
124 |
+
return process.poll() == 0
|
125 |
+
except Exception as e:
|
126 |
+
update_terminal(f"Error executing command: {str(e)}")
|
127 |
+
logging.error(f"Error executing command: {str(e)}")
|
128 |
+
return False
|
129 |
+
|
130 |
+
# Input fields for Hugging Face models, datasets, spaces, and GitHub links
|
131 |
+
st.subheader("Input Links")
|
132 |
+
huggingface_link = st.text_input("Hugging Face Model/Dataset/Space Link")
|
133 |
+
github_link = st.text_input("GitHub Repository Link")
|
134 |
+
instruction = st.text_area("Instructions for Processing")
|
135 |
+
|
136 |
+
if st.button("Submit"):
|
137 |
+
if huggingface_link or github_link:
|
138 |
+
if huggingface_link:
|
139 |
+
handle_huggingface_request(huggingface_link, instruction)
|
140 |
+
if github_link:
|
141 |
+
handle_huggingface_request(github_link, instruction)
|
142 |
+
st.success("Request processed. Check terminal for details.")
|
143 |
+
else:
|
144 |
+
st.error("Please provide at least one link.")
|
145 |
+
|
146 |
+
# Setup steps
|
147 |
+
with st.expander("1. Clone Repository", expanded=not st.session_state.setup_complete):
|
148 |
+
if st.button("Clone SuperCoder"):
|
149 |
+
with st.spinner("Cloning repository..."):
|
150 |
+
if os.path.exists(REPO_DIR):
|
151 |
+
update_terminal("Repository already exists. Skipping clone.")
|
152 |
+
logging.info("Repository already exists. Skipping clone.")
|
153 |
+
else:
|
154 |
+
success = execute_command(
|
155 |
+
"git clone https://github.com/TransformerOptimus/SuperCoder",
|
156 |
+
PROJECT_DIR
|
157 |
+
)
|
158 |
+
if success:
|
159 |
+
create_requirements_txt()
|
160 |
+
create_dockerfile()
|
161 |
+
st.session_state.setup_complete = True
|
162 |
+
st.success("Repository cloned successfully.")
|
163 |
+
else:
|
164 |
+
st.error("Failed to clone the repository. Check logs for details.")
|
165 |
+
|
166 |
+
with st.expander("2. Build and Run Docker Container", expanded=st.session_state.setup_complete and not st.session_state.install_complete):
|
167 |
+
if st.button("Build and Run Container", disabled=not st.session_state.setup_complete):
|
168 |
+
with st.spinner("Building and running Docker container..."):
|
169 |
+
# Build Docker image
|
170 |
+
build_success = execute_command(
|
171 |
+
"docker build -t supercoder .",
|
172 |
+
REPO_DIR
|
173 |
+
)
|
174 |
+
|
175 |
+
if build_success:
|
176 |
+
# Run Docker container
|
177 |
+
run_success = execute_command(
|
178 |
+
"docker run -d -p 8501:8501 --name supercoder_container supercoder",
|
179 |
+
REPO_DIR
|
180 |
+
)
|
181 |
+
|
182 |
+
if run_success:
|
183 |
+
st.session_state.install_complete = True
|
184 |
+
st.success("Docker container is running!")
|
185 |
+
else:
|
186 |
+
st.error("Failed to run the Docker container. Check logs for details.")
|
187 |
+
else:
|
188 |
+
st.error("Failed to build the Docker image. Check logs for details.")
|
189 |
+
|
190 |
+
with st.expander("3. Access Application", expanded=st.session_state.install_complete):
|
191 |
+
if st.session_state.install_complete:
|
192 |
+
st.markdown("""
|
193 |
+
The application is running! Access it at:
|
194 |
+
|
195 |
+
[http://localhost:8501](http://localhost:8501)
|
196 |
+
""")
|
197 |
+
|
198 |
+
if st.button("Stop Container"):
|
199 |
+
execute_command("docker stop supercoder_container")
|
200 |
+
execute_command("docker rm supercoder_container")
|
201 |
+
st.session_state.install_complete = False
|
202 |
+
st.success("Docker container stopped and removed.")
|
203 |
+
|
204 |
+
# Display terminal output
|
205 |
+
st.markdown("### Terminal Output")
|
206 |
+
terminal_container.code('\n'.join(st.session_state.terminal_output))
|
207 |
+
|
208 |
+
# Add a clear terminal button
|
209 |
+
if st.button("Clear Terminal"):
|
210 |
+
st.session_state.terminal_output = []
|
211 |
+
st.success("Terminal cleared.")
|
212 |
+
|
213 |
+
# Add a reset button
|
214 |
+
if st.button("Reset Setup"):
|
215 |
+
if st.session_state.install_complete:
|
216 |
+
execute_command("docker stop supercoder_container")
|
217 |
+
execute_command("docker rm supercoder_container")
|
218 |
+
st.session_state.setup_complete = False
|
219 |
+
st.session_state.install_complete = False
|
220 |
+
st.session_state.terminal_output = []
|
221 |
+
st.success("Setup reset.")
|
222 |
+
|
223 |
+
# Embed an interactive terminal
|
224 |
+
terminal_html = """
|
225 |
+
<div id="terminal" style="width: 100%; height: 400px; border: 1px solid black;"></div>
|
226 |
+
<script>
|
227 |
+
var term = new Terminal();
|
228 |
+
var fitAddon = new Fit Addon.FitAddon();
|
229 |
+
term.loadAddon(fitAddon);
|
230 |
+
term.open(document.getElementById('terminal'));
|
231 |
+
fitAddon.fit();
|
232 |
+
term.onData(function(data) {
|
233 |
+
// Send data to Python backend
|
234 |
+
var xhr = new XMLHttpRequest();
|
235 |
+
xhr.open("POST", "/send_data", true);
|
236 |
+
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
|
237 |
+
xhr.send(JSON.stringify({data: data}));
|
238 |
+
});
|
239 |
+
// Receive data from Python backend
|
240 |
+
window.addEventListener('message', function(event) {
|
241 |
+
var message = event.data;
|
242 |
+
if (message.type === 'terminal_output') {
|
243 |
+
term.write(message.output);
|
244 |
+
}
|
245 |
+
});
|
246 |
+
</script>
|
247 |
+
"""
|
248 |
+
|
249 |
+
html(terminal_html, height=400)
|
250 |
+
|
251 |
+
if __name__ == "__main__":
|
252 |
+
main()
|