m7n commited on
Commit
594ffdd
·
verified ·
1 Parent(s): a3eebe6

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +12 -53
main.py CHANGED
@@ -1,102 +1,61 @@
1
  import os
2
  from pathlib import Path
3
- import json
4
- import base64
5
  from datetime import datetime
6
- import time
7
 
8
-
9
- # Standard imports
10
  import gradio as gr
11
- from fastapi import FastAPI, Request
12
  from fastapi.staticfiles import StaticFiles
13
  import uvicorn
14
 
15
- # Hugging Face Spaces imports
16
  import spaces
17
  from spaces.zero.client import _get_token
18
 
19
- # Create FastAPI app
20
  app = FastAPI()
21
 
22
- # Create and configure static directory
23
  static_dir = Path("./static")
24
  static_dir.mkdir(parents=True, exist_ok=True)
25
-
26
- # Mount static directory to FastAPI
27
  app.mount("/static", StaticFiles(directory="static"), name="static")
28
- #app.mount("/_app/immutable/assets", StaticFiles(directory="assets"), name="immutable_assets")
29
-
30
- # Tell Gradio which paths are allowed to be served
31
- os.environ["GRADIO_ALLOWED_PATHS"] = str(static_dir.resolve())
32
 
 
 
33
 
34
- @spaces.GPU(duration=4*60) # Specify GPU duration in seconds, added for testing
35
  def process_text(text):
36
- """Example GPU function - in reality, this might be model inference"""
37
  time.sleep(10)
38
-
39
  return text.upper()
40
- # I've tried also wrapping the immediate function called by gradio, and the zerogpu = True trick
41
 
42
  def process_and_save(request: gr.Request, text):
43
- """Main processing function that handles tokens and calls GPU function"""
44
- # Get and decode the authentication token
45
  token = _get_token(request)
46
  payload = token.split('.')[1]
47
  payload = f"{payload}{'=' * ((4 - len(payload) % 4) % 4)}"
48
  payload = json.loads(base64.urlsafe_b64decode(payload).decode())
49
- print(f"Token payload: {payload}") # For debugging
50
 
51
- # Process the text using GPU function
52
  processed_text = process_text(text)
53
-
54
- # Save to file
55
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
56
  file_path = static_dir / f"output_{timestamp}.txt"
57
  with open(file_path, "w") as f:
58
  f.write(processed_text)
59
-
60
- # Convert file_path to a string before returning
61
  return gr.File(value=str(file_path))
62
 
63
- # Mark main function as not requiring GPU
64
  process_and_save.zerogpu = True
65
 
66
- # Create Gradio interface
67
  with gr.Blocks() as demo:
68
  text_input = gr.Textbox(label="Enter some text")
69
  submit_btn = gr.Button("Process and Download")
70
  output = gr.File(label="Download Processed File")
71
-
72
- submit_btn.click(
73
- fn=process_and_save,
74
- inputs=[text_input],
75
- outputs=output
76
- )
77
-
78
-
79
- #os.environ["GRADIO_ROOT_PATH"] = "https://m7n-zero-gpu-uvicorn-ssr-example.hf.space/_app/immutable/"
80
 
81
- os.environ["GRADIO_ROOT_PATH"] = "/_app/immutable"
82
 
83
- # Mount Gradio app to FastAPI with SSR mode for Spaces
84
  app = gr.mount_gradio_app(app, demo, path="/", ssr_mode=True)
85
- #app.mount("/_app/immutable/assets", StaticFiles(directory="assets"), name="immutable_assets")
86
-
87
-
88
- from pathlib import Path
89
 
 
90
  assets_dir = Path("./assets")
91
- if not assets_dir.exists():
92
- assets_dir.mkdir(parents=True, exist_ok=True) # Create the folder if it doesn't exist
93
 
94
- app.mount("/_app/immutable/assets", StaticFiles(directory=str(assets_dir)), name="immutable_assets")
95
-
96
-
97
- # Run server
98
  if __name__ == "__main__":
99
- # Set SSR mode for Spaces
100
-
101
-
102
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
  import os
2
  from pathlib import Path
3
+ import json, base64, time
 
4
  from datetime import datetime
 
5
 
 
 
6
  import gradio as gr
7
+ from fastapi import FastAPI
8
  from fastapi.staticfiles import StaticFiles
9
  import uvicorn
10
 
 
11
  import spaces
12
  from spaces.zero.client import _get_token
13
 
 
14
  app = FastAPI()
15
 
16
+ # Create and mount a static directory if needed
17
  static_dir = Path("./static")
18
  static_dir.mkdir(parents=True, exist_ok=True)
 
 
19
  app.mount("/static", StaticFiles(directory="static"), name="static")
 
 
 
 
20
 
21
+ # Remove or override the default GRADIO_ROOT_PATH
22
+ os.environ["GRADIO_ROOT_PATH"] = "/assets"
23
 
24
+ @spaces.GPU(duration=4*60)
25
  def process_text(text):
 
26
  time.sleep(10)
 
27
  return text.upper()
 
28
 
29
  def process_and_save(request: gr.Request, text):
 
 
30
  token = _get_token(request)
31
  payload = token.split('.')[1]
32
  payload = f"{payload}{'=' * ((4 - len(payload) % 4) % 4)}"
33
  payload = json.loads(base64.urlsafe_b64decode(payload).decode())
34
+ print(f"Token payload: {payload}")
35
 
 
36
  processed_text = process_text(text)
 
 
37
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
38
  file_path = static_dir / f"output_{timestamp}.txt"
39
  with open(file_path, "w") as f:
40
  f.write(processed_text)
 
 
41
  return gr.File(value=str(file_path))
42
 
 
43
  process_and_save.zerogpu = True
44
 
 
45
  with gr.Blocks() as demo:
46
  text_input = gr.Textbox(label="Enter some text")
47
  submit_btn = gr.Button("Process and Download")
48
  output = gr.File(label="Download Processed File")
 
 
 
 
 
 
 
 
 
49
 
50
+ submit_btn.click(fn=process_and_save, inputs=[text_input], outputs=output)
51
 
52
+ # Mount Gradio app with SSR
53
  app = gr.mount_gradio_app(app, demo, path="/", ssr_mode=True)
 
 
 
 
54
 
55
+ # Make sure assets are served from /assets
56
  assets_dir = Path("./assets")
57
+ assets_dir.mkdir(parents=True, exist_ok=True)
58
+ app.mount("/assets", StaticFiles(directory=str(assets_dir)), name="assets")
59
 
 
 
 
 
60
  if __name__ == "__main__":
 
 
 
61
  uvicorn.run(app, host="0.0.0.0", port=7860)