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