Spaces:
Sleeping
Sleeping
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() |