| import os |
| import logging |
| import requests |
| import json |
| from flask import Flask, request, jsonify, Response, stream_with_context, send_file |
| from flask_cors import CORS |
| from openai import OpenAI |
|
|
| |
| |
| |
| logging.basicConfig( |
| level=logging.INFO, |
| format='%(asctime)s - %(levelname)s - [Vedika Space] - %(message)s' |
| ) |
| logger = logging.getLogger(__name__) |
|
|
| app = Flask(__name__) |
| CORS(app, resources={r"/*": {"origins": "*"}}) |
|
|
| |
| |
| |
| API_KEY = os.getenv("API_KEY") |
| MODEL_ID = os.getenv("MODEL_ID") |
| GAS_BACKEND_URL = os.getenv("GAS_BACKEND_URL") |
|
|
| |
| client = OpenAI( |
| base_url="https://integrate.api.nvidia.com/v1", |
| api_key=API_KEY |
| ) |
|
|
| |
| |
| |
| def validate_token_with_gas(token): |
| if not token: |
| return False |
| try: |
| response = requests.post(GAS_BACKEND_URL, json={"action": "validate", "token": token}, timeout=5) |
| if response.status_code == 200: |
| return response.json().get("valid") == True |
| return False |
| except requests.exceptions.RequestException as e: |
| logger.error(f"GAS validation error: {e}") |
| return False |
|
|
| |
| |
| |
| @app.route('/') |
| def index(): |
| return send_file('index.html') |
|
|
| |
| |
| |
| @app.route('/chat', methods=['POST', 'OPTIONS']) |
| def chat_endpoint(): |
| |
| if request.method == 'OPTIONS': |
| return Response(status=204) |
|
|
| |
| auth_header = request.headers.get("Authorization") |
| if not auth_header or not auth_header.startswith("Bearer "): |
| return jsonify({"error": "Unauthorized: Missing Token"}), 401 |
|
|
| token = auth_header.split("Bearer ")[1].strip() |
|
|
| if not validate_token_with_gas(token): |
| return jsonify({"error": "Forbidden: Invalid Token"}), 403 |
|
|
| try: |
| data = request.json |
| mode = data.get('mode', 'FLASH') |
| messages = data.get('messages', []) |
| if not messages: |
| return jsonify({"error": "Messages empty"}), 400 |
| except Exception: |
| return jsonify({"error": "Invalid JSON"}), 400 |
|
|
| |
| system_prompt = os.getenv(mode.upper()) |
| if not system_prompt: |
| return jsonify({"error": f"Mode {mode} missing in Secrets."}), 500 |
|
|
| messages.insert(0, {"role": "system", "content": system_prompt}) |
|
|
| |
| def generate_stream(): |
| try: |
| |
| completion = client.chat.completions.create( |
| model=MODEL_ID, |
| messages=messages, |
| temperature=0.7, |
| top_p=0.8, |
| max_tokens=1024, |
| stream=True |
| ) |
| |
| for chunk in completion: |
| if chunk.choices and chunk.choices[0].delta.content is not None: |
| |
| |
| chunk_data = chunk.model_dump_json() |
| yield f"data: {chunk_data}\n\n" |
| |
| except Exception as e: |
| logger.error(f"OpenAI API Error: {str(e)}") |
| yield f"data: {{\"error\": \"AI Provider Error: {str(e)}\"}}\n\n" |
|
|
| return Response(stream_with_context(generate_stream()), mimetype='text/event-stream') |
|
|
| if __name__ == '__main__': |
| app.run(host='0.0.0.0', port=7860, debug=False) |
|
|