# app.py import hashlib import json from fastapi import FastAPI from pydantic import BaseModel from Crypto.Cipher import AES from transformers import AutoModelForCausalLM, AutoTokenizer import requests # FastAPI app initialization app = FastAPI() # Load GPT model and tokenizer model_name = "EleutherAI/gpt-neo-2.7B" # Change to any compatible HuggingFace model model = AutoModelForCausalLM.from_pretrained(model_name) tokenizer = AutoTokenizer.from_pretrained(model_name) # Encryption/Decryption Functions (AES-GCM) def encrypt(data: str, key: str) -> str: """Encrypt input data using AES encryption.""" key_bytes = hashlib.sha256(key.encode()).digest() cipher = AES.new(key_bytes, AES.MODE_GCM) ciphertext, tag = cipher.encrypt_and_digest(data.encode()) return cipher.nonce.hex() + tag.hex() + ciphertext.hex() def decrypt(encrypted_data: str, key: str) -> str: """Decrypt encrypted data using AES encryption.""" nonce, tag, ciphertext = bytes.fromhex(encrypted_data[:32]), bytes.fromhex(encrypted_data[32:64]), bytes.fromhex(encrypted_data[64:]) key_bytes = hashlib.sha256(key.encode()).digest() cipher = AES.new(key_bytes, AES.MODE_GCM, nonce=nonce) return cipher.decrypt_and_verify(ciphertext, tag).decode() # AI Response Generation Function def generate_response(prompt: str) -> str: """Generate a response from the AI model (GPT-NeoX).""" inputs = tokenizer(prompt, return_tensors="pt") outputs = model.generate(inputs['input_ids'], max_length=200) return tokenizer.decode(outputs[0], skip_special_tokens=True) # Pydantic model to handle task input class Task(BaseModel): task_description: str encryption_key: str # FastAPI route to generate AI response for a task @app.post("/generate_response/") def generate_response_for_task(task: Task): encrypted_prompt = task.task_description decrypted_prompt = decrypt(encrypted_prompt, task.encryption_key) response = generate_response(decrypted_prompt) encrypted_response = encrypt(response, task.encryption_key) return {"encrypted_response": encrypted_response} # FastAPI route to add a new task (example placeholder) @app.post("/add_task/") def add_task(task: Task): return {"message": f"Task '{task.task_description}' added successfully."} # CLI for interacting with the API def send_task_to_server(task_description, encryption_key): url = "http://127.0.0.1:8000/generate_response/" encrypted_prompt = encrypt(task_description, encryption_key) payload = json.dumps({ "task_description": encrypted_prompt, "encryption_key": encryption_key }) headers = {'Content-Type': 'application/json'} response = requests.post(url, headers=headers, data=payload) return response.json() # CLI Main function def main(): task_description = input("Enter your task description: ") encryption_key = input("Enter your encryption key: ") response = send_task_to_server(task_description, encryption_key) decrypted_response = decrypt(response["encrypted_response"], encryption_key) print("Assistant's Response:") print(decrypted_response) if __name__ == "__main__": # Run FastAPI app or CLI tool import sys if len(sys.argv) > 1 and sys.argv[1] == "cli": main() # Run the CLI tool if "cli" is passed as an argument else: import uvicorn uvicorn.run(app, host="127.0.0.1", port=8000) # Start the FastAPI server