Scaryscar commited on
Commit
15cc4ad
·
verified ·
1 Parent(s): a90b78a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -127
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
- from functools import lru_cache
7
 
8
  # ===== BULLETPROOF INITIALIZATION =====
9
- def get_device_config():
10
- """Automatically configure the best available device"""
11
- try:
12
- if torch.cuda.is_available():
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
- # Attempt authenticated model if token exists
29
- if os.environ.get("HF_TOKEN"):
30
- try:
31
- from huggingface_hub import login
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
- try:
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
- with time.timeout(5): # Prevent infinite hangs
55
- model("Warmup", max_new_tokens=1)
56
- except:
57
- pass
58
-
59
- return model
60
- except Exception as e:
61
- # Final fallback to tiny model
62
- return pipeline(
63
- "text-generation",
64
- model="gpt2",
65
- device=device,
66
- torch_dtype=dtype
67
- )
 
 
 
 
68
 
69
- model = load_model()
 
 
 
 
 
70
 
71
- # ===== ULTRA-RELIABLE GENERATION =====
72
- def generate_with_explanation(prompt):
73
- """Quadruple-protected generation"""
74
- start_time = time.time()
 
75
 
76
  try:
77
- # Enhanced prompt engineering
78
- enhanced_prompt = f"""Provide a detailed step-by-step explanation:
 
 
79
 
80
  Question: {prompt}
81
 
82
- Answer in bullet points:
83
- -"""
84
 
85
- # Generation with multiple fallbacks
86
- try:
87
- output = model(
88
- enhanced_prompt,
89
- max_new_tokens=100,
90
- temperature=0.3,
91
- top_k=40,
92
- do_sample=False,
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
- # Formatting protection
102
- try:
103
- if "- " not in response: # Ensure bullet points
104
- response = "- " + response.replace("\n", "\n- ")
105
- except:
106
- pass
107
-
108
  gen_time = time.time() - start_time
109
- return f"""📝 Explanation:
110
-
111
- {response}
112
-
113
- ⏱️ Generated in {gen_time:.2f}s | Using {device_name}"""
114
 
 
 
 
 
 
 
115
  except Exception as e:
116
- return f"""🛡System Protected: The AI encountered an error but recovered.
117
- Please try rephrasing your question. Technical details: {str(e)}"""
118
 
119
- # ===== INDUSTRIAL-STRENGTH INTERFACE =====
120
- with gr.Blocks(theme=gr.themes.Soft(), title="🔒 Bulletproof AI Tutor") as demo:
121
  # Header
122
- gr.Markdown("""<h1><center>Ultra-Reliable Step-by-Step Tutor</center></h1>""")
123
 
124
  # Input Section
125
  with gr.Row():
126
- input_box = gr.Textbox(
127
- label="Ask anything",
128
- placeholder="Explain quantum physics like I'm 5...",
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="Step-by-Step Explanation",
142
- lines=8,
143
  interactive=False
144
  )
145
 
146
  # Examples
147
  gr.Examples(
148
  examples=[
149
- "Explain how neural networks work step by step",
150
- "Solve 2x + 5 = 15 showing each step",
151
- "Describe photosynthesis in simple terms"
152
  ],
153
- inputs=input_box
154
  )
155
 
156
- # Event Handlers with Error Protection
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=safe_generate,
165
- inputs=input_box,
166
- outputs=output_box,
167
- api_name="generate"
168
  )
169
-
170
  clear_btn.click(
171
  fn=lambda: ("", ""),
172
  inputs=None,
173
- outputs=[input_box, output_box]
174
  )
175
 
176
- # ===== NUCLEAR-PROOF LAUNCH =====
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"🚨 Critical error: {str(e)}")
186
  print("Attempting to restart...")
187
- demo.launch(server_name="0.0.0.0", share=True)
 
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)