nirkyy commited on
Commit
8ffd7c8
·
verified ·
1 Parent(s): 63af3e4

Create Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +79 -0
Dockerfile ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM alpine:latest
2
+
3
+ WORKDIR /app
4
+
5
+ ENV PORT=7860
6
+ ENV MODEL_ID="gemini-1.5-flash-latest"
7
+ ENV GENERATE_CONTENT_API="streamGenerateContent"
8
+
9
+ RUN apk update && \
10
+ apk add --no-cache python3 py3-pip curl jq && \
11
+ rm -rf /var/cache/apk/*
12
+
13
+ RUN pip3 install --no-cache-dir flask flask-cors
14
+
15
+ RUN echo '#!/bin/bash \n\
16
+ set -e \n\
17
+ \n\
18
+ SYSTEM_PROMPT="$1" \n\
19
+ USER_PROMPT="$2" \n\
20
+ \n\
21
+ API_KEYS=("apikey1" "apikey2" "apikey3") \n\
22
+ RANDOM_INDEX=$((RANDOM % ${#API_KEYS[@]})) \n\
23
+ GEMINI_API_KEY=${API_KEYS[$RANDOM_INDEX]} \n\
24
+ \n\
25
+ JSON_PAYLOAD=$(jq -n \
26
+ --arg sp "$SYSTEM_PROMPT" \
27
+ --arg up "$USER_PROMPT" \
28
+ \'{ "system_instruction": { "parts": [ { "text": $sp } ] }, "contents": [ { "role": "user", "parts": [ { "text": $up } ] } ] }\') \n\
29
+ \n\
30
+ API_URL="https://generativelanguage.googleapis.com/v1beta/models/${MODEL_ID}:${GENERATE_CONTENT_API}?key=${GEMINI_API_KEY}" \n\
31
+ \n\
32
+ curl -s -X POST -H "Content-Type: application/json" "$API_URL" -d "$JSON_PAYLOAD" | \
33
+ jq -r \'.candidates[0].content.parts[0].text\' | \
34
+ tr -d \'\\n\' \
35
+ ' > /usr/local/bin/gemini_handler && chmod +x /usr/local/bin/gemini_handler
36
+
37
+ RUN echo 'import os \n\
38
+ import subprocess \n\
39
+ from flask import Flask, request, jsonify \n\
40
+ from flask_cors import CORS \n\
41
+ \n\
42
+ app = Flask(__name__) \n\
43
+ CORS(app) \n\
44
+ \n\
45
+ @app.route("/") \n\
46
+ def health_check(): \n\
47
+ return jsonify({"status": "ok", "message": "API is running"}) \n\
48
+ \n\
49
+ @app.route("/chat", methods=["POST"]) \n\
50
+ def chat_handler(): \n\
51
+ data = request.get_json() \n\
52
+ if not data or "prompt" not in data: \n\
53
+ return jsonify({"error": "Missing \'prompt\' in request body"}), 400 \n\
54
+ \n\
55
+ system_prompt = data.get("system", "You are a helpful assistant.") \n\
56
+ user_prompt = data.get("prompt") \n\
57
+ \n\
58
+ try: \n\
59
+ result = subprocess.run( \n\
60
+ ["/usr/local/bin/gemini_handler", system_prompt, user_prompt], \n\
61
+ capture_output=True, \n\
62
+ text=True, \n\
63
+ check=True, \n\
64
+ timeout=60 \n\
65
+ ) \n\
66
+ return jsonify({"response": result.stdout.strip()}) \n\
67
+ except subprocess.CalledProcessError as e: \n\
68
+ return jsonify({"error": "Failed to call Gemini API", "details": e.stderr}), 500 \n\
69
+ except subprocess.TimeoutExpired: \n\
70
+ return jsonify({"error": "Request to Gemini API timed out"}), 504 \n\
71
+ \n\
72
+ if __name__ == "__main__": \n\
73
+ port = int(os.environ.get("PORT", 7860)) \n\
74
+ app.run(host="0.0.0.0", port=port) \n\
75
+ ' > app.py
76
+
77
+ EXPOSE 7860
78
+
79
+ CMD ["python3", "app.py"]