Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,12 @@
|
|
1 |
-
from flask import Flask, request, jsonify, Response
|
2 |
import hmac, hashlib, secrets, time
|
3 |
import requests
|
4 |
import os
|
|
|
5 |
|
6 |
app = Flask(__name__)
|
7 |
|
8 |
-
#
|
9 |
SECRET_KEY = os.getenv("SECRET_KEY")
|
10 |
|
11 |
# Track API statistics
|
@@ -50,35 +51,37 @@ def validate_api_key(api_key):
|
|
50 |
expected_signature = hmac.new(SECRET_KEY.encode(), random_part.encode(), hashlib.sha256).hexdigest()[:16]
|
51 |
return expected_signature == received_signature
|
52 |
|
53 |
-
#
|
54 |
-
def
|
55 |
json_data = {
|
56 |
'model': 'meta-llama/Llama-3.3-70B-Instruct-Turbo',
|
57 |
'messages': [
|
58 |
-
{'role': 'system', 'content': 'You are TrueSyncAI, a pioneering AI startup founded by Sujal Rajpoot and Anuj Rajpoot.'},
|
59 |
{'role': 'user', 'content': query}
|
60 |
],
|
61 |
-
'stream': True,
|
62 |
-
'max_tokens': 2048,
|
63 |
-
'temperature': 0.2,
|
64 |
-
'top_p': 1
|
65 |
}
|
66 |
-
|
67 |
try:
|
68 |
-
|
69 |
'https://api.deepinfra.com/v1/openai/chat/completions',
|
70 |
json=json_data, stream=True, timeout=None
|
71 |
-
)
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
|
|
75 |
try:
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
|
|
|
|
|
|
|
|
82 |
@app.route("/v1/chat/completions", methods=["POST"])
|
83 |
def chat():
|
84 |
global request_count
|
@@ -93,8 +96,7 @@ def chat():
|
|
93 |
return jsonify({"error": "Message cannot be empty"}), 400
|
94 |
|
95 |
request_count += 1
|
96 |
-
|
97 |
-
return Response(stream_with_context(generate_stream_response(message)), content_type="text/plain")
|
98 |
|
99 |
if __name__ == "__main__":
|
100 |
-
app.run(host="0.0.0.0", port=7860) #
|
|
|
1 |
+
from flask import Flask, request, jsonify, Response
|
2 |
import hmac, hashlib, secrets, time
|
3 |
import requests
|
4 |
import os
|
5 |
+
import json
|
6 |
|
7 |
app = Flask(__name__)
|
8 |
|
9 |
+
# Secret key for API authentication (Load from environment in production)
|
10 |
SECRET_KEY = os.getenv("SECRET_KEY")
|
11 |
|
12 |
# Track API statistics
|
|
|
51 |
expected_signature = hmac.new(SECRET_KEY.encode(), random_part.encode(), hashlib.sha256).hexdigest()[:16]
|
52 |
return expected_signature == received_signature
|
53 |
|
54 |
+
# Stream response from DeepInfra
|
55 |
+
def stream_response(query: str):
|
56 |
json_data = {
|
57 |
'model': 'meta-llama/Llama-3.3-70B-Instruct-Turbo',
|
58 |
'messages': [
|
59 |
+
{'role': 'system', 'content': 'You are TrueSyncAI, a pioneering AI startup founded by Sujal Rajpoot and Anuj Rajpoot. Your mission is to bring virtual versions of individuals into the real world, redefining human-AI interaction.'},
|
60 |
{'role': 'user', 'content': query}
|
61 |
],
|
62 |
+
'stream': True, 'max_tokens': 2048, 'temperature': 0.2, 'top_p': 1
|
|
|
|
|
|
|
63 |
}
|
|
|
64 |
try:
|
65 |
+
response = requests.post(
|
66 |
'https://api.deepinfra.com/v1/openai/chat/completions',
|
67 |
json=json_data, stream=True, timeout=None
|
68 |
+
)
|
69 |
+
response.raise_for_status()
|
70 |
+
|
71 |
+
def generate():
|
72 |
+
for value in response.iter_lines(decode_unicode=True, chunk_size=1000):
|
73 |
+
if value:
|
74 |
try:
|
75 |
+
content = json.loads(value[5:])['choices'][0]['delta']['content']
|
76 |
+
yield content + '\n'
|
77 |
+
except:
|
78 |
+
continue
|
79 |
+
|
80 |
+
return Response(generate(), content_type='text/plain')
|
81 |
+
except:
|
82 |
+
return jsonify({"error": "API Server is under maintenance. Please try again later."})
|
83 |
+
|
84 |
+
# Chat Endpoint with streaming response
|
85 |
@app.route("/v1/chat/completions", methods=["POST"])
|
86 |
def chat():
|
87 |
global request_count
|
|
|
96 |
return jsonify({"error": "Message cannot be empty"}), 400
|
97 |
|
98 |
request_count += 1
|
99 |
+
return stream_response(message)
|
|
|
100 |
|
101 |
if __name__ == "__main__":
|
102 |
+
app.run(host="0.0.0.0", port=7860, threaded=True) # Enable threaded mode for better concurrency
|