Upload 2 files
Browse files- app.py +36 -0
- requirements (2).txt +2 -0
app.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
from hashlib import sha256
|
4 |
+
import json
|
5 |
+
import concurrent.futures
|
6 |
+
|
7 |
+
app = Flask(__name__)
|
8 |
+
|
9 |
+
hashed_api_key = "47d8666ed003a61b3bbc900ce94dc1ce8a1f9d0f24ee1ceb5e2f20381eb3f3a6"
|
10 |
+
client = InferenceClient("mistralai/Mistral-Small-24B-Instruct-2501")
|
11 |
+
|
12 |
+
def generate_response(params):
|
13 |
+
try:
|
14 |
+
response = client.chat_completion(**params)
|
15 |
+
# Extract the content from the response
|
16 |
+
message_content = response.choices[0].message.content
|
17 |
+
return message_content
|
18 |
+
except Exception as e:
|
19 |
+
return f"An error occurred: {str(e)}"
|
20 |
+
|
21 |
+
@app.route('/respond', methods=['POST'])
|
22 |
+
def respond():
|
23 |
+
params = request.json # Уже Python-объект
|
24 |
+
api_key = params.pop("api_key", None)
|
25 |
+
if sha256(api_key.encode('utf-8')).hexdigest() != hashed_api_key:
|
26 |
+
return jsonify({"error": "Invalid API key"}), 403
|
27 |
+
|
28 |
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
29 |
+
future = executor.submit(generate_response, params)
|
30 |
+
result = future.result()
|
31 |
+
return jsonify({"response": result})
|
32 |
+
|
33 |
+
|
34 |
+
if __name__ == "__main__":
|
35 |
+
print('Ok')
|
36 |
+
app.run('0.0.0.0', port=7860)
|
requirements (2).txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
huggingface_hub==0.25.2
|
2 |
+
flask
|