Spaces:
Running
Running
File size: 5,589 Bytes
75342d8 0ca3156 75342d8 0ca3156 75342d8 5f7ceab 75342d8 0ca3156 75342d8 0ca3156 75342d8 0ca3156 75342d8 0ca3156 75342d8 0ca3156 75342d8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# app.py
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
import gradio as gr
# Initialize variables
model = None
tokenizer = None
device = None
# Define function to load model
def load_model():
global model, tokenizer, device
# Use GPU if available
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using device: {device}")
# Load the Phi-2 model
model_id = "microsoft/phi-1_5"
print("Loading Phi-2 model and tokenizer...")
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
model_id,
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
device_map="auto" # Better device management for Spaces
)
print("Model loaded successfully!")
# Define inference function
def generate_text(prompt, task_type, max_length=300):
global model, tokenizer, device
# If model hasn't been loaded yet, load it
if model is None:
load_model()
# Set temperature based on task type
temperature_map = {
"Math Problem": 0.2,
"Science Theory": 0.4,
"Coding Question": 0.3,
"Reasoning": 0.5,
"Creative Writing": 0.8
}
temperature = temperature_map.get(task_type, 0.5)
# Enhance the prompt to request step-by-step solutions
enhanced_prompt = f"{prompt}\n\nPlease provide a detailed step-by-step solution with clear reasoning."
# Progress update for UI
yield "Generating solution..."
# Tokenize input
inputs = tokenizer(enhanced_prompt, return_tensors="pt").to(device)
# Generate output
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=max_length,
temperature=temperature,
do_sample=True
)
# Decode response
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
# If the response doesn't seem to include steps, add formatting for clarity
if "step" not in response.lower() and len(response) > 100:
# Split into paragraphs and format as steps
paragraphs = [p for p in response.split('\n') if p.strip()]
formatted_response = ""
for i, para in enumerate(paragraphs):
if i == 0 and para == enhanced_prompt:
continue
formatted_response += f"Step {i+1}: {para}\n\n"
yield formatted_response
else:
yield response
# Create Gradio interface
with gr.Blocks(title="Phi-2 Step-by-Step Solution Generator", theme=gr.themes.Soft()) as demo:
gr.Markdown("# 🧠 Phi-2 Step-by-Step Solution Generator")
gr.Markdown("""
Enter a prompt below and get detailed step-by-step solutions using Microsoft's Phi-2 model.
Select the appropriate task type to optimize the model's response.
""")
with gr.Row():
with gr.Column(scale=2):
prompt_input = gr.Textbox(
label="Prompt",
placeholder="Enter your question or problem here...",
lines=5
)
with gr.Row():
task_type = gr.Radio(
["Math Problem", "Science Theory", "Coding Question", "Reasoning", "Creative Writing"],
label="Task Type (sets optimal temperature)",
value="Reasoning"
)
max_length_slider = gr.Slider(
minimum=100,
maximum=1000,
value=300,
step=50,
label="Maximum Output Length"
)
with gr.Row():
generate_button = gr.Button(
"✨ Generate Step-by-Step Solution",
variant="primary",
size="lg"
)
clear_button = gr.Button("Clear", variant="secondary")
with gr.Column(scale=3):
output_text = gr.Textbox(
label="Step-by-Step Solution",
lines=15,
show_copy_button=True
)
# Examples with different task types
with gr.Accordion("Example Prompts", open=False):
gr.Examples(
examples=[
["Solve the quadratic equation: 2x² + 5x - 3 = 0", "Math Problem"],
["Explain how photosynthesis works in plants", "Science Theory"],
["Write a function in Python to find the Fibonacci sequence up to n terms", "Coding Question"],
["Why might increasing minimum wage have both positive and negative economic impacts?", "Reasoning"],
["Write a short story about a robot discovering emotions", "Creative Writing"]
],
inputs=[prompt_input, task_type]
)
# Add functionality to buttons
generate_button.click(
fn=generate_text,
inputs=[prompt_input, task_type, max_length_slider],
outputs=output_text
)
# Clear functionality
clear_button.click(
fn=lambda: ("", "Reasoning"),
inputs=[],
outputs=[prompt_input, task_type]
)
# Adding a note about load times
gr.Markdown("""
> **Note**: The model loads when you submit your first prompt, which may take 1-2 minutes.
> Subsequent generations will be much faster.
""")
# Launch the app
if __name__ == "__main__":
demo.queue().launch() |