MHD011 commited on
Commit
fdb7b37
·
verified ·
1 Parent(s): 5d000a9

Upload 4 files

Browse files
Files changed (2) hide show
  1. Dockerfile +6 -0
  2. app.py +95 -10
Dockerfile CHANGED
@@ -2,18 +2,23 @@ FROM python:3.9-slim
2
 
3
  WORKDIR /app
4
 
 
5
  RUN apt-get update && apt-get install -y \
6
  build-essential \
7
  libpq-dev \
 
8
  && rm -rf /var/lib/apt/lists/*
9
 
10
  COPY . .
11
 
 
12
  RUN mkdir -p /app/model_cache && chmod -R 777 /app
13
 
 
14
  RUN pip install --upgrade pip setuptools wheel
15
  RUN pip install --no-cache-dir -r requirements.txt
16
 
 
17
  ENV FLASK_APP=app.py
18
  ENV FLASK_ENV=production
19
  ENV TRANSFORMERS_CACHE=/app/model_cache
@@ -23,4 +28,5 @@ ENV TOKENIZERS_PARALLELISM=false
23
 
24
  EXPOSE 7860
25
 
 
26
  CMD ["gunicorn", "--workers", "1", "--threads", "2", "--bind", "0.0.0.0:7860", "--timeout", "120", "--preload", "app:app"]
 
2
 
3
  WORKDIR /app
4
 
5
+ # تثبيت الاعتمادات النظامية
6
  RUN apt-get update && apt-get install -y \
7
  build-essential \
8
  libpq-dev \
9
+ python3-dev \
10
  && rm -rf /var/lib/apt/lists/*
11
 
12
  COPY . .
13
 
14
+ # إنشاء مجلد الذاكرة المؤقتة
15
  RUN mkdir -p /app/model_cache && chmod -R 777 /app
16
 
17
+ # تثبيت المتطلبات
18
  RUN pip install --upgrade pip setuptools wheel
19
  RUN pip install --no-cache-dir -r requirements.txt
20
 
21
+ # متغيرات البيئة
22
  ENV FLASK_APP=app.py
23
  ENV FLASK_ENV=production
24
  ENV TRANSFORMERS_CACHE=/app/model_cache
 
28
 
29
  EXPOSE 7860
30
 
31
+ # تشغيل التطبيق
32
  CMD ["gunicorn", "--workers", "1", "--threads", "2", "--bind", "0.0.0.0:7860", "--timeout", "120", "--preload", "app:app"]
app.py CHANGED
@@ -1,12 +1,12 @@
1
  import os
2
  import re
3
  import logging
4
- from flask import Flask, request, jsonify, Response
5
  from flask_cors import CORS
6
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
7
  import torch
8
  import psycopg2
9
- import json
10
 
11
  app = Flask(__name__)
12
  CORS(app)
@@ -33,18 +33,26 @@ def initialize():
33
  logger.info("🚀 جاري تحميل النموذج...")
34
  tokenizer = AutoTokenizer.from_pretrained(
35
  MODEL_NAME,
36
- cache_dir="model_cache",
37
- local_files_only=False
38
  )
 
 
39
  model = AutoModelForSeq2SeqLM.from_pretrained(
40
  MODEL_NAME,
41
  cache_dir="model_cache",
42
  device_map="auto",
43
- torch_dtype=torch.float16,
44
  low_cpu_mem_usage=True
45
  )
 
 
 
 
 
 
 
 
46
  model.eval()
47
- logger.info("✅ تم تحميل النموذج بنجاح")
48
  except Exception as e:
49
  logger.error(f"❌ فشل في تحميل النموذج: {str(e)}")
50
  raise
@@ -55,13 +63,86 @@ initialize()
55
  DB_SCHEMA = """
56
  CREATE TABLE public.profiles (
57
  id uuid NOT NULL,
 
 
 
 
 
58
  cam_mac text UNIQUE,
59
- ...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  );
 
61
  CREATE TABLE public.data (
62
- id bigint GENERATED ALWAYS AS IDENTITY,
 
 
 
 
 
 
63
  cam_mac text,
64
- ...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  );
66
  """.strip()
67
 
@@ -119,7 +200,10 @@ def handle_query():
119
  SQL:
120
  """
121
 
122
- inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512).to(model.device)
 
 
 
123
 
124
  with torch.no_grad():
125
  outputs = model.generate(
@@ -189,6 +273,7 @@ def health_check():
189
  return jsonify({
190
  "status": "healthy",
191
  "model_loaded": model is not None,
 
192
  "db_connection": get_db_connection() is not None,
193
  "timestamp": datetime.now().isoformat()
194
  })
 
1
  import os
2
  import re
3
  import logging
4
+ from flask import Flask, request, jsonify
5
  from flask_cors import CORS
6
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
7
  import torch
8
  import psycopg2
9
+ from datetime import datetime
10
 
11
  app = Flask(__name__)
12
  CORS(app)
 
33
  logger.info("🚀 جاري تحميل النموذج...")
34
  tokenizer = AutoTokenizer.from_pretrained(
35
  MODEL_NAME,
36
+ cache_dir="model_cache"
 
37
  )
38
+
39
+ # تحميل النموذج مع إعدادات الذاكرة المنخفضة
40
  model = AutoModelForSeq2SeqLM.from_pretrained(
41
  MODEL_NAME,
42
  cache_dir="model_cache",
43
  device_map="auto",
44
+ torch_dtype=torch.float32, # استخدام float32 بدلاً من float16
45
  low_cpu_mem_usage=True
46
  )
47
+
48
+ # التحقق من توفر GPU
49
+ if torch.cuda.is_available():
50
+ model.to('cuda')
51
+ logger.info("✅ تم تحميل النموذج على GPU")
52
+ else:
53
+ logger.info("✅ تم تحميل النموذج على CPU")
54
+
55
  model.eval()
 
56
  except Exception as e:
57
  logger.error(f"❌ فشل في تحميل النموذج: {str(e)}")
58
  raise
 
63
  DB_SCHEMA = """
64
  CREATE TABLE public.profiles (
65
  id uuid NOT NULL,
66
+ updated_at timestamp with time zone,
67
+ username text UNIQUE CHECK (char_length(username) >= 3),
68
+ full_name text,
69
+ avatar_url text,
70
+ website text,
71
  cam_mac text UNIQUE,
72
+ fcm_token text,
73
+ notification_enabled boolean DEFAULT true,
74
+ CONSTRAINT profiles_pkey PRIMARY KEY (id),
75
+ CONSTRAINT profiles_id_fkey FOREIGN KEY (id) REFERENCES auth.users(id)
76
+ );
77
+
78
+ CREATE TABLE public.place (
79
+ id bigint GENERATED ALWAYS AS IDENTITY NOT NULL,
80
+ created_at timestamp with time zone DEFAULT (now() AT TIME ZONE 'utc'::text),
81
+ name text,
82
+ CONSTRAINT place_pkey PRIMARY KEY (id)
83
+ );
84
+
85
+ CREATE TABLE public.user_place (
86
+ id bigint GENERATED ALWAYS AS IDENTITY NOT NULL,
87
+ created_at timestamp with time zone NOT NULL DEFAULT now(),
88
+ place_id bigint,
89
+ user_cam_mac text,
90
+ CONSTRAINT user_place_pkey PRIMARY KEY (id),
91
+ CONSTRAINT user_place_place_id_fkey FOREIGN KEY (place_id) REFERENCES public.place(id),
92
+ CONSTRAINT user_place_user_cam_mac_fkey FOREIGN KEY (user_cam_mac) REFERENCES public.profiles(cam_mac)
93
  );
94
+
95
  CREATE TABLE public.data (
96
+ id bigint GENERATED ALWAYS AS IDENTITY NOT NULL,
97
+ created_at timestamp without time zone,
98
+ caption text,
99
+ image_url text,
100
+ latitude double precision DEFAULT '36.1833854'::double precision,
101
+ longitude double precision DEFAULT '37.1309255'::double precision,
102
+ user_place_id bigint,
103
  cam_mac text,
104
+ CONSTRAINT data_pkey PRIMARY KEY (id),
105
+ CONSTRAINT data_user_place_id_fkey FOREIGN KEY (user_place_id) REFERENCES public.user_place(id)
106
+ );
107
+
108
+ CREATE TABLE public.biodata (
109
+ id bigint GENERATED ALWAYS AS IDENTITY NOT NULL,
110
+ created_at timestamp with time zone NOT NULL DEFAULT now(),
111
+ mac_address text,
112
+ acceleration_x double precision,
113
+ acceleration_y double precision,
114
+ acceleration_z double precision,
115
+ gyro_x double precision,
116
+ gyro_y double precision,
117
+ gyro_z double precision,
118
+ temperature double precision,
119
+ CONSTRAINT biodata_pkey PRIMARY KEY (id),
120
+ CONSTRAINT biodata_mac_address_fkey FOREIGN KEY (mac_address) REFERENCES public.profiles(cam_mac)
121
+ );
122
+
123
+ CREATE TABLE public.notification (
124
+ id bigint GENERATED ALWAYS AS IDENTITY NOT NULL,
125
+ created_at timestamp without time zone NOT NULL DEFAULT now(),
126
+ user_cam_mac text,
127
+ title text,
128
+ message text,
129
+ is_read boolean,
130
+ acceleration_x double precision,
131
+ acceleration_y double precision,
132
+ acceleration_z double precision,
133
+ gyro_x double precision,
134
+ gyro_y double precision,
135
+ gyro_z double precision,
136
+ CONSTRAINT notification_pkey PRIMARY KEY (id),
137
+ CONSTRAINT notification_user_cam_mac_fkey FOREIGN KEY (user_cam_mac) REFERENCES public.profiles(cam_mac)
138
+ );
139
+
140
+ CREATE TABLE public.flag (
141
+ id bigint GENERATED ALWAYS AS IDENTITY NOT NULL,
142
+ flag smallint,
143
+ user_mac_address text,
144
+ CONSTRAINT flag_pkey PRIMARY KEY (id),
145
+ CONSTRAINT flag_user_mac_address_fkey FOREIGN KEY (user_mac_address) REFERENCES public.profiles(cam_mac)
146
  );
147
  """.strip()
148
 
 
200
  SQL:
201
  """
202
 
203
+ # تحضير المدخلات مع التحقق من الجهاز
204
+ inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512)
205
+ if torch.cuda.is_available():
206
+ inputs = inputs.to('cuda')
207
 
208
  with torch.no_grad():
209
  outputs = model.generate(
 
273
  return jsonify({
274
  "status": "healthy",
275
  "model_loaded": model is not None,
276
+ "device": str(model.device) if model else "none",
277
  "db_connection": get_db_connection() is not None,
278
  "timestamp": datetime.now().isoformat()
279
  })