Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,50 +4,48 @@ from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
| 4 |
import torch
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
-
CORS(app)
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 14 |
model = AutoModelForCausalLM.from_pretrained(
|
| 15 |
-
|
| 16 |
torch_dtype=torch.float16,
|
| 17 |
device_map="auto"
|
| 18 |
)
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
You can help with logic, reasoning, and programming tasks in a kind, conversational tone."""
|
| 24 |
|
| 25 |
@app.route("/api/ask", methods=["POST"])
|
| 26 |
def ask():
|
| 27 |
data = request.get_json()
|
| 28 |
-
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
#
|
| 34 |
inputs = tokenizer(full_prompt, return_tensors="pt").to(model.device)
|
| 35 |
|
| 36 |
-
# Generate response
|
| 37 |
outputs = model.generate(
|
| 38 |
**inputs,
|
| 39 |
-
max_new_tokens=
|
| 40 |
temperature=0.7,
|
| 41 |
top_p=0.9,
|
| 42 |
-
do_sample=True
|
| 43 |
)
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
if "<|assistant|>" in response:
|
| 48 |
-
response = response.split("<|assistant|>")[-1].strip()
|
| 49 |
|
| 50 |
-
return jsonify({"reply":
|
| 51 |
|
| 52 |
|
| 53 |
if __name__ == "__main__":
|
|
|
|
| 4 |
import torch
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
+
CORS(app)
|
| 8 |
|
| 9 |
+
# 🔹 Load model
|
| 10 |
+
MODEL_NAME = "microsoft/Phi-3-mini-4k-instruct"
|
| 11 |
+
print("🚀 Loading Phi-3-mini model (this may take a minute)...")
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
|
|
|
| 13 |
model = AutoModelForCausalLM.from_pretrained(
|
| 14 |
+
MODEL_NAME,
|
| 15 |
torch_dtype=torch.float16,
|
| 16 |
device_map="auto"
|
| 17 |
)
|
| 18 |
+
print("✅ Model ready!")
|
| 19 |
|
| 20 |
+
@app.route("/")
|
| 21 |
+
def home():
|
| 22 |
+
return "✅ Phi-3-mini API is running! POST JSON to /api/ask with {'prompt': 'your question'}"
|
|
|
|
| 23 |
|
| 24 |
@app.route("/api/ask", methods=["POST"])
|
| 25 |
def ask():
|
| 26 |
data = request.get_json()
|
| 27 |
+
prompt = data.get("prompt", "")
|
| 28 |
|
| 29 |
+
system_prompt = (
|
| 30 |
+
"You are Acla, a friendly and helpful assistant powered by Phi-3 mini who gives clear, step-by-step answers. "
|
| 31 |
+
"Be concise but thoughtful. Use reasoning and math when needed."
|
| 32 |
+
)
|
| 33 |
|
| 34 |
+
full_prompt = f"### System:\n{system_prompt}\n\n### User:\n{prompt}\n\n### Assistant:"
|
| 35 |
inputs = tokenizer(full_prompt, return_tensors="pt").to(model.device)
|
| 36 |
|
|
|
|
| 37 |
outputs = model.generate(
|
| 38 |
**inputs,
|
| 39 |
+
max_new_tokens=250,
|
| 40 |
temperature=0.7,
|
| 41 |
top_p=0.9,
|
| 42 |
+
do_sample=True
|
| 43 |
)
|
| 44 |
|
| 45 |
+
text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 46 |
+
reply = text.split("### Assistant:")[-1].strip()
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
return jsonify({"reply": reply})
|
| 49 |
|
| 50 |
|
| 51 |
if __name__ == "__main__":
|