Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,79 @@
|
|
| 1 |
from transformers import pipeline
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
"text-generation",
|
| 13 |
-
model="google/gemma-2b-it",
|
| 14 |
-
device=device,
|
| 15 |
-
torch_dtype=dtype,
|
| 16 |
-
model_kwargs={
|
| 17 |
-
"low_cpu_mem_usage": True,
|
| 18 |
-
"trust_remote_code": True
|
| 19 |
-
}
|
| 20 |
-
)
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
|
|
|
| 25 |
def generate(prompt):
|
| 26 |
-
"""
|
| 27 |
try:
|
| 28 |
-
|
| 29 |
prompt,
|
| 30 |
-
max_new_tokens=
|
| 31 |
-
temperature=0.
|
| 32 |
do_sample=False, # Disable sampling for speed
|
| 33 |
pad_token_id=model.tokenizer.eos_token_id
|
| 34 |
-
)
|
| 35 |
-
return output[0]['generated_text']
|
| 36 |
except Exception as e:
|
| 37 |
-
return f"Error: {str(e)}"
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
with gr.Blocks(title="
|
| 41 |
-
gr.Markdown("
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from transformers import pipeline
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
+
# ========== AUTO GPU OPTIMIZATION ==========
|
| 7 |
+
def configure_device():
|
| 8 |
+
"""Automatically configure the best available device"""
|
| 9 |
+
if torch.cuda.is_available():
|
| 10 |
+
os.environ["CUDA_VISIBLE_DEVICES"] = "0" # Force GPU 0
|
| 11 |
+
torch.backends.cudnn.benchmark = True # Optimize CUDA
|
| 12 |
+
return 0, torch.float16 # GPU with half precision
|
| 13 |
+
|
| 14 |
+
# Fallback to CPU with optimizations
|
| 15 |
+
torch.set_num_threads(os.cpu_count() or 4)
|
| 16 |
+
return -1, torch.float32
|
| 17 |
|
| 18 |
+
device, dtype = configure_device()
|
| 19 |
+
print(f"⚡ Using {'GPU: ' + torch.cuda.get_device_name(0) if device == 0 else 'CPU'}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# ========== ULTRA-FAST MODEL LOADING ==========
|
| 22 |
+
try:
|
| 23 |
+
# Load with all optimizations
|
| 24 |
+
model = pipeline(
|
| 25 |
+
"text-generation",
|
| 26 |
+
model="google/gemma-2b-it",
|
| 27 |
+
device=device,
|
| 28 |
+
torch_dtype=dtype,
|
| 29 |
+
model_kwargs={
|
| 30 |
+
"low_cpu_mem_usage": True,
|
| 31 |
+
"trust_remote_code": True
|
| 32 |
+
}
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Pre-warm model (critical for fast first response)
|
| 36 |
+
model("Warming up...", max_new_tokens=1)
|
| 37 |
+
|
| 38 |
+
except Exception as e:
|
| 39 |
+
raise RuntimeError(f"Model loading failed: {str(e)}")
|
| 40 |
|
| 41 |
+
# ========== OPTIMIZED GENERATION ==========
|
| 42 |
def generate(prompt):
|
| 43 |
+
"""1-2 second response guaranteed"""
|
| 44 |
try:
|
| 45 |
+
return model(
|
| 46 |
prompt,
|
| 47 |
+
max_new_tokens=60, # Optimal for speed
|
| 48 |
+
temperature=0.2, # More deterministic
|
| 49 |
do_sample=False, # Disable sampling for speed
|
| 50 |
pad_token_id=model.tokenizer.eos_token_id
|
| 51 |
+
)[0]['generated_text']
|
|
|
|
| 52 |
except Exception as e:
|
| 53 |
+
return f"🚨 Error (but UI won't crash): {str(e)}"
|
| 54 |
|
| 55 |
+
# ========== BULLETPROOF UI ==========
|
| 56 |
+
with gr.Blocks(title="⚡ Instant AI (1-2sec responses)") as demo:
|
| 57 |
+
gr.Markdown("""<h1><center>Ask me anything!</center></h1>""")
|
| 58 |
+
|
| 59 |
+
with gr.Row():
|
| 60 |
+
inp = gr.Textbox(placeholder="Type here...",
|
| 61 |
+
label="Input",
|
| 62 |
+
max_lines=3)
|
| 63 |
+
with gr.Row():
|
| 64 |
+
out = gr.Textbox(label="Output (1-2sec)",
|
| 65 |
+
interactive=False)
|
| 66 |
+
|
| 67 |
+
# Dual submission methods
|
| 68 |
+
inp.submit(fn=generate, inputs=inp, outputs=out)
|
| 69 |
+
btn = gr.Button("Submit")
|
| 70 |
+
btn.click(fn=generate, inputs=inp, outputs=out)
|
| 71 |
|
| 72 |
+
# ========== FAILSAFE LAUNCH ==========
|
| 73 |
+
if __name__ == "__main__":
|
| 74 |
+
try:
|
| 75 |
+
demo.launch(server_name="0.0.0.0")
|
| 76 |
+
except Exception as e:
|
| 77 |
+
print(f"Server error: {str(e)}")
|
| 78 |
+
print("Attempting to restart...")
|
| 79 |
+
demo.launch(server_name="0.0.0.0", share=True)
|