Update app.py
Browse files
app.py
CHANGED
|
@@ -1,108 +1,56 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
import threading
|
| 4 |
-
import time
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
STUDIO_HOST = "127.0.0.1"
|
| 10 |
-
STUDIO_PORT = 8888
|
| 11 |
|
| 12 |
-
# Keep Studio data inside the Space
|
| 13 |
os.environ.setdefault(
|
| 14 |
"UNSLOTH_STUDIO_HOME",
|
| 15 |
"/tmp/unsloth_studio",
|
| 16 |
)
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
os.makedirs(os.environ["UNSLOTH_STUDIO_HOME"], exist_ok=True)
|
| 23 |
-
|
| 24 |
-
cmd = [
|
| 25 |
-
"unsloth",
|
| 26 |
-
"studio",
|
| 27 |
-
"-H",
|
| 28 |
-
STUDIO_HOST,
|
| 29 |
-
"-p",
|
| 30 |
-
str(STUDIO_PORT),
|
| 31 |
-
]
|
| 32 |
-
|
| 33 |
-
process = subprocess.Popen(
|
| 34 |
-
cmd,
|
| 35 |
-
stdout=subprocess.PIPE,
|
| 36 |
-
stderr=subprocess.STDOUT,
|
| 37 |
-
text=True,
|
| 38 |
-
bufsize=1,
|
| 39 |
-
)
|
| 40 |
-
|
| 41 |
-
def log_output():
|
| 42 |
-
for line in process.stdout:
|
| 43 |
-
print("[Unsloth Studio]", line.rstrip())
|
| 44 |
-
|
| 45 |
-
threading.Thread(
|
| 46 |
-
target=log_output,
|
| 47 |
-
daemon=True,
|
| 48 |
-
).start()
|
| 49 |
-
|
| 50 |
-
return process
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
studio_process = start_studio()
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
def wait_for_studio():
|
| 57 |
-
import urllib.request
|
| 58 |
-
|
| 59 |
-
for _ in range(60):
|
| 60 |
-
try:
|
| 61 |
-
urllib.request.urlopen(
|
| 62 |
-
f"http://{STUDIO_HOST}:{STUDIO_PORT}",
|
| 63 |
-
timeout=1,
|
| 64 |
-
)
|
| 65 |
-
return True
|
| 66 |
-
except Exception:
|
| 67 |
-
time.sleep(1)
|
| 68 |
|
| 69 |
-
|
| 70 |
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
|
| 73 |
-
raise RuntimeError(
|
| 74 |
-
"Unsloth Studio failed to start."
|
| 75 |
-
)
|
| 76 |
|
| 77 |
|
| 78 |
-
# Gradio Space wrapper
|
| 79 |
with gr.Blocks(title="Unsloth Studio") as demo:
|
| 80 |
|
| 81 |
gr.Markdown(
|
| 82 |
"""
|
| 83 |
# 🦥 Unsloth Studio
|
| 84 |
|
| 85 |
-
Unsloth Studio
|
| 86 |
"""
|
| 87 |
)
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
height: 900px;
|
| 96 |
-
border: none;
|
| 97 |
-
border-radius: 10px;
|
| 98 |
-
"
|
| 99 |
-
></iframe>
|
| 100 |
-
"""
|
| 101 |
-
)
|
| 102 |
|
| 103 |
|
| 104 |
if __name__ == "__main__":
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
| 108 |
)
|
|
|
|
| 1 |
import os
|
| 2 |
+
import sys
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
# ---------------------------------------------------------
|
| 5 |
+
# Unsloth Studio environment
|
| 6 |
+
# ---------------------------------------------------------
|
|
|
|
|
|
|
| 7 |
|
|
|
|
| 8 |
os.environ.setdefault(
|
| 9 |
"UNSLOTH_STUDIO_HOME",
|
| 10 |
"/tmp/unsloth_studio",
|
| 11 |
)
|
| 12 |
|
| 13 |
+
os.makedirs(
|
| 14 |
+
os.environ["UNSLOTH_STUDIO_HOME"],
|
| 15 |
+
exist_ok=True,
|
| 16 |
+
)
|
| 17 |
|
| 18 |
+
# ---------------------------------------------------------
|
| 19 |
+
# Import the REAL Unsloth Studio FastAPI application
|
| 20 |
+
# ---------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
from studio.backend.main import app as studio_app
|
| 23 |
|
| 24 |
+
# ---------------------------------------------------------
|
| 25 |
+
# Gradio
|
| 26 |
+
# ---------------------------------------------------------
|
| 27 |
|
| 28 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
|
|
|
|
| 31 |
with gr.Blocks(title="Unsloth Studio") as demo:
|
| 32 |
|
| 33 |
gr.Markdown(
|
| 34 |
"""
|
| 35 |
# 🦥 Unsloth Studio
|
| 36 |
|
| 37 |
+
Full Unsloth Studio running inside a Hugging Face Space.
|
| 38 |
"""
|
| 39 |
)
|
| 40 |
|
| 41 |
+
|
| 42 |
+
# ---------------------------------------------------------
|
| 43 |
+
# Mount Studio into Gradio
|
| 44 |
+
# ---------------------------------------------------------
|
| 45 |
+
|
| 46 |
+
demo.app = studio_app
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
|
| 49 |
if __name__ == "__main__":
|
| 50 |
+
import uvicorn
|
| 51 |
+
|
| 52 |
+
uvicorn.run(
|
| 53 |
+
studio_app,
|
| 54 |
+
host="0.0.0.0",
|
| 55 |
+
port=7860,
|
| 56 |
)
|