Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,57 @@
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
import requests
|
| 3 |
-
import json
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
|
| 7 |
-
# Define the
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
# Define the headers
|
| 11 |
-
headers = {
|
| 12 |
-
"Authorization": "Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IklUMU5Lb2ItOFBsY3ZJdGd2NWRIaiIsImVtYWlsIjoiYWxob29yc2hvcHBAZ21haWwuY29tIiwic3ViIjoiSVQxTktvYi04UGxjdkl0Z3Y1ZEhqIiwiaWF0IjoxNzM0OTQ4MTk3LjIzNiwiZXhwIjoxNzM1NTUyOTk3fQ.OXIabj1PQ_NHQcFTr406OAKS1kunVjYd9EMaiPUk5uxqXZd6vXd-C4CC-tXRPCG1q0UnqbGlDJDQsgKIXEfs2zUNZm_28wO577MLe4ANkr12Z79jpNcNGdllmpCdaZsYv6_kFnqavlyPgp45TjIJDFaWQ7yRqaS_CKVkle9CRKNQk4Kv1O6_1YFJFlKqI8AeOL3TrHOGY2HiAW0HbPIsPeC2rYfQwzFjgiQ1BziofgJZnR1bXPVDqHvmZWlgRbphypshCroc56e0vbxhGAJsd4Xdw3-DT3NdD3u76QLPekcU4Lx_i5f7LxHATVSmmr2W0nFw6-vCkVwRQku8EkDBMQ",
|
| 13 |
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
|
| 14 |
-
"x-
|
| 15 |
-
"
|
| 16 |
-
"
|
| 17 |
-
"x-nonce": "5852edfbd49cf2926a78368bb1a36491",
|
| 18 |
-
"x-signature": "70d66c850525d9908d733f1af105ab39",
|
| 19 |
-
"x-timestamp": "1734981969"
|
| 20 |
}
|
| 21 |
|
| 22 |
-
@app.route('/
|
| 23 |
-
def
|
| 24 |
try:
|
| 25 |
-
# Get the
|
| 26 |
data = request.get_json()
|
| 27 |
question = data.get('question', '')
|
| 28 |
history = data.get('history', [])
|
| 29 |
|
| 30 |
-
#
|
| 31 |
payload = {
|
| 32 |
-
"
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
"
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
"
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
),
|
| 48 |
-
"history": history,
|
| 49 |
-
"temperature": 0.7,
|
| 50 |
-
"userId": "IT1NKob-8PlcvItgv5dHj"
|
| 51 |
}
|
| 52 |
|
| 53 |
-
# Send the POST request to the
|
| 54 |
-
response = requests.post(
|
| 55 |
-
|
| 56 |
-
# Process the streamed response
|
| 57 |
-
message = ''
|
| 58 |
-
for line in response.iter_lines():
|
| 59 |
-
if line:
|
| 60 |
-
event = json.loads(line.decode('utf-8'))
|
| 61 |
-
message += event['data']
|
| 62 |
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
except Exception as e:
|
| 66 |
-
return jsonify({"error": str(e)}), 500
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
| 69 |
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
import requests
|
|
|
|
| 3 |
|
| 4 |
app = Flask(__name__)
|
| 5 |
|
| 6 |
+
# Define the URL and headers for the external API
|
| 7 |
+
EXTERNAL_API_URL = "https://api.botpress.cloud/v1/cognitive/chat-gpt/query"
|
| 8 |
+
HEADERS = {
|
|
|
|
|
|
|
|
|
|
| 9 |
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
|
| 10 |
+
"x-bot-id": "fbe047bc-06fb-44c1-9672-342fe1a2e506",
|
| 11 |
+
"Content-Type": "application/json",
|
| 12 |
+
"Cookie": "YOUR_COOKIES_HERE", # Replace with actual cookie value
|
|
|
|
|
|
|
|
|
|
| 13 |
}
|
| 14 |
|
| 15 |
+
@app.route('/query', methods=['POST'])
|
| 16 |
+
def query():
|
| 17 |
try:
|
| 18 |
+
# Get the JSON payload from the request
|
| 19 |
data = request.get_json()
|
| 20 |
question = data.get('question', '')
|
| 21 |
history = data.get('history', [])
|
| 22 |
|
| 23 |
+
# Construct the payload for the external API
|
| 24 |
payload = {
|
| 25 |
+
"prompt": {
|
| 26 |
+
"messages": history + [
|
| 27 |
+
{"role": "user", "content": question}
|
| 28 |
+
],
|
| 29 |
+
"model": "gpt-4o",
|
| 30 |
+
"temperature": 0.9,
|
| 31 |
+
"signatureVersion": "Jan-2024",
|
| 32 |
+
},
|
| 33 |
+
"variables": {
|
| 34 |
+
"TASK_INPUT": ""
|
| 35 |
+
},
|
| 36 |
+
"options": {
|
| 37 |
+
"origin": "cards/ai-task",
|
| 38 |
+
},
|
| 39 |
+
"origin": "cards/ai-task",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
}
|
| 41 |
|
| 42 |
+
# Send the POST request to the external API
|
| 43 |
+
response = requests.post(EXTERNAL_API_URL, json=payload, headers=HEADERS)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
# Check the response status
|
| 46 |
+
if response.status_code == 200:
|
| 47 |
+
response_json = response.json()
|
| 48 |
+
assistant_content = response_json.get('choices', [{}])[0].get('message', {}).get('content', '')
|
| 49 |
+
return jsonify({"success": True, "response": assistant_content})
|
| 50 |
+
else:
|
| 51 |
+
return jsonify({"success": False, "error": response.text}), response.status_code
|
| 52 |
|
| 53 |
except Exception as e:
|
| 54 |
+
return jsonify({"success": False, "error": str(e)}), 500
|
| 55 |
|
| 56 |
if __name__ == "__main__":
|
| 57 |
app.run(host="0.0.0.0", port=7860)
|