Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -2,10 +2,7 @@ import os | |
| 2 | 
             
            import sys
         | 
| 3 | 
             
            import asyncio
         | 
| 4 | 
             
            import logging
         | 
| 5 | 
            -
             | 
| 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
         | 
| @@ -21,9 +18,7 @@ class EnvironmentManager: | |
| 21 | 
             
                @staticmethod
         | 
| 22 | 
             
                def load_env_variables() -> Dict[str, str]:
         | 
| 23 | 
             
                    required_vars = [
         | 
| 24 | 
            -
                        " | 
| 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}
         | 
| @@ -34,98 +29,37 @@ class EnvironmentManager: | |
| 34 |  | 
| 35 | 
             
                    return env_vars
         | 
| 36 |  | 
| 37 | 
            -
            class  | 
| 38 | 
            -
                """ | 
| 39 | 
            -
                def __init__(self,  | 
| 40 | 
            -
                    self. | 
| 41 | 
            -
                    self.ml_client = None
         | 
| 42 | 
            -
                    self.openai_client = None
         | 
| 43 |  | 
| 44 | 
            -
                def  | 
| 45 | 
            -
                     | 
| 46 | 
            -
             | 
| 47 | 
            -
             | 
| 48 | 
            -
             | 
| 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  | 
| 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. | 
| 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 | 
            -
                         | 
| 91 | 
            -
                            " | 
| 92 | 
            -
                             | 
| 93 | 
            -
             | 
| 94 | 
            -
             | 
| 95 | 
            -
             | 
| 96 | 
            -
             | 
| 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 | 
            -
             | 
| 126 | 
            -
                    logging.error(f"Application initialization failed: {e}")
         | 
| 127 | 
            -
                    sys.exit(1)
         | 
| 128 |  | 
| 129 | 
            -
             | 
| 130 | 
            -
                main()
         | 
| 131 | 
            -
                
         | 
|  | |
| 2 | 
             
            import sys
         | 
| 3 | 
             
            import asyncio
         | 
| 4 | 
             
            import logging
         | 
| 5 | 
            +
            import openai  # Correct OpenAI import
         | 
|  | |
|  | |
|  | |
| 6 | 
             
            from transformers import AutoTokenizer
         | 
| 7 | 
             
            from typing import Dict, Any, List
         | 
| 8 | 
             
            import aiohttp
         | 
|  | |
| 18 | 
             
                @staticmethod
         | 
| 19 | 
             
                def load_env_variables() -> Dict[str, str]:
         | 
| 20 | 
             
                    required_vars = [
         | 
| 21 | 
            +
                        "OPENAI_API_KEY", "ENCRYPTION_KEY"
         | 
|  | |
|  | |
| 22 | 
             
                    ]
         | 
| 23 |  | 
| 24 | 
             
                    env_vars = {var: os.getenv(var) for var in required_vars}
         | 
|  | |
| 29 |  | 
| 30 | 
             
                    return env_vars
         | 
| 31 |  | 
| 32 | 
            +
            class EncryptionManager:
         | 
| 33 | 
            +
                """Handles encryption and decryption of sensitive data."""
         | 
| 34 | 
            +
                def __init__(self, key: str):
         | 
| 35 | 
            +
                    self.cipher = Fernet(key.encode())
         | 
|  | |
|  | |
| 36 |  | 
| 37 | 
            +
                def encrypt(self, data: str) -> str:
         | 
| 38 | 
            +
                    return self.cipher.encrypt(data.encode()).decode()
         | 
| 39 | 
            +
             | 
| 40 | 
            +
                def decrypt(self, encrypted_data: str) -> str:
         | 
| 41 | 
            +
                    return self.cipher.decrypt(encrypted_data.encode()).decode()
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 42 |  | 
| 43 | 
             
            class AICore:
         | 
| 44 | 
            +
                """Main AI Core system integrating OpenAI chat functionality."""
         | 
| 45 | 
             
                def __init__(self, env_vars: Dict[str, str]):
         | 
| 46 | 
             
                    self.env_vars = env_vars
         | 
| 47 | 
             
                    self.encryption_manager = EncryptionManager(env_vars["ENCRYPTION_KEY"])
         | 
| 48 | 
            +
                    self.openai_api_key = env_vars["OPENAI_API_KEY"]
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 49 |  | 
| 50 | 
             
                async def generate_response(self, query: str) -> Dict[str, Any]:
         | 
| 51 | 
             
                    try:
         | 
| 52 | 
             
                        encrypted_query = self.encryption_manager.encrypt(query)
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
| 53 |  | 
| 54 | 
            +
                        chat_completion = await openai.ChatCompletion.acreate(
         | 
| 55 | 
            +
                            model="gpt-4-turbo",  # Ensure you use a valid model name
         | 
| 56 | 
            +
                            messages=[
         | 
| 57 | 
            +
                                {"role": "system", "content": "You are a helpful AI assistant."},
         | 
| 58 | 
            +
                                {"role": "user", "content": query}
         | 
| 59 | 
            +
                            ],
         | 
| 60 | 
            +
                            api_key=self.openai_api_key
         | 
| 61 | 
            +
                        )
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 62 |  | 
| 63 | 
            +
                        model_response = chat_completion['choices'][0]['message']['content']
         | 
|  | |
|  | |
| 64 |  | 
| 65 | 
            +
                        return
         | 
|  | |
|  | 
