Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,179 +1,139 @@
|
|
| 1 |
from transformers import pipeline, AutoTokenizer
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
-
import os
|
| 5 |
import time
|
| 6 |
-
|
| 7 |
|
| 8 |
# ===== BULLETPROOF INITIALIZATION =====
|
| 9 |
-
def
|
| 10 |
-
"""
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
torch.backends.cudnn.benchmark = True
|
| 14 |
-
return 0, torch.float16 # GPU with half precision
|
| 15 |
-
return -1, torch.float32 # CPU fallback
|
| 16 |
-
except:
|
| 17 |
-
return -1, torch.float32 # Ultimate fallback
|
| 18 |
-
|
| 19 |
-
device, dtype = get_device_config()
|
| 20 |
-
device_name = "GPU" if device == 0 else "CPU"
|
| 21 |
-
|
| 22 |
-
# ===== MODEL LOADING WITH MULTI-LAYER ERROR PROTECTION =====
|
| 23 |
-
@lru_cache(maxsize=1) # Cache loaded model
|
| 24 |
-
def load_model():
|
| 25 |
-
"""Triple-protected model loading"""
|
| 26 |
-
model_name = "mistralai/Mistral-7B-v0.1" # Default open model
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
login(token=os.environ.get("HF_TOKEN"))
|
| 33 |
-
model_name = "google/gemma-2b-it"
|
| 34 |
-
except:
|
| 35 |
-
pass # Silently fallback to open model
|
| 36 |
|
| 37 |
-
|
| 38 |
-
# First try optimized loading
|
| 39 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 40 |
-
model = pipeline(
|
| 41 |
-
"text-generation",
|
| 42 |
-
model=model_name,
|
| 43 |
-
tokenizer=tokenizer,
|
| 44 |
-
device=device,
|
| 45 |
-
torch_dtype=dtype,
|
| 46 |
-
model_kwargs={
|
| 47 |
-
"low_cpu_mem_usage": True,
|
| 48 |
-
"trust_remote_code": False # Safer option
|
| 49 |
-
}
|
| 50 |
-
)
|
| 51 |
-
|
| 52 |
-
# Warm-up with timeout protection
|
| 53 |
try:
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
model
|
| 65 |
-
device=
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
# =====
|
| 72 |
-
def
|
| 73 |
-
"""
|
| 74 |
-
|
|
|
|
| 75 |
|
| 76 |
try:
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
| 79 |
|
| 80 |
Question: {prompt}
|
| 81 |
|
| 82 |
-
Answer in
|
| 83 |
-
-"""
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
pad_token_id=model.tokenizer.eos_token_id if hasattr(model, 'tokenizer') else 50256
|
| 94 |
-
)
|
| 95 |
-
response = output[0]['generated_text'].split("Answer in bullet points:")[-1].strip()
|
| 96 |
-
except:
|
| 97 |
-
# Simplified fallback
|
| 98 |
-
output = model(prompt, max_new_tokens=80)
|
| 99 |
-
response = output[0]['generated_text']
|
| 100 |
|
| 101 |
-
#
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
pass
|
| 107 |
-
|
| 108 |
gen_time = time.time() - start_time
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
{response}
|
| 112 |
-
|
| 113 |
-
⏱️ Generated in {gen_time:.2f}s | Using {device_name}"""
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
except Exception as e:
|
| 116 |
-
return f"
|
| 117 |
-
Please try rephrasing your question. Technical details: {str(e)}"""
|
| 118 |
|
| 119 |
-
# =====
|
| 120 |
-
with gr.Blocks(theme=gr.themes.Soft(), title="
|
| 121 |
# Header
|
| 122 |
-
gr.Markdown("""<h1><center>
|
| 123 |
|
| 124 |
# Input Section
|
| 125 |
with gr.Row():
|
| 126 |
-
|
| 127 |
-
label="
|
| 128 |
-
placeholder="Explain quantum
|
| 129 |
-
lines=3
|
| 130 |
-
max_lines=5
|
| 131 |
)
|
| 132 |
|
| 133 |
# Control Panel
|
| 134 |
with gr.Row():
|
| 135 |
-
submit_btn = gr.Button("Generate", variant="primary")
|
| 136 |
clear_btn = gr.Button("Clear")
|
| 137 |
|
| 138 |
# Output Section
|
| 139 |
with gr.Row():
|
| 140 |
output_box = gr.Textbox(
|
| 141 |
-
label="
|
| 142 |
-
lines=
|
| 143 |
interactive=False
|
| 144 |
)
|
| 145 |
|
| 146 |
# Examples
|
| 147 |
gr.Examples(
|
| 148 |
examples=[
|
| 149 |
-
"Explain how
|
| 150 |
-
"Solve
|
| 151 |
-
"Describe
|
| 152 |
],
|
| 153 |
-
inputs=
|
| 154 |
)
|
| 155 |
|
| 156 |
-
# Event Handlers
|
| 157 |
-
def safe_generate(prompt):
|
| 158 |
-
try:
|
| 159 |
-
return generate_with_explanation(prompt)
|
| 160 |
-
except:
|
| 161 |
-
return "🛡️ System Protected: Please try again or rephrase your question"
|
| 162 |
-
|
| 163 |
submit_btn.click(
|
| 164 |
-
fn=
|
| 165 |
-
inputs=
|
| 166 |
-
outputs=output_box
|
| 167 |
-
api_name="generate"
|
| 168 |
)
|
| 169 |
-
|
| 170 |
clear_btn.click(
|
| 171 |
fn=lambda: ("", ""),
|
| 172 |
inputs=None,
|
| 173 |
-
outputs=[
|
| 174 |
)
|
| 175 |
|
| 176 |
-
# =====
|
| 177 |
if __name__ == "__main__":
|
| 178 |
try:
|
| 179 |
demo.launch(
|
|
@@ -182,6 +142,6 @@ if __name__ == "__main__":
|
|
| 182 |
show_error=True
|
| 183 |
)
|
| 184 |
except Exception as e:
|
| 185 |
-
print(f"🚨
|
| 186 |
print("Attempting to restart...")
|
| 187 |
-
demo.launch(
|
|
|
|
| 1 |
from transformers import pipeline, AutoTokenizer
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
|
|
|
| 4 |
import time
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
# ===== BULLETPROOF INITIALIZATION =====
|
| 8 |
+
def initialize_model():
|
| 9 |
+
"""Safe model loading with multiple fallbacks"""
|
| 10 |
+
device = 0 if torch.cuda.is_available() else -1
|
| 11 |
+
dtype = torch.float16 if device == 0 else torch.float32
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
models_to_try = [
|
| 14 |
+
"mistralai/Mistral-7B-v0.1", # Open-access
|
| 15 |
+
"google/gemma-2b-it" # Gated (will fail without auth)
|
| 16 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
for model_name in models_to_try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
try:
|
| 20 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 21 |
+
model = pipeline(
|
| 22 |
+
"text-generation",
|
| 23 |
+
model=model_name,
|
| 24 |
+
tokenizer=tokenizer,
|
| 25 |
+
device=device,
|
| 26 |
+
torch_dtype=dtype,
|
| 27 |
+
model_kwargs={"low_cpu_mem_usage": True}
|
| 28 |
+
)
|
| 29 |
+
# Warm-up
|
| 30 |
+
model("Warmup", max_new_tokens=1)
|
| 31 |
+
print(f"✅ Loaded {model_name} on {'GPU' if device == 0 else 'CPU'}")
|
| 32 |
+
return model, tokenizer
|
| 33 |
+
except Exception as e:
|
| 34 |
+
print(f"⚠️ Failed {model_name}: {str(e)}")
|
| 35 |
+
continue
|
| 36 |
+
|
| 37 |
+
raise RuntimeError("Could not load any model")
|
| 38 |
|
| 39 |
+
# ===== SAFE EXECUTION =====
|
| 40 |
+
try:
|
| 41 |
+
model, tokenizer = initialize_model()
|
| 42 |
+
except Exception as e:
|
| 43 |
+
model = None
|
| 44 |
+
print(f"🔴 Critical failure: {str(e)}")
|
| 45 |
|
| 46 |
+
# ===== ENHANCED GENERATION =====
|
| 47 |
+
def generate_response(prompt):
|
| 48 |
+
"""Full error-proof generation with metrics"""
|
| 49 |
+
if model is None:
|
| 50 |
+
return "System offline - please check server logs"
|
| 51 |
|
| 52 |
try:
|
| 53 |
+
start_time = time.time()
|
| 54 |
+
|
| 55 |
+
# Enhanced prompt for step-by-step answers
|
| 56 |
+
full_prompt = f"""Provide a detailed, step-by-step explanation:
|
| 57 |
|
| 58 |
Question: {prompt}
|
| 59 |
|
| 60 |
+
Answer in clear steps:"""
|
|
|
|
| 61 |
|
| 62 |
+
output = model(
|
| 63 |
+
full_prompt,
|
| 64 |
+
max_new_tokens=150,
|
| 65 |
+
temperature=0.4,
|
| 66 |
+
top_k=40,
|
| 67 |
+
do_sample=True,
|
| 68 |
+
pad_token_id=tokenizer.eos_token_id
|
| 69 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
# Extract and format response
|
| 72 |
+
response = output[0]['generated_text'].split("Answer in clear steps:")[-1]
|
| 73 |
+
response = response.strip().replace("\n\n", "\n• ")
|
| 74 |
+
|
| 75 |
+
# Performance metrics
|
|
|
|
|
|
|
| 76 |
gen_time = time.time() - start_time
|
| 77 |
+
speed = len(response.split()) / gen_time
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
+
return f"""📝 Step-by-Step Explanation:
|
| 80 |
+
|
| 81 |
+
• {response}
|
| 82 |
+
|
| 83 |
+
⏱️ Generated in {gen_time:.2f}s ({speed:.1f} words/sec)"""
|
| 84 |
+
|
| 85 |
except Exception as e:
|
| 86 |
+
return f"⚠️ Generation error: {str(e)}"
|
|
|
|
| 87 |
|
| 88 |
+
# ===== COMPLETE UI =====
|
| 89 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="🧠 AI Expert Assistant") as demo:
|
| 90 |
# Header
|
| 91 |
+
gr.Markdown("""<h1><center>Step-by-Step Problem Solver</center></h1>""")
|
| 92 |
|
| 93 |
# Input Section
|
| 94 |
with gr.Row():
|
| 95 |
+
user_input = gr.Textbox(
|
| 96 |
+
label="Your Question",
|
| 97 |
+
placeholder="Explain quantum computing basics step by step...",
|
| 98 |
+
lines=3
|
|
|
|
| 99 |
)
|
| 100 |
|
| 101 |
# Control Panel
|
| 102 |
with gr.Row():
|
| 103 |
+
submit_btn = gr.Button("Generate Explanation", variant="primary")
|
| 104 |
clear_btn = gr.Button("Clear")
|
| 105 |
|
| 106 |
# Output Section
|
| 107 |
with gr.Row():
|
| 108 |
output_box = gr.Textbox(
|
| 109 |
+
label="Detailed Explanation",
|
| 110 |
+
lines=10,
|
| 111 |
interactive=False
|
| 112 |
)
|
| 113 |
|
| 114 |
# Examples
|
| 115 |
gr.Examples(
|
| 116 |
examples=[
|
| 117 |
+
"Explain how photosynthesis works in plants",
|
| 118 |
+
"Solve 3x + 7 = 22 showing all steps",
|
| 119 |
+
"Describe the water cycle with bullet points"
|
| 120 |
],
|
| 121 |
+
inputs=user_input
|
| 122 |
)
|
| 123 |
|
| 124 |
+
# Event Handlers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
submit_btn.click(
|
| 126 |
+
fn=generate_response,
|
| 127 |
+
inputs=user_input,
|
| 128 |
+
outputs=output_box
|
|
|
|
| 129 |
)
|
|
|
|
| 130 |
clear_btn.click(
|
| 131 |
fn=lambda: ("", ""),
|
| 132 |
inputs=None,
|
| 133 |
+
outputs=[user_input, output_box]
|
| 134 |
)
|
| 135 |
|
| 136 |
+
# ===== FAILSAFE LAUNCH =====
|
| 137 |
if __name__ == "__main__":
|
| 138 |
try:
|
| 139 |
demo.launch(
|
|
|
|
| 142 |
show_error=True
|
| 143 |
)
|
| 144 |
except Exception as e:
|
| 145 |
+
print(f"🚨 Server crashed: {str(e)}")
|
| 146 |
print("Attempting to restart...")
|
| 147 |
+
demo.launch(share=True)
|