Update app.py
Browse files
app.py
CHANGED
|
@@ -3,57 +3,69 @@ from flask import Flask, send_file, request, jsonify
|
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
import torch
|
| 5 |
from functools import lru_cache
|
| 6 |
-
import
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
return model, tokenizer
|
| 20 |
|
| 21 |
-
def generate_response(prompt
|
| 22 |
"""Generate response from the model"""
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
@app.route('/')
|
| 38 |
def home():
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
@app.route('/message', methods=['POST'])
|
| 42 |
def message():
|
| 43 |
try:
|
| 44 |
data = request.json
|
| 45 |
-
|
|
|
|
| 46 |
|
|
|
|
| 47 |
if not user_message:
|
| 48 |
-
return jsonify({"response": "
|
| 49 |
|
| 50 |
-
|
| 51 |
-
response = generate_response(user_message
|
|
|
|
| 52 |
|
| 53 |
return jsonify({"response": response})
|
| 54 |
|
| 55 |
except Exception as e:
|
| 56 |
-
|
|
|
|
| 57 |
|
| 58 |
if __name__ == '__main__':
|
| 59 |
-
app.run(debug=True)
|
|
|
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
import torch
|
| 5 |
from functools import lru_cache
|
| 6 |
+
from flask_cors import CORS # إضافة دعم CORS
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
+
CORS(app) # تفعيل CORS للسماح بالاتصال
|
| 10 |
|
| 11 |
+
# تحميل النموذج - نقوم بتحميله مرة واحدة عند بدء التطبيق
|
| 12 |
+
print("جاري تحميل النموذج...")
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained("amd/AMD-OLMo-1B")
|
| 14 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 15 |
+
"amd/AMD-OLMo-1B",
|
| 16 |
+
torch_dtype=torch.float16,
|
| 17 |
+
device_map="auto"
|
| 18 |
+
)
|
| 19 |
+
print("تم تحميل النموذج بنجاح!")
|
|
|
|
| 20 |
|
| 21 |
+
def generate_response(prompt):
|
| 22 |
"""Generate response from the model"""
|
| 23 |
+
try:
|
| 24 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 25 |
+
with torch.no_grad():
|
| 26 |
+
outputs = model.generate(
|
| 27 |
+
**inputs,
|
| 28 |
+
max_length=200,
|
| 29 |
+
num_return_sequences=1,
|
| 30 |
+
temperature=0.7,
|
| 31 |
+
top_p=0.9,
|
| 32 |
+
repetition_penalty=1.2,
|
| 33 |
+
pad_token_id=tokenizer.eos_token_id
|
| 34 |
+
)
|
| 35 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 36 |
+
return response.replace(prompt, "").strip()
|
| 37 |
+
except Exception as e:
|
| 38 |
+
print(f"خطأ في توليد الرد: {str(e)}")
|
| 39 |
+
return "عذراً، حدث خطأ في معالجة رسالتك."
|
| 40 |
|
| 41 |
@app.route('/')
|
| 42 |
def home():
|
| 43 |
+
try:
|
| 44 |
+
return send_file('index.html')
|
| 45 |
+
except Exception as e:
|
| 46 |
+
print(f"خطأ في تحميل الصفحة: {str(e)}")
|
| 47 |
+
return "خطأ في تحميل الصفحة"
|
| 48 |
|
| 49 |
@app.route('/message', methods=['POST'])
|
| 50 |
def message():
|
| 51 |
try:
|
| 52 |
data = request.json
|
| 53 |
+
if not data:
|
| 54 |
+
return jsonify({"response": "لم يتم استلام أي بيانات"}), 400
|
| 55 |
|
| 56 |
+
user_message = data.get('message', '')
|
| 57 |
if not user_message:
|
| 58 |
+
return jsonify({"response": "الرسالة فارغة"}), 400
|
| 59 |
|
| 60 |
+
print(f"رسالة مستلمة: {user_message}")
|
| 61 |
+
response = generate_response(user_message)
|
| 62 |
+
print(f"الرد: {response}")
|
| 63 |
|
| 64 |
return jsonify({"response": response})
|
| 65 |
|
| 66 |
except Exception as e:
|
| 67 |
+
print(f"خطأ في معالجة الرسالة: {str(e)}")
|
| 68 |
+
return jsonify({"response": "عذراً، حدث خطأ في معالجة رسالتك"}), 500
|
| 69 |
|
| 70 |
if __name__ == '__main__':
|
| 71 |
+
app.run(debug=True, port=5000)
|