rui3000 commited on
Commit
277f293
Β·
verified Β·
1 Parent(s): dae2baa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -93
app.py CHANGED
@@ -1,121 +1,71 @@
1
  import gradio as gr
2
  import spaces
3
- import torch
4
- from transformers import AutoModelForCausalLM, AutoTokenizer
5
 
6
- print("[App] Starting application...")
7
- print("[App] Spaces imported successfully")
8
-
9
- # Global variables
10
- _model = None
11
- _tokenizer = None
12
- _model_name = "microsoft/DialoGPT-small"
13
-
14
- def initialize_tokenizer():
15
- """Initialize tokenizer"""
16
- global _tokenizer
17
- if _tokenizer is None:
18
- print("[App] Loading tokenizer...")
19
- _tokenizer = AutoTokenizer.from_pretrained(_model_name)
20
- if _tokenizer.pad_token is None:
21
- _tokenizer.pad_token = _tokenizer.eos_token
22
- print("[App] Tokenizer loaded successfully.")
23
- return _tokenizer
24
 
 
25
  @spaces.GPU
26
- def generate_text_gpu(prompt: str, max_tokens: int = 50):
27
- """GPU function for text generation - this should be detected by Spaces"""
28
- global _model, _tokenizer
29
-
30
- print("[App] GPU function called")
31
-
32
- # Initialize tokenizer
33
- if _tokenizer is None:
34
- initialize_tokenizer()
35
-
36
- # Load model in GPU context
37
- if _model is None:
38
- print("[App] Loading model in GPU context...")
39
- _model = AutoModelForCausalLM.from_pretrained(
40
- _model_name,
41
- torch_dtype=torch.float16,
42
- device_map="auto"
43
- )
44
- print("[App] Model loaded successfully.")
45
-
46
- # Simple generation
47
- inputs = _tokenizer.encode(prompt, return_tensors="pt")
48
- device = next(_model.parameters()).device
49
- inputs = inputs.to(device)
50
-
51
- with torch.no_grad():
52
- outputs = _model.generate(
53
- inputs,
54
- max_new_tokens=max_tokens,
55
- temperature=0.7,
56
- do_sample=True,
57
- pad_token_id=_tokenizer.eos_token_id
58
- )
59
-
60
- response = _tokenizer.decode(outputs[0], skip_special_tokens=True)
61
- print("[App] Generation completed")
62
- return response
63
 
64
- # Print confirmation that GPU function is available
65
- print(f"[App] GPU function registered: {generate_text_gpu.__name__}")
 
 
 
 
 
66
 
67
  def generate_response(user_input):
68
- """Generate response using GPU function"""
69
  if not user_input.strip():
70
  return "Please enter some text!"
71
 
72
  try:
73
- print(f"[App] Processing input: {user_input}")
74
- response = generate_text_gpu(user_input)
75
  return f"Generated: {response}"
76
  except Exception as e:
77
- print(f"[App] Error: {e}")
78
  return f"Error: {str(e)}"
79
 
80
- # Initialize tokenizer at startup (lightweight operation)
81
- try:
82
- initialize_tokenizer()
83
- print("[App] Initial setup completed")
84
- except Exception as e:
85
- print(f"[App] Setup error: {e}")
86
-
87
  # Create Gradio interface
88
- with gr.Blocks(title="GPU Test", theme=gr.themes.Soft()) as demo:
89
- gr.Markdown("# πŸš€ GPU Function Test")
90
- gr.Markdown("Testing if Spaces can detect the GPU function properly.")
91
 
92
  with gr.Row():
93
- with gr.Column():
94
- input_text = gr.Textbox(
95
- label="Enter text",
96
- placeholder="Type something...",
97
- value="Hello, how are you today?"
98
- )
99
- generate_btn = gr.Button("Generate with GPU", variant="primary")
100
-
101
- with gr.Column():
102
- output_text = gr.Textbox(
103
- label="Generated response",
104
- interactive=False,
105
- lines=5
106
- )
107
 
108
  generate_btn.click(
109
  fn=generate_response,
110
  inputs=[input_text],
111
  outputs=[output_text]
112
  )
113
-
114
- gr.Markdown("---")
115
- gr.Markdown("**Note:** If this works, the GPU function is being detected properly.")
116
 
117
- print("[App] Gradio interface created")
 
 
 
 
 
 
 
 
 
 
118
 
119
  if __name__ == "__main__":
120
- print("[App] Launching demo...")
121
- demo.launch()
 
 
1
  import gradio as gr
2
  import spaces
 
 
3
 
4
+ # Import the service - this should trigger GPU function registration
5
+ from minimal_service import service, generate_text_gpu
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
+ # Additional GPU function at app level for extra safety
8
  @spaces.GPU
9
+ def app_gpu_test():
10
+ """Test GPU function at app level"""
11
+ return "App GPU function works"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
+ print("[App] GPU functions imported successfully")
14
+ print(f"[App] Service GPU function: {generate_text_gpu.__name__}")
15
+ print(f"[App] App GPU function: {app_gpu_test.__name__}")
16
+
17
+ # ADD FASTAPI - Step 2 change
18
+ from fastapi import FastAPI
19
+ from fastapi.responses import RedirectResponse
20
 
21
  def generate_response(user_input):
22
+ """Generate response using the service"""
23
  if not user_input.strip():
24
  return "Please enter some text!"
25
 
26
  try:
27
+ response = service.generate(user_input)
 
28
  return f"Generated: {response}"
29
  except Exception as e:
 
30
  return f"Error: {str(e)}"
31
 
 
 
 
 
 
 
 
32
  # Create Gradio interface
33
+ with gr.Blocks(title="Minimal GPU Test with FastAPI") as demo:
34
+ gr.Markdown("# Minimal GPU Test with FastAPI")
35
+ gr.Markdown("Testing if adding FastAPI breaks GPU detection.")
36
 
37
  with gr.Row():
38
+ input_text = gr.Textbox(
39
+ label="Enter text",
40
+ placeholder="Type something...",
41
+ value="Hello, how are you?"
42
+ )
43
+ output_text = gr.Textbox(
44
+ label="Generated response",
45
+ interactive=False
46
+ )
47
+
48
+ generate_btn = gr.Button("Generate", variant="primary")
 
 
 
49
 
50
  generate_btn.click(
51
  fn=generate_response,
52
  inputs=[input_text],
53
  outputs=[output_text]
54
  )
 
 
 
55
 
56
+ # ADD FASTAPI MOUNTING - Step 2 change
57
+ app = FastAPI()
58
+
59
+ @app.get("/")
60
+ async def root():
61
+ return RedirectResponse(url="/gradio")
62
+
63
+ # Mount Gradio on FastAPI
64
+ app = gr.mount_gradio_app(app, demo, path="/gradio")
65
+
66
+ print("[App] FastAPI + Gradio setup completed")
67
 
68
  if __name__ == "__main__":
69
+ print("[App] Starting application...")
70
+ import uvicorn
71
+ uvicorn.run(app, host="0.0.0.0", port=7860)