Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108import osimport subprocessimport threadingimport timeimport gradio as grSTUDIO_HOST = "127.0.0.1"STUDIO_PORT = 8888# Keep Studio data inside the Spaceos.environ.setdefault("UNSLOTH_STUDIO_HOME","/tmp/unsloth_studio",)def start_studio():print("🦥 Starting Unsloth Studio...")os.makedirs(os.environ["UNSLOTH_STUDIO_HOME"], exist_ok=True)cmd = ["unsloth","studio","-H",STUDIO_HOST,"-p",str(STUDIO_PORT),]process = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.STDOUT,text=True,bufsize=1,)def log_output():for line in process.stdout:print("[Unsloth Studio]", line.rstrip())threading.Thread(target=log_output,daemon=True,).start()return processstudio_process = start_studio()def wait_for_studio():import urllib.requestfor _ in range(60):try:urllib.request.urlopen(f"http://{STUDIO_HOST}:{STUDIO_PORT}",timeout=1,)return Trueexcept Exception:time.sleep(1)return Falseif not wait_for_studio():raise RuntimeError("Unsloth Studio failed to start.")# Gradio Space wrapperwith gr.Blocks(title="Unsloth Studio") as demo:gr.Markdown("""# 🦥 Unsloth StudioUnsloth Studio is running inside this Hugging Face Space.""")gr.HTML(f"""<iframesrc="http://{STUDIO_HOST}:{STUDIO_PORT}"style="width: 100%;height: 900px;border: none;border-radius: 10px;"></iframe>""")if __name__ == "__main__":demo.launch(server_name="0.0.0.0",server_port=7860,)1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556import osimport sys# ---------------------------------------------------------# Unsloth Studio environment# ---------------------------------------------------------os.environ.setdefault("UNSLOTH_STUDIO_HOME","/tmp/unsloth_studio",)os.makedirs(os.environ["UNSLOTH_STUDIO_HOME"],exist_ok=True,)# ---------------------------------------------------------# Import the REAL Unsloth Studio FastAPI application# ---------------------------------------------------------from studio.backend.main import app as studio_app# ---------------------------------------------------------# Gradio# ---------------------------------------------------------import gradio as grwith gr.Blocks(title="Unsloth Studio") as demo:gr.Markdown("""# 🦥 Unsloth StudioFull Unsloth Studio running inside a Hugging Face Space.""")# ---------------------------------------------------------# Mount Studio into Gradio# ---------------------------------------------------------demo.app = studio_appif __name__ == "__main__":import uvicornuvicorn.run(studio_app,host="0.0.0.0",port=7860,)
app.py+29-81