MHD011 commited on
Commit
6039d0c
·
verified ·
1 Parent(s): 2d66b35

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +11 -10
  2. app.py +175 -164
  3. requirements.txt +6 -7
Dockerfile CHANGED
@@ -1,17 +1,18 @@
1
- # Use an official Python runtime as a parent image
 
 
 
2
  FROM python:3.9-slim
3
 
4
- # Set the working directory in the container
5
  WORKDIR /app
6
 
7
- # Copy the current directory contents into the container
8
- COPY . .
9
-
10
- # Install any needed packages specified in requirements.txt
11
  RUN pip install --no-cache-dir -r requirements.txt
12
 
13
- # Make port 7860 available to the world outside this container
14
- EXPOSE 7860
 
 
 
15
 
16
- # Run app.py when the container launches
17
- CMD ["python", "app.py"]
 
1
+
2
+ ## ملف `Dockerfile` (اختياري)
3
+
4
+ ```dockerfile
5
  FROM python:3.9-slim
6
 
 
7
  WORKDIR /app
8
 
9
+ COPY requirements.txt .
 
 
 
10
  RUN pip install --no-cache-dir -r requirements.txt
11
 
12
+ RUN mkdir -p /app/model_cache && chmod -R 777 /app/model_cache
13
+ ENV TRANSFORMERS_CACHE=/app/model_cache
14
+ ENV HF_HOME=/app/model_cache
15
+
16
+ COPY . .
17
 
18
+ CMD ["gunicorn", "--bind", "0.0.0.0:8080", "app:app"]
 
app.py CHANGED
@@ -1,164 +1,175 @@
1
- import os
2
- import logging
3
- from flask import Flask, request, jsonify
4
- from flask_cors import CORS
5
- from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
6
- import torch
7
-
8
- app = Flask(__name__)
9
- CORS(app)
10
-
11
- # --- إعداد السجل ---
12
- logging.basicConfig(level=logging.INFO)
13
- logger = logging.getLogger(__name__)
14
-
15
- # --- إعداد النموذج ---
16
- MODEL_NAME = "Salesforce/grappa_large_jnt"
17
- # يمكنك تغييره لنموذج آخر إذا رغبت
18
-
19
- tokenizer = None
20
- model = None
21
-
22
- def initialize():
23
- global tokenizer, model
24
- device = "cuda" if torch.cuda.is_available() else "cpu"
25
- logger.info(f"تحميل النموذج على الجهاز: {device}")
26
-
27
- tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
28
- model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
29
- logger.info("تم تحميل النموذج بنجاح")
30
-
31
- initialize()
32
-
33
- # --- سكيمة قاعدة البيانات (كمثال) ---
34
- DB_SCHEMA = """
35
- CREATE TABLE public.profiles (
36
- id uuid NOT NULL,
37
- updated_at timestamp with time zone,
38
- username text UNIQUE CHECK (char_length(username) >= 3),
39
- full_name text,
40
- avatar_url text,
41
- website text,
42
- cam_mac text UNIQUE,
43
- fcm_token text,
44
- notification_enabled boolean DEFAULT true,
45
- CONSTRAINT profiles_pkey PRIMARY KEY (id),
46
- CONSTRAINT profiles_id_fkey FOREIGN KEY (id) REFERENCES auth.users(id)
47
- );
48
-
49
- CREATE TABLE public.place (
50
- id bigint GENERATED ALWAYS AS IDENTITY NOT NULL,
51
- created_at timestamp with time zone DEFAULT (now() AT TIME ZONE 'utc'::text),
52
- name text,
53
- CONSTRAINT place_pkey PRIMARY KEY (id)
54
- );
55
-
56
- CREATE TABLE public.user_place (
57
- id bigint GENERATED ALWAYS AS IDENTITY NOT NULL,
58
- created_at timestamp with time zone NOT NULL DEFAULT now(),
59
- place_id bigint,
60
- user_cam_mac text,
61
- CONSTRAINT user_place_pkey PRIMARY KEY (id),
62
- CONSTRAINT user_place_place_id_fkey FOREIGN KEY (place_id) REFERENCES public.place(id),
63
- CONSTRAINT user_place_user_cam_mac_fkey FOREIGN KEY (user_cam_mac) REFERENCES public.profiles(cam_mac)
64
- );
65
-
66
- CREATE TABLE public.data (
67
- id bigint GENERATED ALWAYS AS IDENTITY NOT NULL,
68
- created_at timestamp without time zone,
69
- caption text,
70
- image_url text,
71
- latitude double precision DEFAULT '36.1833854'::double precision,
72
- longitude double precision DEFAULT '37.1309255'::double precision,
73
- user_place_id bigint,
74
- cam_mac text,
75
- CONSTRAINT data_pkey PRIMARY KEY (id),
76
- CONSTRAINT data_user_place_id_fkey FOREIGN KEY (user_place_id) REFERENCES public.user_place(id)
77
- );
78
-
79
- CREATE TABLE public.biodata (
80
- id bigint GENERATED ALWAYS AS IDENTITY NOT NULL,
81
- created_at timestamp with time zone NOT NULL DEFAULT now(),
82
- mac_address text,
83
- acceleration_x double precision,
84
- acceleration_y double precision,
85
- acceleration_z double precision,
86
- gyro_x double precision,
87
- gyro_y double precision,
88
- gyro_z double precision,
89
- temperature double precision,
90
- CONSTRAINT biodata_pkey PRIMARY KEY (id),
91
- CONSTRAINT biodata_mac_address_fkey FOREIGN KEY (mac_address) REFERENCES public.profiles(cam_mac)
92
- );
93
-
94
- CREATE TABLE public.notification (
95
- id bigint GENERATED ALWAYS AS IDENTITY NOT NULL,
96
- created_at timestamp without time zone NOT NULL DEFAULT now(),
97
- user_cam_mac text,
98
- title text,
99
- message text,
100
- is_read boolean,
101
- acceleration_x double precision,
102
- acceleration_y double precision,
103
- acceleration_z double precision,
104
- gyro_x double precision,
105
- gyro_y double precision,
106
- gyro_z double precision,
107
- CONSTRAINT notification_pkey PRIMARY KEY (id),
108
- CONSTRAINT notification_user_cam_mac_fkey FOREIGN KEY (user_cam_mac) REFERENCES public.profiles(cam_mac)
109
- );
110
-
111
- CREATE TABLE public.flag (
112
- id bigint GENERATED ALWAYS AS IDENTITY NOT NULL,
113
- flag smallint,
114
- user_mac_address text,
115
- CONSTRAINT flag_pkey PRIMARY KEY (id),
116
- CONSTRAINT flag_user_mac_address_fkey FOREIGN KEY (user_mac_address) REFERENCES public.profiles(cam_mac)
117
- );
118
- """.strip()
119
-
120
- @app.route('/generate-sql', methods=['POST'])
121
- def generate_sql():
122
- try:
123
- body = request.get_json()
124
- user_text = body.get("text", "").strip()
125
- cam_mac = body.get("cam_mac", "").strip()
126
-
127
- if not user_text or not cam_mac:
128
- return jsonify({"error": "يرجى إرسال 'text' و 'cam_mac'"}), 400
129
-
130
- prompt = f"""
131
- ### Postgres SQL table definitions
132
- {DB_SCHEMA}
133
-
134
- ### User question: {user_text}
135
-
136
- ### SQL query to answer the question filtered by cam_mac = '{cam_mac}':
137
- SELECT
138
- """.strip()
139
-
140
- inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
141
- outputs = model.generate(**inputs, max_length=256)
142
- sql = tokenizer.decode(outputs[0], skip_special_tokens=True)
143
-
144
- # تنظيف الناتج
145
- if not sql.lower().startswith("select"):
146
- sql = "SELECT " + sql
147
- if not sql.endswith(";"):
148
- sql += ";"
149
-
150
- return jsonify({"sql": sql})
151
-
152
- except Exception as e:
153
- logger.error(f"خطأ في التوليد: {str(e)}")
154
- return jsonify({"error": "فشل في توليد الاستعلام"}), 500
155
-
156
- @app.route('/')
157
- def home():
158
- return """
159
- <h1>Text2SQL API</h1>
160
- <p>Send a POST request to <code>/generate-sql</code> with JSON: {"text": "سؤالك", "cam_mac": "عنوان MAC"}</p>
161
- """
162
-
163
- if __name__ == '__main__':
164
- app.run(host='0.0.0.0', port=7860)
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import logging
3
+ from flask import Flask, request, jsonify
4
+ from flask_cors import CORS
5
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
6
+ import torch
7
+
8
+ app = Flask(__name__)
9
+ CORS(app)
10
+
11
+ # --- إعداد السجل ---
12
+ logging.basicConfig(level=logging.INFO)
13
+ logger = logging.getLogger(__name__)
14
+
15
+ # --- إعداد النموذج ---
16
+ MODEL_NAME = "tscholak/cxmefzzi" # نموذج Text-to-SQL بديل
17
+
18
+ tokenizer = None
19
+ model = None
20
+
21
+ def initialize():
22
+ global tokenizer, model
23
+
24
+ # تحديد مسار ذاكرة مؤقتة ضمن المساحة المسموح بها
25
+ cache_dir = os.path.join(os.getcwd(), "model_cache")
26
+ os.makedirs(cache_dir, exist_ok=True)
27
+
28
+ device = "cuda" if torch.cuda.is_available() else "cpu"
29
+ logger.info(f"تحميل النموذج على الجهاز: {device}")
30
+
31
+ try:
32
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, cache_dir=cache_dir)
33
+ model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME, cache_dir=cache_dir).to(device)
34
+ logger.info("تم تحميل النموذج بنجاح")
35
+ except Exception as e:
36
+ logger.error(f"فشل في تحميل النموذج: {str(e)}")
37
+ raise
38
+
39
+ initialize()
40
+
41
+ # --- سكيمة قاعدة البيانات ---
42
+ DB_SCHEMA = """
43
+ CREATE TABLE public.profiles (
44
+ id uuid NOT NULL,
45
+ updated_at timestamp with time zone,
46
+ username text UNIQUE CHECK (char_length(username) >= 3),
47
+ full_name text,
48
+ avatar_url text,
49
+ website text,
50
+ cam_mac text UNIQUE,
51
+ fcm_token text,
52
+ notification_enabled boolean DEFAULT true,
53
+ CONSTRAINT profiles_pkey PRIMARY KEY (id),
54
+ CONSTRAINT profiles_id_fkey FOREIGN KEY (id) REFERENCES auth.users(id)
55
+ );
56
+
57
+ CREATE TABLE public.place (
58
+ id bigint GENERATED ALWAYS AS IDENTITY NOT NULL,
59
+ created_at timestamp with time zone DEFAULT (now() AT TIME ZONE 'utc'::text),
60
+ name text,
61
+ CONSTRAINT place_pkey PRIMARY KEY (id)
62
+ );
63
+
64
+ CREATE TABLE public.user_place (
65
+ id bigint GENERATED ALWAYS AS IDENTITY NOT NULL,
66
+ created_at timestamp with time zone NOT NULL DEFAULT now(),
67
+ place_id bigint,
68
+ user_cam_mac text,
69
+ CONSTRAINT user_place_pkey PRIMARY KEY (id),
70
+ CONSTRAINT user_place_place_id_fkey FOREIGN KEY (place_id) REFERENCES public.place(id),
71
+ CONSTRAINT user_place_user_cam_mac_fkey FOREIGN KEY (user_cam_mac) REFERENCES public.profiles(cam_mac)
72
+ );
73
+
74
+ CREATE TABLE public.data (
75
+ id bigint GENERATED ALWAYS AS IDENTITY NOT NULL,
76
+ created_at timestamp without time zone,
77
+ caption text,
78
+ image_url text,
79
+ latitude double precision DEFAULT '36.1833854'::double precision,
80
+ longitude double precision DEFAULT '37.1309255'::double precision,
81
+ user_place_id bigint,
82
+ cam_mac text,
83
+ CONSTRAINT data_pkey PRIMARY KEY (id),
84
+ CONSTRAINT data_user_place_id_fkey FOREIGN KEY (user_place_id) REFERENCES public.user_place(id)
85
+ );
86
+
87
+ CREATE TABLE public.biodata (
88
+ id bigint GENERATED ALWAYS AS IDENTITY NOT NULL,
89
+ created_at timestamp with time zone NOT NULL DEFAULT now(),
90
+ mac_address text,
91
+ acceleration_x double precision,
92
+ acceleration_y double precision,
93
+ acceleration_z double precision,
94
+ gyro_x double precision,
95
+ gyro_y double precision,
96
+ gyro_z double precision,
97
+ temperature double precision,
98
+ CONSTRAINT biodata_pkey PRIMARY KEY (id),
99
+ CONSTRAINT biodata_mac_address_fkey FOREIGN KEY (mac_address) REFERENCES public.profiles(cam_mac)
100
+ );
101
+
102
+ CREATE TABLE public.notification (
103
+ id bigint GENERATED ALWAYS AS IDENTITY NOT NULL,
104
+ created_at timestamp without time zone NOT NULL DEFAULT now(),
105
+ user_cam_mac text,
106
+ title text,
107
+ message text,
108
+ is_read boolean,
109
+ acceleration_x double precision,
110
+ acceleration_y double precision,
111
+ acceleration_z double precision,
112
+ gyro_x double precision,
113
+ gyro_y double precision,
114
+ gyro_z double precision,
115
+ CONSTRAINT notification_pkey PRIMARY KEY (id),
116
+ CONSTRAINT notification_user_cam_mac_fkey FOREIGN KEY (user_cam_mac) REFERENCES public.profiles(cam_mac)
117
+ );
118
+
119
+ CREATE TABLE public.flag (
120
+ id bigint GENERATED ALWAYS AS IDENTITY NOT NULL,
121
+ flag smallint,
122
+ user_mac_address text,
123
+ CONSTRAINT flag_pkey PRIMARY KEY (id),
124
+ CONSTRAINT flag_user_mac_address_fkey FOREIGN KEY (user_mac_address) REFERENCES public.profiles(cam_mac)
125
+ );
126
+ """.strip()
127
+
128
+ @app.route('/generate-sql', methods=['POST'])
129
+ def generate_sql():
130
+ if tokenizer is None or model is None:
131
+ return jsonify({"error": "النموذج غير محمل، يرجى المحاولة لاحقاً"}), 503
132
+
133
+ try:
134
+ body = request.get_json()
135
+ user_text = body.get("text", "").strip()
136
+ cam_mac = body.get("cam_mac", "").strip()
137
+
138
+ if not user_text or not cam_mac:
139
+ return jsonify({"error": "يرجى إرسال 'text' و 'cam_mac'"}), 400
140
+
141
+ prompt = f"""
142
+ ### Postgres SQL table definitions
143
+ {DB_SCHEMA}
144
+
145
+ ### User question: {user_text}
146
+
147
+ ### SQL query to answer the question filtered by cam_mac = '{cam_mac}':
148
+ SELECT
149
+ """.strip()
150
+
151
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
152
+ outputs = model.generate(**inputs, max_length=256)
153
+ sql = tokenizer.decode(outputs[0], skip_special_tokens=True)
154
+
155
+ # تنظيف الناتج
156
+ if not sql.lower().startswith("select"):
157
+ sql = "SELECT " + sql
158
+ if not sql.endswith(";"):
159
+ sql += ";"
160
+
161
+ return jsonify({"sql": sql})
162
+
163
+ except Exception as e:
164
+ logger.error(f"خطأ في التوليد: {str(e)}")
165
+ return jsonify({"error": "فشل في توليد الاستعلام"}), 500
166
+
167
+ @app.route('/')
168
+ def home():
169
+ return """
170
+ <h1>Text2SQL API</h1>
171
+ <p>Send a POST request to <code>/generate-sql</code> with JSON: {"text": "سؤالك", "cam_mac": "عنوان MAC"}</p>
172
+ """
173
+
174
+ if __name__ == '__main__':
175
+ app.run()
requirements.txt CHANGED
@@ -1,7 +1,6 @@
1
- flask
2
- flask-cors
3
- transformers
4
- torch
5
- accelerate
6
- sentencepiece
7
- psycopg2-binary
 
1
+ flask>=2.0.0
2
+ flask-cors>=3.0.0
3
+ torch>=2.0.0
4
+ transformers>=4.30.0
5
+ accelerate
6
+ huggingface-hub>=0.15.0