Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
import asyncio
|
| 4 |
+
import logging
|
| 5 |
+
from azure.identity import ClientSecretCredential
|
| 6 |
+
from azure.ai.ml import MLClient
|
| 7 |
+
from azure.ai.ml.entities import OnlineEndpoint, OnlineDeployment
|
| 8 |
+
from azure.ai.openai import AzureOpenAIClient, ChatClient, SystemChatMessage, UserChatMessage, AssistantChatMessage
|
| 9 |
+
from transformers import AutoTokenizer
|
| 10 |
+
from typing import Dict, Any, List
|
| 11 |
+
import aiohttp
|
| 12 |
+
from cryptography.fernet import Fernet
|
| 13 |
+
import gradio as gr
|
| 14 |
+
import json
|
| 15 |
+
import torch
|
| 16 |
+
import psutil
|
| 17 |
+
import random
|
| 18 |
+
|
| 19 |
+
class EnvironmentManager:
|
| 20 |
+
"""Handles loading and validation of environment variables."""
|
| 21 |
+
@staticmethod
|
| 22 |
+
def load_env_variables() -> Dict[str, str]:
|
| 23 |
+
required_vars = [
|
| 24 |
+
"AZURE_CLIENT_ID", "AZURE_CLIENT_SECRET", "AZURE_TENANT_ID",
|
| 25 |
+
"AZURE_SUBSCRIPTION_ID", "AZURE_RESOURCE_GROUP", "AZURE_WORKSPACE_NAME",
|
| 26 |
+
"AZURE_MODEL_ID", "ENCRYPTION_KEY", "AZURE_OPENAI_ENDPOINT"
|
| 27 |
+
]
|
| 28 |
+
|
| 29 |
+
env_vars = {var: os.getenv(var) for var in required_vars}
|
| 30 |
+
missing_vars = [var for var, value in env_vars.items() if not value]
|
| 31 |
+
|
| 32 |
+
if missing_vars:
|
| 33 |
+
raise ValueError(f"Missing required environment variables: {', '.join(missing_vars)}")
|
| 34 |
+
|
| 35 |
+
return env_vars
|
| 36 |
+
|
| 37 |
+
class AzureClient:
|
| 38 |
+
"""Sets up the Azure ML and OpenAI Clients with required credentials."""
|
| 39 |
+
def __init__(self, env_vars: Dict[str, str]):
|
| 40 |
+
self.env_vars = env_vars
|
| 41 |
+
self.ml_client = None
|
| 42 |
+
self.openai_client = None
|
| 43 |
+
|
| 44 |
+
def authenticate(self):
|
| 45 |
+
try:
|
| 46 |
+
credential = ClientSecretCredential(
|
| 47 |
+
client_id=self.env_vars["AZURE_CLIENT_ID"],
|
| 48 |
+
client_secret=self.env_vars["AZURE_CLIENT_SECRET"],
|
| 49 |
+
tenant_id=self.env_vars["AZURE_TENANT_ID"]
|
| 50 |
+
)
|
| 51 |
+
self.ml_client = MLClient(
|
| 52 |
+
credential,
|
| 53 |
+
subscription_id=self.env_vars["AZURE_SUBSCRIPTION_ID"],
|
| 54 |
+
resource_group=self.env_vars["AZURE_RESOURCE_GROUP"],
|
| 55 |
+
workspace_name=self.env_vars["AZURE_WORKSPACE_NAME"]
|
| 56 |
+
)
|
| 57 |
+
self.openai_client = AzureOpenAIClient(
|
| 58 |
+
endpoint=self.env_vars["AZURE_OPENAI_ENDPOINT"],
|
| 59 |
+
credential=credential
|
| 60 |
+
)
|
| 61 |
+
logging.info("Azure authentication successful.")
|
| 62 |
+
except Exception as e:
|
| 63 |
+
logging.error(f"Azure authentication failed: {e}")
|
| 64 |
+
sys.exit(1)
|
| 65 |
+
|
| 66 |
+
class AICore:
|
| 67 |
+
"""Main AI Core system integrating Azure OpenAI chat functionality."""
|
| 68 |
+
def __init__(self, env_vars: Dict[str, str]):
|
| 69 |
+
self.env_vars = env_vars
|
| 70 |
+
self.encryption_manager = EncryptionManager(env_vars["ENCRYPTION_KEY"])
|
| 71 |
+
self.openai_client = AzureOpenAIClient(
|
| 72 |
+
endpoint=self.env_vars["AZURE_OPENAI_ENDPOINT"],
|
| 73 |
+
credential=ClientSecretCredential(
|
| 74 |
+
client_id=self.env_vars["AZURE_CLIENT_ID"],
|
| 75 |
+
client_secret=self.env_vars["AZURE_CLIENT_SECRET"],
|
| 76 |
+
tenant_id=self.env_vars["AZURE_TENANT_ID"]
|
| 77 |
+
)
|
| 78 |
+
)
|
| 79 |
+
self.chat_client = self.openai_client.get_chat_client(self.env_vars["AZURE_MODEL_ID"])
|
| 80 |
+
|
| 81 |
+
async def generate_response(self, query: str) -> Dict[str, Any]:
|
| 82 |
+
try:
|
| 83 |
+
encrypted_query = self.encryption_manager.encrypt(query)
|
| 84 |
+
chat_completion = self.chat_client.complete_chat([
|
| 85 |
+
SystemChatMessage("You are a helpful AI assistant."),
|
| 86 |
+
UserChatMessage(query)
|
| 87 |
+
])
|
| 88 |
+
model_response = chat_completion.content[0].text
|
| 89 |
+
|
| 90 |
+
return {
|
| 91 |
+
"encrypted_query": encrypted_query,
|
| 92 |
+
"model_response": model_response
|
| 93 |
+
}
|
| 94 |
+
except Exception as e:
|
| 95 |
+
logging.error(f"Error during response generation: {e}")
|
| 96 |
+
return {"error": "Failed to generate response"}
|
| 97 |
+
|
| 98 |
+
# Main Application
|
| 99 |
+
def main():
|
| 100 |
+
logging.basicConfig(level=logging.INFO)
|
| 101 |
+
try:
|
| 102 |
+
env_vars = EnvironmentManager.load_env_variables()
|
| 103 |
+
|
| 104 |
+
azure_client = AzureClient(env_vars)
|
| 105 |
+
azure_client.authenticate()
|
| 106 |
+
|
| 107 |
+
ai_core = AICore(env_vars)
|
| 108 |
+
|
| 109 |
+
# Example Gradio interface
|
| 110 |
+
async def async_respond(message: str) -> str:
|
| 111 |
+
response_data = await ai_core.generate_response(message)
|
| 112 |
+
return response_data.get("model_response", "Error: Response not available")
|
| 113 |
+
|
| 114 |
+
def respond(message: str) -> str:
|
| 115 |
+
return asyncio.run(async_respond(message))
|
| 116 |
+
|
| 117 |
+
interface = gr.Interface(
|
| 118 |
+
fn=respond,
|
| 119 |
+
inputs="text",
|
| 120 |
+
outputs="text",
|
| 121 |
+
title="Advanced AI Chat Interface"
|
| 122 |
+
)
|
| 123 |
+
interface.launch(share=True)
|
| 124 |
+
|
| 125 |
+
except Exception as e:
|
| 126 |
+
logging.error(f"Application initialization failed: {e}")
|
| 127 |
+
sys.exit(1)
|
| 128 |
+
|
| 129 |
+
if __name__ == "__main__":
|
| 130 |
+
main()
|