Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,16 @@
|
|
| 1 |
import requests
|
| 2 |
from flask import Flask, request, jsonify
|
| 3 |
from datetime import datetime
|
|
|
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
|
| 7 |
# URLs
|
| 8 |
SEPTIMIUS_JSON_URL = "https://corvo-ai-septimius.hf.space/read_Septimius_json"
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
def is_token_valid(token):
|
| 12 |
"""
|
|
@@ -38,6 +42,48 @@ def is_token_valid(token):
|
|
| 38 |
return False
|
| 39 |
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
@app.route('/chat', methods=['POST'])
|
| 42 |
def chat():
|
| 43 |
try:
|
|
@@ -55,21 +101,9 @@ def chat():
|
|
| 55 |
if not is_token_valid(token):
|
| 56 |
return jsonify({"error": "You must subscribe to continue."}), 403
|
| 57 |
|
| 58 |
-
#
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
if response.status_code == 200:
|
| 63 |
-
response_json = response.json()
|
| 64 |
-
ai_response = response_json.get("response", "No 'response' key found in the JSON.")
|
| 65 |
-
ai_response = ai_response.encode('latin1').decode('utf-8', 'ignore') # Handle encoding issues
|
| 66 |
-
return jsonify({"response": ai_response})
|
| 67 |
-
else:
|
| 68 |
-
# Handle errors from PythonAnywhere
|
| 69 |
-
return jsonify({
|
| 70 |
-
"error": "Error from PythonAnywhere",
|
| 71 |
-
"details": response.text
|
| 72 |
-
}), response.status_code
|
| 73 |
|
| 74 |
except Exception as e:
|
| 75 |
# Catch any unexpected errors
|
|
|
|
| 1 |
import requests
|
| 2 |
from flask import Flask, request, jsonify
|
| 3 |
from datetime import datetime
|
| 4 |
+
import json
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
| 8 |
# URLs
|
| 9 |
SEPTIMIUS_JSON_URL = "https://corvo-ai-septimius.hf.space/read_Septimius_json"
|
| 10 |
+
|
| 11 |
+
# Brain Inc API Details
|
| 12 |
+
BRAININC_API_URL = "https://api.braininc.net/stream/bas-demo-v4/nlp/completions_generation"
|
| 13 |
+
BRAININC_API_TOKEN = "token 72ec00483379076f580eb8126f29da802a5140c3" # Replace if needed
|
| 14 |
|
| 15 |
def is_token_valid(token):
|
| 16 |
"""
|
|
|
|
| 42 |
return False
|
| 43 |
|
| 44 |
|
| 45 |
+
def generate_ai_response(message):
|
| 46 |
+
"""
|
| 47 |
+
Generate AI response using Brain Inc API.
|
| 48 |
+
"""
|
| 49 |
+
headers = {
|
| 50 |
+
"Authorization": f"{BRAININC_API_TOKEN}",
|
| 51 |
+
"Content-Type": "application/json;charset=UTF-8",
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
payload = {
|
| 55 |
+
"identifier_type": "object_id",
|
| 56 |
+
"identifier_value": "4775260",
|
| 57 |
+
"pubsub_topic": "/studios/516104/wsuid_4647155_new-edge-112_nodeid_editor-111/textGenStream/1722445975926",
|
| 58 |
+
"stream": True,
|
| 59 |
+
"variables": {
|
| 60 |
+
"Input": message,
|
| 61 |
+
"User Input": message,
|
| 62 |
+
"input": message,
|
| 63 |
+
}
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
try:
|
| 67 |
+
response = requests.post(BRAININC_API_URL, headers=headers, json=payload)
|
| 68 |
+
combined_content = ""
|
| 69 |
+
|
| 70 |
+
for line in response.text.splitlines():
|
| 71 |
+
if line.startswith('data: '):
|
| 72 |
+
json_data = line[6:].strip()
|
| 73 |
+
try:
|
| 74 |
+
data = json.loads(json_data)
|
| 75 |
+
delta_content = data.get('choices')[0].get('delta').get('content', '')
|
| 76 |
+
combined_content += delta_content
|
| 77 |
+
except json.JSONDecodeError:
|
| 78 |
+
continue
|
| 79 |
+
|
| 80 |
+
combined_content = combined_content.replace("\n", "<br>")
|
| 81 |
+
return combined_content
|
| 82 |
+
except requests.exceptions.RequestException as e:
|
| 83 |
+
print(f"Error connecting to Brain Inc API: {e}")
|
| 84 |
+
return "Error generating response"
|
| 85 |
+
|
| 86 |
+
|
| 87 |
@app.route('/chat', methods=['POST'])
|
| 88 |
def chat():
|
| 89 |
try:
|
|
|
|
| 101 |
if not is_token_valid(token):
|
| 102 |
return jsonify({"error": "You must subscribe to continue."}), 403
|
| 103 |
|
| 104 |
+
# Generate AI response using Brain Inc API
|
| 105 |
+
ai_response = generate_ai_response(message)
|
| 106 |
+
return jsonify({"response": ai_response})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
except Exception as e:
|
| 109 |
# Catch any unexpected errors
|