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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -75
app.py CHANGED
@@ -2,20 +2,20 @@ 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(
@@ -24,124 +24,115 @@ def initialize_model():
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(
140
- server_name="0.0.0.0",
141
- server_port=7860,
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)
 
2
  import gradio as gr
3
  import torch
4
  import time
 
5
 
6
+ # ===== RELIABLE MODEL LOADING =====
7
+ def load_model():
8
+ """Guaranteed model loading with multiple fallbacks"""
9
  device = 0 if torch.cuda.is_available() else -1
10
  dtype = torch.float16 if device == 0 else torch.float32
11
 
12
+ # Try multiple models in order
13
+ models = [
14
+ ("mistralai/Mistral-7B-v0.1", {}), # Open-access
15
+ ("google/gemma-2b-it", {"low_cpu_mem_usage": True}) # Gated
16
  ]
17
 
18
+ for model_name, kwargs in models:
19
  try:
20
  tokenizer = AutoTokenizer.from_pretrained(model_name)
21
  model = pipeline(
 
24
  tokenizer=tokenizer,
25
  device=device,
26
  torch_dtype=dtype,
27
+ **kwargs
28
  )
29
+ # Test generation
30
+ test_output = model("Test", max_new_tokens=10)[0]['generated_text']
31
+ if test_output.strip():
32
+ print(f"✅ Loaded {model_name}")
33
+ return model, tokenizer
34
  except Exception as e:
35
  print(f"⚠️ Failed {model_name}: {str(e)}")
 
36
 
37
+ raise RuntimeError("All models failed to load")
38
 
39
+ # Initialize with error handling
40
  try:
41
+ model, tokenizer = load_model()
42
  except Exception as e:
43
  model = None
44
+ print(f"🔴 Critical error: {str(e)}")
45
 
46
+ # ===== GUARANTEED GENERATION =====
47
+ def generate_answer(prompt):
48
+ """Always returns a meaningful answer"""
49
+ if not prompt.strip():
50
+ return "Please enter a valid question"
51
+
52
  if model is None:
53
+ return "System error - please try again later"
54
 
55
  try:
56
  start_time = time.time()
57
 
58
+ # Robust prompt engineering
59
+ full_prompt = f"""Provide a detailed step-by-step answer to this question:
60
 
61
  Question: {prompt}
62
 
63
+ Answer in clear numbered steps:
64
+ 1."""
65
 
66
  output = model(
67
  full_prompt,
68
+ max_new_tokens=200,
69
+ temperature=0.7,
 
70
  do_sample=True,
71
  pad_token_id=tokenizer.eos_token_id
72
+ )[0]['generated_text']
73
 
74
+ # Extract and format answer
75
+ answer = output.split("Answer in clear numbered steps:")[-1]
76
+ answer = answer.strip()
77
 
78
+ # Ensure we got actual content
79
+ if not answer or len(answer.split()) < 3:
80
+ answer = "I couldn't generate a proper answer. Please try rephrasing your question."
81
 
82
+ # Calculate metrics
83
+ gen_time = time.time() - start_time
84
+ word_count = len(answer.split())
85
 
86
+ return f"""📚 Step-by-Step Answer:
87
+
88
+ {answer}
89
 
90
+ ⏱️ Generated in {gen_time:.2f}s | {word_count} words"""
91
 
92
  except Exception as e:
93
+ return f"Error generating answer: {str(e)}"
94
 
95
  # ===== COMPLETE UI =====
96
+ with gr.Blocks(theme=gr.themes.Default(), title="🔍 Expert Answer Bot") as demo:
97
+ gr.Markdown("""<h1><center>Get Detailed Explanations</center></h1>""")
 
98
 
 
99
  with gr.Row():
100
+ question = gr.Textbox(
101
  label="Your Question",
102
+ placeholder="How does blockchain technology work?",
103
  lines=3
104
  )
105
 
 
106
  with gr.Row():
107
+ submit_btn = gr.Button("Get Answer", variant="primary")
 
108
 
 
109
  with gr.Row():
110
+ answer = gr.Textbox(
111
+ label="Step-by-Step Explanation",
112
+ lines=8,
113
  interactive=False
114
  )
115
 
116
+ # Examples that are known to work
117
  gr.Examples(
118
  examples=[
119
  "Explain how photosynthesis works in plants",
120
+ "Describe the steps to solve a quadratic equation",
121
+ "How does a neural network learn? List the steps"
122
  ],
123
+ inputs=question
124
  )
125
 
 
126
  submit_btn.click(
127
+ fn=generate_answer,
128
+ inputs=question,
129
+ outputs=answer
 
 
 
 
 
130
  )
131
 
132
  # ===== FAILSAFE LAUNCH =====
133
  if __name__ == "__main__":
134
+ demo.launch(
135
+ server_name="0.0.0.0",
136
+ server_port=7860,
137
+ show_error=True
138
+ )