Spaces:
Running
Running
first commit
Browse files- Dockerfile +31 -0
- app.py +68 -0
- download_model.py +5 -0
- requirements.txt +5 -0
Dockerfile
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
|
| 2 |
+
# Hugging Face Spaces best practices for Docker
|
| 3 |
+
|
| 4 |
+
FROM python:3.9
|
| 5 |
+
|
| 6 |
+
# สร้าง non-root user เพื่อความปลอดภัย
|
| 7 |
+
RUN useradd -m -u 1000 user
|
| 8 |
+
USER user
|
| 9 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
| 10 |
+
|
| 11 |
+
# ตั้งค่า cache directory สำหรับ Hugging Face models
|
| 12 |
+
# ใช้ /tmp เพราะมี space มากกว่า default location (~/.cache/)
|
| 13 |
+
ENV HF_HOME="/home/user/.cache/huggingface"
|
| 14 |
+
|
| 15 |
+
WORKDIR /app
|
| 16 |
+
|
| 17 |
+
# Copy requirements และ install dependencies
|
| 18 |
+
COPY --chown=user ./requirements.txt
|
| 19 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 20 |
+
|
| 21 |
+
COPY --chown=user ./download_model.py
|
| 22 |
+
RUN python download_model.py
|
| 23 |
+
|
| 24 |
+
# Copy application code
|
| 25 |
+
COPY --chown=user . /app
|
| 26 |
+
|
| 27 |
+
# Expose port 7860 (default port สำหรับ Hugging Face Spaces)
|
| 28 |
+
EXPOSE 7860
|
| 29 |
+
|
| 30 |
+
# Run Flask app with Gunicorn (production WSGI server)
|
| 31 |
+
CMD ["gunicorn", "app:app", "--bind", "0.0.0.0:7860", "--workers", "1", "--timeout", "120"]
|
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, Response
|
| 2 |
+
from flask_cors import CORS
|
| 3 |
+
from sentence_transformers import SentenceTransformer
|
| 4 |
+
|
| 5 |
+
# 1. สร้าง Flask app
|
| 6 |
+
app = Flask(__name__)
|
| 7 |
+
|
| 8 |
+
# 2. ตั้งค่า CORS (Cross-Origin Resource Sharing)
|
| 9 |
+
# นี่คือส่วนสำคัญมาก! ที่จะอนุญาตให้ JavaScript (ที่อยู่คนละที่) เรียก API นี้ได้
|
| 10 |
+
CORS(app)
|
| 11 |
+
|
| 12 |
+
model = SentenceTransformer("google/embeddinggemma-300m")
|
| 13 |
+
|
| 14 |
+
# 3. สร้าง "Endpoint" หรือ "Route" ของ API
|
| 15 |
+
# นี่คือ URL ที่ JavaScript จะเรียกมา
|
| 16 |
+
# เช่น http://127.0.0.1:5000/api/v1/chat/completions
|
| 17 |
+
|
| 18 |
+
@app.route("/api/v1/embeddings", methods=['GET'])
|
| 19 |
+
def get_start():
|
| 20 |
+
return jsonify({"message": "Server is running"})
|
| 21 |
+
|
| 22 |
+
@app.route("/api/v1/embeddings", methods=['POST'])
|
| 23 |
+
def return_query_embedding():
|
| 24 |
+
# 1. รับ JSON Payload
|
| 25 |
+
try:
|
| 26 |
+
data = request.json
|
| 27 |
+
if not data:
|
| 28 |
+
return jsonify({"error": "No JSON data provided"}), 400
|
| 29 |
+
except Exception:
|
| 30 |
+
# ดักจับกรณีที่ JSON ที่ส่งมาผิดรูปแบบ
|
| 31 |
+
return jsonify({"error": "Invalid JSON format"}), 400
|
| 32 |
+
|
| 33 |
+
# 2. ดึงข้อความสนทนา (Messages)
|
| 34 |
+
query = data.get("text")
|
| 35 |
+
|
| 36 |
+
if not query:
|
| 37 |
+
# ตรวจสอบว่ามี key 'messages' อยู่ใน JSON ที่ส่งมาหรือไม่
|
| 38 |
+
return jsonify({"error": "Missing 'text' key in request body (JSON)"}), 400
|
| 39 |
+
|
| 40 |
+
# 3. เรียกใช้ Logic
|
| 41 |
+
try:
|
| 42 |
+
query_embeddings = model.encode_query(query)
|
| 43 |
+
# convert to json
|
| 44 |
+
query_embeddings = query_embeddings.tolist()
|
| 45 |
+
query_embeddings_json = {
|
| 46 |
+
"query_embeddings": query_embeddings
|
| 47 |
+
}
|
| 48 |
+
response = jsonify(query_embeddings_json)
|
| 49 |
+
|
| 50 |
+
# 4. ส่งคำตอบกลับไป
|
| 51 |
+
# ใช้ Response Object เพื่อให้แน่ใจว่า Header (mimetype) และ Encoding (UTF-8) ถูกต้อง
|
| 52 |
+
# เพื่อรองรับภาษาไทยตามที่คุณใช้ ensure_ascii=False
|
| 53 |
+
return Response(
|
| 54 |
+
response,
|
| 55 |
+
mimetype='application/json; charset=utf-8',
|
| 56 |
+
status=200
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
except Exception as e:
|
| 60 |
+
# ดักจับ Error ที่อาจเกิดขึ้นภายในฟังก์ชัน LLM
|
| 61 |
+
print(f"Error: {e}")
|
| 62 |
+
return jsonify({"error": "Internal server error"}), 500
|
| 63 |
+
|
| 64 |
+
# 6. สั่งให้ app ทำงาน (Run) เมื่อรันไฟล์นี้ตรงๆ (สำหรับ local development เท่านั้น)
|
| 65 |
+
# สำหรับ production บน HF Spaces จะใช้ Gunicorn แทน
|
| 66 |
+
if __name__ == "__main__":
|
| 67 |
+
# Local development mode
|
| 68 |
+
app.run(host="0.0.0.0", port=7860, debug=False)
|
download_model.py
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sentence_transformers import SentenceTransformer
|
| 2 |
+
|
| 3 |
+
print("Downloading model...")
|
| 4 |
+
model = SentenceTransformer("google/embeddinggemma-300m")
|
| 5 |
+
print("Model downloaded and cached successfully!")
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Flask==3.0.0
|
| 2 |
+
flask-cors==4.0.0
|
| 3 |
+
sentence-transformers==2.2.2
|
| 4 |
+
torch==2.1.0
|
| 5 |
+
gunicorn==21.2.0
|