Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
# =====
|
| 8 |
-
def
|
| 9 |
-
"""
|
| 10 |
device = 0 if torch.cuda.is_available() else -1
|
| 11 |
dtype = torch.float16 if device == 0 else torch.float32
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
"
|
|
|
|
| 16 |
]
|
| 17 |
|
| 18 |
-
for model_name in
|
| 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 |
-
|
| 28 |
)
|
| 29 |
-
#
|
| 30 |
-
model("
|
| 31 |
-
|
| 32 |
-
|
|
|
|
| 33 |
except Exception as e:
|
| 34 |
print(f"⚠️ Failed {model_name}: {str(e)}")
|
| 35 |
-
continue
|
| 36 |
|
| 37 |
-
raise RuntimeError("
|
| 38 |
|
| 39 |
-
#
|
| 40 |
try:
|
| 41 |
-
model, tokenizer =
|
| 42 |
except Exception as e:
|
| 43 |
model = None
|
| 44 |
-
print(f"🔴 Critical
|
| 45 |
|
| 46 |
-
# =====
|
| 47 |
-
def
|
| 48 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 49 |
if model is None:
|
| 50 |
-
return "System
|
| 51 |
|
| 52 |
try:
|
| 53 |
start_time = time.time()
|
| 54 |
|
| 55 |
-
#
|
| 56 |
-
full_prompt = f"""Provide a detailed
|
| 57 |
|
| 58 |
Question: {prompt}
|
| 59 |
|
| 60 |
-
Answer in clear steps:
|
|
|
|
| 61 |
|
| 62 |
output = model(
|
| 63 |
full_prompt,
|
| 64 |
-
max_new_tokens=
|
| 65 |
-
temperature=0.
|
| 66 |
-
top_k=40,
|
| 67 |
do_sample=True,
|
| 68 |
pad_token_id=tokenizer.eos_token_id
|
| 69 |
-
)
|
| 70 |
|
| 71 |
-
# Extract and format
|
| 72 |
-
|
| 73 |
-
|
| 74 |
|
| 75 |
-
#
|
| 76 |
-
|
| 77 |
-
|
| 78 |
|
| 79 |
-
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
|
|
|
|
|
|
|
| 82 |
|
| 83 |
-
⏱️ Generated in {gen_time:.2f}s
|
| 84 |
|
| 85 |
except Exception as e:
|
| 86 |
-
return f"
|
| 87 |
|
| 88 |
# ===== COMPLETE UI =====
|
| 89 |
-
with gr.Blocks(theme=gr.themes.
|
| 90 |
-
|
| 91 |
-
gr.Markdown("""<h1><center>Step-by-Step Problem Solver</center></h1>""")
|
| 92 |
|
| 93 |
-
# Input Section
|
| 94 |
with gr.Row():
|
| 95 |
-
|
| 96 |
label="Your Question",
|
| 97 |
-
placeholder="
|
| 98 |
lines=3
|
| 99 |
)
|
| 100 |
|
| 101 |
-
# Control Panel
|
| 102 |
with gr.Row():
|
| 103 |
-
submit_btn = gr.Button("
|
| 104 |
-
clear_btn = gr.Button("Clear")
|
| 105 |
|
| 106 |
-
# Output Section
|
| 107 |
with gr.Row():
|
| 108 |
-
|
| 109 |
-
label="
|
| 110 |
-
lines=
|
| 111 |
interactive=False
|
| 112 |
)
|
| 113 |
|
| 114 |
-
# Examples
|
| 115 |
gr.Examples(
|
| 116 |
examples=[
|
| 117 |
"Explain how photosynthesis works in plants",
|
| 118 |
-
"
|
| 119 |
-
"
|
| 120 |
],
|
| 121 |
-
inputs=
|
| 122 |
)
|
| 123 |
|
| 124 |
-
# Event Handlers
|
| 125 |
submit_btn.click(
|
| 126 |
-
fn=
|
| 127 |
-
inputs=
|
| 128 |
-
outputs=
|
| 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 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 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 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|