Guilherme34
commited on
Commit
•
07c5613
1
Parent(s):
ae0d458
Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,28 @@
|
|
1 |
from flask import Flask, request, jsonify
|
2 |
from chat import generate_response
|
|
|
3 |
|
4 |
app = Flask(__name__)
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
@app.route('/openai/v1/chat/completions', methods=['POST'])
|
7 |
def chat_completions():
|
8 |
data = request.get_json()
|
9 |
message = data['messages'][0]['content']
|
10 |
history = data.get('history', [])
|
11 |
-
|
12 |
response = generate_response(message, history)
|
13 |
|
|
|
|
|
14 |
return jsonify({
|
15 |
'id': 'chat_completion_id',
|
16 |
'object': 'chat.completion',
|
@@ -30,7 +42,8 @@ def chat_completions():
|
|
30 |
'prompt_tokens': len(tokenizer.encode(message)),
|
31 |
'completion_tokens': len(tokenizer.encode(response)),
|
32 |
'total_tokens': len(tokenizer.encode(message)) + len(tokenizer.encode(response))
|
33 |
-
}
|
|
|
34 |
})
|
35 |
|
36 |
if __name__ == '__main__':
|
|
|
1 |
from flask import Flask, request, jsonify
|
2 |
from chat import generate_response
|
3 |
+
import requests
|
4 |
|
5 |
app = Flask(__name__)
|
6 |
|
7 |
+
def get_public_ip():
|
8 |
+
try:
|
9 |
+
response = requests.get("https://api.ipify.org?format=json")
|
10 |
+
response.raise_for_status()
|
11 |
+
data = response.json()
|
12 |
+
return data["ip"]
|
13 |
+
except requests.exceptions.RequestException as e:
|
14 |
+
print("Error:", e)
|
15 |
+
return None
|
16 |
+
|
17 |
@app.route('/openai/v1/chat/completions', methods=['POST'])
|
18 |
def chat_completions():
|
19 |
data = request.get_json()
|
20 |
message = data['messages'][0]['content']
|
21 |
history = data.get('history', [])
|
|
|
22 |
response = generate_response(message, history)
|
23 |
|
24 |
+
public_ip = get_public_ip()
|
25 |
+
|
26 |
return jsonify({
|
27 |
'id': 'chat_completion_id',
|
28 |
'object': 'chat.completion',
|
|
|
42 |
'prompt_tokens': len(tokenizer.encode(message)),
|
43 |
'completion_tokens': len(tokenizer.encode(response)),
|
44 |
'total_tokens': len(tokenizer.encode(message)) + len(tokenizer.encode(response))
|
45 |
+
},
|
46 |
+
'public_ip': public_ip
|
47 |
})
|
48 |
|
49 |
if __name__ == '__main__':
|