Spaces:
Paused
Paused
| from flask import Flask, request, jsonify | |
| import requests | |
| app = Flask(__name__) | |
| # Define the URL and headers for the external API | |
| EXTERNAL_API_URL = "https://api.botpress.cloud/v1/cognitive/chat-gpt/query" | |
| HEADERS = { | |
| "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", | |
| "x-bot-id": "fbe047bc-06fb-44c1-9672-342fe1a2e506", | |
| "Content-Type": "application/json", | |
| "Cookie": "YOUR_COOKIES_HERE", # Replace with actual cookie value | |
| } | |
| def query(): | |
| try: | |
| # Get the JSON payload from the request | |
| data = request.get_json() | |
| question = data.get('question', '') | |
| history = data.get('history', []) | |
| # Construct the payload for the external API | |
| payload = { | |
| "prompt": { | |
| "messages": history + [ | |
| {"role": "user", "content": question} | |
| ], | |
| "model": "gpt-4o", | |
| "temperature": 0.9, | |
| "signatureVersion": "Jan-2024", | |
| }, | |
| "variables": { | |
| "TASK_INPUT": "" | |
| }, | |
| "options": { | |
| "origin": "cards/ai-task", | |
| }, | |
| "origin": "cards/ai-task", | |
| } | |
| # Send the POST request to the external API | |
| response = requests.post(EXTERNAL_API_URL, json=payload, headers=HEADERS) | |
| # Check the response status | |
| if response.status_code == 200: | |
| response_json = response.json() | |
| assistant_content = response_json.get('choices', [{}])[0].get('message', {}).get('content', '') | |
| return jsonify({"success": True, "response": assistant_content}) | |
| else: | |
| return jsonify({"success": False, "error": response.text}), response.status_code | |
| except Exception as e: | |
| return jsonify({"success": False, "error": str(e)}), 500 | |
| if __name__ == "__main__": | |
| app.run(host="0.0.0.0", port=7860) |