Spaces:
Runtime error
Runtime error
Commit
Β·
588dca4
1
Parent(s):
0b5616c
Update app to handle optional package imports
Browse files
app.py
CHANGED
|
@@ -7,7 +7,14 @@ print(f"Python version: {sys.version}")
|
|
| 7 |
print(f"Working directory: {os.getcwd()}")
|
| 8 |
print(f"Directory contents: {os.listdir('.')}")
|
| 9 |
|
| 10 |
-
# Import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
try:
|
| 12 |
import torch
|
| 13 |
print(f"PyTorch version: {torch.__version__}")
|
|
@@ -17,54 +24,63 @@ try:
|
|
| 17 |
torch_available = True
|
| 18 |
except ImportError as e:
|
| 19 |
print(f"PyTorch import error: {e}")
|
| 20 |
-
torch_available = False
|
| 21 |
|
| 22 |
-
|
| 23 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
return "Download test successful! Check the assets folder."
|
| 32 |
-
else:
|
| 33 |
-
return "Download test failed. See logs for details."
|
| 34 |
-
except Exception as e:
|
| 35 |
-
return f"Error while testing download: {str(e)}"
|
| 36 |
|
|
|
|
| 37 |
def hello(name):
|
| 38 |
if not name:
|
| 39 |
name = "World"
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
if torch_available:
|
| 44 |
-
|
| 45 |
-
tensor = torch.rand(3, 3)
|
| 46 |
-
result += f"PyTorch is working. Random tensor:\n{tensor}\n\n"
|
| 47 |
else:
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
return result
|
| 51 |
-
|
| 52 |
-
# Create the Gradio interface with tabs
|
| 53 |
-
with gr.Blocks(title="SonicDiffusion - Setup Tests") as demo:
|
| 54 |
-
gr.Markdown("# SonicDiffusion - Environment Tests")
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
hello_output = gr.Textbox(label="Output")
|
| 61 |
-
hello_btn.click(fn=hello, inputs=name_input, outputs=hello_output)
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
demo.launch()
|
|
|
|
| 7 |
print(f"Working directory: {os.getcwd()}")
|
| 8 |
print(f"Directory contents: {os.listdir('.')}")
|
| 9 |
|
| 10 |
+
# Import gradio first - this is our most essential dependency
|
| 11 |
+
import gradio as gr
|
| 12 |
+
|
| 13 |
+
# Try importing other packages (but don't fail if they're missing)
|
| 14 |
+
torch_available = False
|
| 15 |
+
transformers_available = False
|
| 16 |
+
diffusers_available = False
|
| 17 |
+
|
| 18 |
try:
|
| 19 |
import torch
|
| 20 |
print(f"PyTorch version: {torch.__version__}")
|
|
|
|
| 24 |
torch_available = True
|
| 25 |
except ImportError as e:
|
| 26 |
print(f"PyTorch import error: {e}")
|
|
|
|
| 27 |
|
| 28 |
+
try:
|
| 29 |
+
import transformers
|
| 30 |
+
print(f"Transformers version: {transformers.__version__}")
|
| 31 |
+
transformers_available = True
|
| 32 |
+
except ImportError as e:
|
| 33 |
+
print(f"Transformers import error: {e}")
|
| 34 |
|
| 35 |
+
try:
|
| 36 |
+
import diffusers
|
| 37 |
+
print(f"Diffusers version: {diffusers.__version__}")
|
| 38 |
+
diffusers_available = True
|
| 39 |
+
except ImportError as e:
|
| 40 |
+
print(f"Diffusers import error: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
# Simple demo interface
|
| 43 |
def hello(name):
|
| 44 |
if not name:
|
| 45 |
name = "World"
|
| 46 |
|
| 47 |
+
status = []
|
|
|
|
| 48 |
if torch_available:
|
| 49 |
+
status.append("PyTorch β")
|
|
|
|
|
|
|
| 50 |
else:
|
| 51 |
+
status.append("PyTorch β")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
if transformers_available:
|
| 54 |
+
status.append("Transformers β")
|
| 55 |
+
else:
|
| 56 |
+
status.append("Transformers β")
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
if diffusers_available:
|
| 59 |
+
status.append("Diffusers β")
|
| 60 |
+
else:
|
| 61 |
+
status.append("Diffusers β")
|
| 62 |
+
|
| 63 |
+
return f"Hello, {name}!\n\nPackage Status:\n" + "\n".join(status)
|
| 64 |
+
|
| 65 |
+
# Create the Gradio interface
|
| 66 |
+
demo = gr.Interface(
|
| 67 |
+
fn=hello,
|
| 68 |
+
inputs="text",
|
| 69 |
+
outputs="text",
|
| 70 |
+
title="SonicDiffusion - Setup Status",
|
| 71 |
+
description="This app shows which packages are successfully installed."
|
| 72 |
+
)
|
| 73 |
|
| 74 |
if __name__ == "__main__":
|
| 75 |
+
# Try installing packages at runtime if they're not available
|
| 76 |
+
if not torch_available:
|
| 77 |
+
print("Attempting to install PyTorch...")
|
| 78 |
+
try:
|
| 79 |
+
import subprocess
|
| 80 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", "torch==2.0.1"])
|
| 81 |
+
print("PyTorch installed successfully!")
|
| 82 |
+
except Exception as e:
|
| 83 |
+
print(f"Error installing PyTorch: {e}")
|
| 84 |
+
|
| 85 |
+
# Launch the demo
|
| 86 |
demo.launch()
|