alpercagann commited on
Commit
588dca4
Β·
1 Parent(s): 0b5616c

Update app to handle optional package imports

Browse files
Files changed (1) hide show
  1. app.py +54 -38
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 required packages
 
 
 
 
 
 
 
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
- # Simple Gradio interface
23
- import gradio as gr
 
 
 
 
24
 
25
- def test_download():
26
- """Test the download functionality"""
27
- try:
28
- from download_assets import download_test_file
29
- success = download_test_file()
30
- if success:
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
- result = f"Hello, {name}!\n\n"
42
-
43
  if torch_available:
44
- # Create a simple tensor to test PyTorch
45
- tensor = torch.rand(3, 3)
46
- result += f"PyTorch is working. Random tensor:\n{tensor}\n\n"
47
  else:
48
- result += "PyTorch is NOT available.\n\n"
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
- with gr.Tab("Hello Test"):
57
- gr.Markdown("Test basic functionality and PyTorch")
58
- name_input = gr.Textbox(label="Enter your name")
59
- hello_btn = gr.Button("Say Hello")
60
- hello_output = gr.Textbox(label="Output")
61
- hello_btn.click(fn=hello, inputs=name_input, outputs=hello_output)
62
 
63
- with gr.Tab("Download Test"):
64
- gr.Markdown("Test downloading files from Google Drive")
65
- download_btn = gr.Button("Test Download")
66
- download_output = gr.Textbox(label="Download Result")
67
- download_btn.click(fn=test_download, inputs=None, outputs=download_output)
 
 
 
 
 
 
 
 
 
 
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()