Nam Fam commited on
Commit ·
0947551
1
Parent(s): bb20cda
add files
Browse files- .dockerignore +10 -0
- Dockerfile +35 -0
- app.py +176 -0
- requirements.txt +2 -0
.dockerignore
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__
|
| 2 |
+
.gitignore
|
| 3 |
+
.env*
|
| 4 |
+
|
| 5 |
+
/modal
|
| 6 |
+
backend.py
|
| 7 |
+
/notebooks
|
| 8 |
+
|
| 9 |
+
README_1.md
|
| 10 |
+
|
Dockerfile
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# RunNow Gradio Frontend Dockerfile
|
| 2 |
+
# ---------------------------------
|
| 3 |
+
FROM python:3.10-slim
|
| 4 |
+
|
| 5 |
+
# Set environment variables
|
| 6 |
+
ENV PYTHONDONTWRITEBYTECODE=1 \
|
| 7 |
+
PYTHONUNBUFFERED=1
|
| 8 |
+
|
| 9 |
+
# Set workdir
|
| 10 |
+
WORKDIR /app
|
| 11 |
+
|
| 12 |
+
# Install system dependencies
|
| 13 |
+
RUN apt-get update && \
|
| 14 |
+
apt-get install -y --no-install-recommends \
|
| 15 |
+
build-essential \
|
| 16 |
+
git \
|
| 17 |
+
curl \
|
| 18 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 19 |
+
|
| 20 |
+
# Copy requirements (if exists) and install Python dependencies
|
| 21 |
+
COPY requirements.txt ./
|
| 22 |
+
RUN pip install --upgrade pip && \
|
| 23 |
+
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
|
| 24 |
+
|
| 25 |
+
# Copy the rest of the code
|
| 26 |
+
COPY . .
|
| 27 |
+
|
| 28 |
+
# Expose Gradio default port
|
| 29 |
+
EXPOSE 7860
|
| 30 |
+
|
| 31 |
+
# Set environment variables
|
| 32 |
+
ENV GRADIO_SERVER_NAME="0.0.0.0"
|
| 33 |
+
|
| 34 |
+
# Entrypoint
|
| 35 |
+
CMD ["python", "app.py"]
|
app.py
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
load_dotenv()
|
| 6 |
+
|
| 7 |
+
MAX_OUTPUTS = 100
|
| 8 |
+
|
| 9 |
+
RUNNOW_ENDPOINT_URL = os.getenv("RUNNOW_ENDPOINT_URL")
|
| 10 |
+
DEFAULT_RUNNOW_API_KEY = os.getenv("RUNNOW_API_KEY") or ""
|
| 11 |
+
|
| 12 |
+
def inference_interface(dependencies, code, api_key):
|
| 13 |
+
"""
|
| 14 |
+
Called when "Run" is clicked.
|
| 15 |
+
Calls the backend, then updates the UI state and filter.
|
| 16 |
+
"""
|
| 17 |
+
try:
|
| 18 |
+
# Use the provided API key if available, otherwise fallback to default
|
| 19 |
+
api_key = api_key or DEFAULT_RUNNOW_API_KEY
|
| 20 |
+
resp = requests.post(
|
| 21 |
+
RUNNOW_ENDPOINT_URL,
|
| 22 |
+
json={"dependencies": dependencies, "code": code},
|
| 23 |
+
headers={"x-api-key": api_key},
|
| 24 |
+
timeout=600 # Increased timeout for heavy installations
|
| 25 |
+
)
|
| 26 |
+
data = resp.json()
|
| 27 |
+
|
| 28 |
+
# Consolidate all outputs into a single dictionary
|
| 29 |
+
variables = {}
|
| 30 |
+
if "installation_logs" in data and data["installation_logs"].strip():
|
| 31 |
+
variables["installation_logs"] = data["installation_logs"]
|
| 32 |
+
|
| 33 |
+
if "error" in data:
|
| 34 |
+
variables["error"] = data["error"]
|
| 35 |
+
else:
|
| 36 |
+
if "locals" in data:
|
| 37 |
+
variables.update(data["locals"])
|
| 38 |
+
if "result" in data:
|
| 39 |
+
variables["result"] = data["result"]
|
| 40 |
+
|
| 41 |
+
if not variables:
|
| 42 |
+
variables["empty_response"] = str(data)
|
| 43 |
+
|
| 44 |
+
except Exception as e:
|
| 45 |
+
# Handle request exceptions (e.g., timeout, connection error)
|
| 46 |
+
variables = {"request_error": f"Request failed: {str(e)}"}
|
| 47 |
+
|
| 48 |
+
# Prepare UI updates
|
| 49 |
+
all_var_names = list(variables.keys())
|
| 50 |
+
|
| 51 |
+
# Create updates for the accordions
|
| 52 |
+
accordion_updates = []
|
| 53 |
+
for i in range(MAX_OUTPUTS):
|
| 54 |
+
if i < len(all_var_names):
|
| 55 |
+
var_name = all_var_names[i]
|
| 56 |
+
value = variables[var_name]
|
| 57 |
+
accordion_updates.append(gr.update(label=var_name, visible=False))
|
| 58 |
+
accordion_updates.append(gr.update(value=f"```\n{str(value)}\n```"))
|
| 59 |
+
else:
|
| 60 |
+
accordion_updates.append(gr.update(visible=False))
|
| 61 |
+
accordion_updates.append(gr.update(value=""))
|
| 62 |
+
|
| 63 |
+
# Return updates for: state, filter, default_output, and all accordions
|
| 64 |
+
return [
|
| 65 |
+
variables, # Update the state
|
| 66 |
+
gr.update(choices=all_var_names, value=[], visible=bool(all_var_names)), # Update the filter
|
| 67 |
+
gr.update(visible=False), # Hide default output when showing results
|
| 68 |
+
*accordion_updates
|
| 69 |
+
]
|
| 70 |
+
|
| 71 |
+
def update_display(selected_vars, all_vars):
|
| 72 |
+
"""
|
| 73 |
+
Called when the filter selection changes.
|
| 74 |
+
Shows/hides accordions instantly based on the selection.
|
| 75 |
+
"""
|
| 76 |
+
updates = []
|
| 77 |
+
all_var_names = list(all_vars.keys())
|
| 78 |
+
|
| 79 |
+
for i in range(MAX_OUTPUTS):
|
| 80 |
+
if i < len(all_var_names):
|
| 81 |
+
var_name = all_var_names[i]
|
| 82 |
+
is_visible = var_name in selected_vars
|
| 83 |
+
updates.append(gr.update(visible=is_visible)) # Accordion update
|
| 84 |
+
updates.append(gr.update()) # Markdown value doesn't need to change
|
| 85 |
+
else:
|
| 86 |
+
# Hide unused accordions
|
| 87 |
+
updates.append(gr.update(visible=False))
|
| 88 |
+
updates.append(gr.update())
|
| 89 |
+
|
| 90 |
+
return tuple(updates)
|
| 91 |
+
|
| 92 |
+
example_code = '''import pandas as pd
|
| 93 |
+
|
| 94 |
+
# Create a sample DataFrame
|
| 95 |
+
data = {'col1': [1, 2, 3], 'col2': ['A', 'B', 'C']}
|
| 96 |
+
df = pd.DataFrame(data=data)
|
| 97 |
+
|
| 98 |
+
# You can also create other variables
|
| 99 |
+
my_variable = "This is a test with pandas."
|
| 100 |
+
'''
|
| 101 |
+
|
| 102 |
+
def build_ui():
|
| 103 |
+
|
| 104 |
+
custom_css = """
|
| 105 |
+
.output-container {
|
| 106 |
+
border: 1px solid #e0e0e0;
|
| 107 |
+
border-radius: 8px;
|
| 108 |
+
padding: 20px;
|
| 109 |
+
margin-top: 10px;
|
| 110 |
+
min-height: 200px;
|
| 111 |
+
background-color: #f9f9f9;
|
| 112 |
+
}
|
| 113 |
+
.output-container > *:first-child {
|
| 114 |
+
margin-top: 0;
|
| 115 |
+
}
|
| 116 |
+
"""
|
| 117 |
+
|
| 118 |
+
with gr.Blocks(title="RunNow: Instant Python Code Execution", css=custom_css) as demo:
|
| 119 |
+
gr.Markdown("# 🚀 RunNow: Instant Python Code Execution\n\nRun any Python code instantly in the cloud with dynamic dependency installation!")
|
| 120 |
+
with gr.Row():
|
| 121 |
+
with gr.Column(scale=1):
|
| 122 |
+
api_key_input = gr.Textbox(label="API Key (bảo mật)", value=DEFAULT_RUNNOW_API_KEY, type="password", lines=1, placeholder="Nhập API key...")
|
| 123 |
+
dependencies_input = gr.Textbox(label="Dependencies (e.g., pip install, git clone)", value="pip install pandas", lines=3, placeholder="pip install numpy\ngit clone https://github.com/user/repo.git")
|
| 124 |
+
code_input = gr.Textbox(label="Your inference code (Python)", value=example_code, lines=14, show_copy_button=True, elem_id="code_input")
|
| 125 |
+
run_btn = gr.Button("Run", variant="primary")
|
| 126 |
+
with gr.Column(scale=2):
|
| 127 |
+
all_vars_state = gr.State(value={})
|
| 128 |
+
variable_filter = gr.CheckboxGroup(label="Filter variables to display", visible=False, interactive=True)
|
| 129 |
+
|
| 130 |
+
# Wrap output area in a container with custom styling
|
| 131 |
+
with gr.Column(elem_classes=["output-container"]):
|
| 132 |
+
# Default message shown before any results
|
| 133 |
+
default_output = gr.Markdown("""
|
| 134 |
+
<div style='text-align: center; margin: 2rem; color: #666;'>
|
| 135 |
+
<p>👋 Welcome to RunNow!</p>
|
| 136 |
+
<p>Your output will appear here after running the code.</p>
|
| 137 |
+
<p>Click the "Run" button to execute your code.</p>
|
| 138 |
+
</div>
|
| 139 |
+
""")
|
| 140 |
+
|
| 141 |
+
output_components = []
|
| 142 |
+
for _ in range(MAX_OUTPUTS):
|
| 143 |
+
with gr.Accordion("Output", visible=False) as acc:
|
| 144 |
+
md = gr.Markdown()
|
| 145 |
+
output_components.append(acc)
|
| 146 |
+
output_components.append(md)
|
| 147 |
+
|
| 148 |
+
# Event Listeners
|
| 149 |
+
|
| 150 |
+
run_event = run_btn.click(
|
| 151 |
+
fn=inference_interface,
|
| 152 |
+
inputs=[dependencies_input, code_input, api_key_input],
|
| 153 |
+
outputs=[all_vars_state, variable_filter, default_output, *output_components],
|
| 154 |
+
show_progress=True,
|
| 155 |
+
api_name="run_inference"
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
# Show loading indicator and clear default message when running
|
| 159 |
+
run_event.then(
|
| 160 |
+
fn=lambda: gr.update(visible=False),
|
| 161 |
+
inputs=None,
|
| 162 |
+
outputs=default_output,
|
| 163 |
+
queue=False
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
variable_filter.change(
|
| 167 |
+
fn=update_display,
|
| 168 |
+
inputs=[variable_filter, all_vars_state],
|
| 169 |
+
outputs=output_components
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
return demo
|
| 173 |
+
|
| 174 |
+
if __name__ == "__main__":
|
| 175 |
+
demo = build_ui()
|
| 176 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
python-dotenv
|