truesyncai / app.py
sujalrajpoot's picture
Update app.py
d7f4cf1 verified
raw
history blame
3.19 kB
from flask import Flask, request, jsonify
import hmac, hashlib, secrets, time
import requests
app = Flask(__name__)
# 🔑 Secret key for API authentication (Load from environment in production)
SECRET_KEY = "MySuperSecretKey_TrueSyncAI"
# Store API keys temporarily (use a database for real applications)
API_KEYS = {}
# Generate API Key
@app.route("/generate_api_key", methods=["POST"])
def generate_api_key():
random_part = secrets.token_hex(16)
signature = hmac.new(SECRET_KEY.encode(), random_part.encode(), hashlib.sha256).hexdigest()[:16]
api_key = f"TrueSyncAI-{random_part}-{signature}"
# Store the key with a timestamp
API_KEYS[api_key] = time.time()
return jsonify({"api_key": api_key})
# Validate API Key
def validate_api_key(api_key):
parts = api_key.split("-")
if len(parts) != 3 or parts[0] != "TrueSyncAI":
return False
random_part, received_signature = parts[1], parts[2]
expected_signature = hmac.new(SECRET_KEY.encode(), random_part.encode(), hashlib.sha256).hexdigest()[:16]
return expected_signature == received_signature and api_key in API_KEYS
def generate_response(query:str) -> str:
json_data = {'model': 'meta-llama/Llama-3.3-70B-Instruct-Turbo', 'messages': [{'role': 'system', 'content': 'You are TrueSyncAI, an advanced AI assistant developed by TrueSyncAI, a pioneering AI startup founded by Sujal Rajpoot and Anuj Rajpoot. The mission of TrueSyncAI is to bring virtual versions of individuals into the real world, redefining human-AI interaction. \n\nAs TrueSyncAI, you are designed to be intelligent, engaging, and helpful in conversations. You should provide insightful, accurate, and context-aware responses while maintaining a friendly and professional tone. Your goal is to enhance the user’s experience by adapting to their needs, assisting with various tasks, and learning from interactions to improve over time. \n\nAlways ensure clarity, relevance, and accuracy in your responses, and align with TrueSyncAI’s vision of bridging the gap between virtual intelligence and reality.'}, {'role': 'user', 'content': query}], 'stream': False, 'max_tokens': 2048, 'temperature': 0.7, 'top_p': 0.7}
response = requests.post('https://api.deepinfra.com/v1/openai/chat/completions',
json=json_data, timeout=None)
if response.status_code==200:
return response.json()['choices'][0]['message']['content']
else:return "API Server is under maintenance. Please Try After Some Time Thank You for using TrueSyncAI Chat API. Have a great day."
# Chat Endpoint
@app.route("/chat", methods=["POST"])
def chat():
data = request.json
api_key = data.get("api_key")
message = data.get("message", "").strip()
if not api_key or not validate_api_key(api_key):
return jsonify({"error": "Invalid API Key"}), 401
if not message:
return jsonify({"error": "Message cannot be empty"}), 400
# Basic AI response (Can integrate LLMs here)
response = generate_response(message)
return jsonify({"response": response})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860) # Hugging Face Spaces default port