Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | 
         @@ -1,14 +1,16 @@ 
     | 
|
| 
         | 
|
| 1 | 
         
             
            import json
         
     | 
| 2 | 
         
             
            import logging
         
     | 
| 3 | 
         
             
            from datetime import datetime
         
     | 
| 4 | 
         
             
            from typing import Dict, List, Optional, Any
         
     | 
| 5 | 
         
             
            import gradio as gr
         
     | 
| 6 | 
         
            -
            from openai import  
     | 
| 7 | 
         | 
| 8 | 
         
             
            # Configure logging
         
     | 
| 9 | 
         
             
            logging.basicConfig(level=logging.INFO)
         
     | 
| 10 | 
         
             
            logger = logging.getLogger(__name__)
         
     | 
| 11 | 
         | 
| 
         | 
|
| 12 | 
         
             
            # System prompts
         
     | 
| 13 | 
         
             
            CONVERSATION_PROMPT = """You are LOSS DOG, a professional profile builder. Your goal is to have natural conversations
         
     | 
| 14 | 
         
             
            with users to gather information about their professional background across 9 categories:
         
     | 
| 
         @@ -49,10 +51,10 @@ class ProfileBuilder: 
     | 
|
| 49 | 
         
             
                    self.client = None
         
     | 
| 50 | 
         | 
| 51 | 
         
             
                def _initialize_client(self, api_key: str) -> None:
         
     | 
| 52 | 
         
            -
                    """Initialize  
     | 
| 53 | 
         
             
                    if not api_key.startswith("sk-"):
         
     | 
| 54 | 
         
             
                        raise ValueError("Invalid API key format")
         
     | 
| 55 | 
         
            -
                    self.client =  
     | 
| 56 | 
         | 
| 57 | 
         
             
                async def process_message(self, message: str, api_key: str) -> Dict[str, Any]:
         
     | 
| 58 | 
         
             
                    """Process a user message through conversation phase."""
         
     | 
| 
         @@ -63,8 +65,8 @@ class ProfileBuilder: 
     | 
|
| 63 | 
         
             
                        # Add message to history
         
     | 
| 64 | 
         
             
                        self.conversation_history.append({"role": "user", "content": message})
         
     | 
| 65 | 
         | 
| 66 | 
         
            -
                        # Get AI response  
     | 
| 67 | 
         
            -
                         
     | 
| 68 | 
         
             
                            model="gpt-4o-mini",
         
     | 
| 69 | 
         
             
                            messages=[
         
     | 
| 70 | 
         
             
                                {"role": "system", "content": CONVERSATION_PROMPT},
         
     | 
| 
         @@ -73,7 +75,7 @@ class ProfileBuilder: 
     | 
|
| 73 | 
         
             
                            temperature=0.7
         
     | 
| 74 | 
         
             
                        )
         
     | 
| 75 | 
         | 
| 76 | 
         
            -
                        ai_message =  
     | 
| 77 | 
         
             
                        self.conversation_history.append({"role": "assistant", "content": ai_message})
         
     | 
| 78 | 
         | 
| 79 | 
         
             
                        return {"response": ai_message}
         
     | 
| 
         @@ -94,8 +96,8 @@ class ProfileBuilder: 
     | 
|
| 94 | 
         
             
                            for msg in self.conversation_history
         
     | 
| 95 | 
         
             
                        )
         
     | 
| 96 | 
         | 
| 97 | 
         
            -
                        # Extract structured information  
     | 
| 98 | 
         
            -
                         
     | 
| 99 | 
         
             
                            model="gpt-4o-mini",
         
     | 
| 100 | 
         
             
                            messages=[
         
     | 
| 101 | 
         
             
                                {"role": "system", "content": EXTRACTION_PROMPT},
         
     | 
| 
         @@ -105,7 +107,7 @@ class ProfileBuilder: 
     | 
|
| 105 | 
         
             
                        )
         
     | 
| 106 | 
         | 
| 107 | 
         
             
                        # Parse the structured response
         
     | 
| 108 | 
         
            -
                        profile_data = json.loads( 
     | 
| 109 | 
         | 
| 110 | 
         
             
                        # Add metadata
         
     | 
| 111 | 
         
             
                        profile = {
         
     | 
| 
         @@ -201,4 +203,8 @@ def create_gradio_interface(): 
     | 
|
| 201 | 
         | 
| 202 | 
         
             
            if __name__ == "__main__":
         
     | 
| 203 | 
         
             
                demo = create_gradio_interface()
         
     | 
| 204 | 
         
            -
                demo. 
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
| 
         | 
|
| 1 | 
         
            +
             
     | 
| 2 | 
         
             
            import json
         
     | 
| 3 | 
         
             
            import logging
         
     | 
| 4 | 
         
             
            from datetime import datetime
         
     | 
| 5 | 
         
             
            from typing import Dict, List, Optional, Any
         
     | 
| 6 | 
         
             
            import gradio as gr
         
     | 
| 7 | 
         
            +
            from openai import AsyncOpenAI  # Changed to AsyncOpenAI
         
     | 
| 8 | 
         | 
| 9 | 
         
             
            # Configure logging
         
     | 
| 10 | 
         
             
            logging.basicConfig(level=logging.INFO)
         
     | 
| 11 | 
         
             
            logger = logging.getLogger(__name__)
         
     | 
| 12 | 
         | 
| 13 | 
         
            +
            # System prompts remain the same as before
         
     | 
| 14 | 
         
             
            # System prompts
         
     | 
| 15 | 
         
             
            CONVERSATION_PROMPT = """You are LOSS DOG, a professional profile builder. Your goal is to have natural conversations
         
     | 
| 16 | 
         
             
            with users to gather information about their professional background across 9 categories:
         
     | 
| 
         | 
|
| 51 | 
         
             
                    self.client = None
         
     | 
| 52 | 
         | 
| 53 | 
         
             
                def _initialize_client(self, api_key: str) -> None:
         
     | 
| 54 | 
         
            +
                    """Initialize AsyncOpenAI client with API key."""
         
     | 
| 55 | 
         
             
                    if not api_key.startswith("sk-"):
         
     | 
| 56 | 
         
             
                        raise ValueError("Invalid API key format")
         
     | 
| 57 | 
         
            +
                    self.client = AsyncOpenAI(api_key=api_key)
         
     | 
| 58 | 
         | 
| 59 | 
         
             
                async def process_message(self, message: str, api_key: str) -> Dict[str, Any]:
         
     | 
| 60 | 
         
             
                    """Process a user message through conversation phase."""
         
     | 
| 
         | 
|
| 65 | 
         
             
                        # Add message to history
         
     | 
| 66 | 
         
             
                        self.conversation_history.append({"role": "user", "content": message})
         
     | 
| 67 | 
         | 
| 68 | 
         
            +
                        # Get AI response - properly awaited
         
     | 
| 69 | 
         
            +
                        completion = await self.client.chat.completions.create(
         
     | 
| 70 | 
         
             
                            model="gpt-4o-mini",
         
     | 
| 71 | 
         
             
                            messages=[
         
     | 
| 72 | 
         
             
                                {"role": "system", "content": CONVERSATION_PROMPT},
         
     | 
| 
         | 
|
| 75 | 
         
             
                            temperature=0.7
         
     | 
| 76 | 
         
             
                        )
         
     | 
| 77 | 
         | 
| 78 | 
         
            +
                        ai_message = completion.choices[0].message.content
         
     | 
| 79 | 
         
             
                        self.conversation_history.append({"role": "assistant", "content": ai_message})
         
     | 
| 80 | 
         | 
| 81 | 
         
             
                        return {"response": ai_message}
         
     | 
| 
         | 
|
| 96 | 
         
             
                            for msg in self.conversation_history
         
     | 
| 97 | 
         
             
                        )
         
     | 
| 98 | 
         | 
| 99 | 
         
            +
                        # Extract structured information - properly awaited
         
     | 
| 100 | 
         
            +
                        completion = await self.client.chat.completions.create(
         
     | 
| 101 | 
         
             
                            model="gpt-4o-mini",
         
     | 
| 102 | 
         
             
                            messages=[
         
     | 
| 103 | 
         
             
                                {"role": "system", "content": EXTRACTION_PROMPT},
         
     | 
| 
         | 
|
| 107 | 
         
             
                        )
         
     | 
| 108 | 
         | 
| 109 | 
         
             
                        # Parse the structured response
         
     | 
| 110 | 
         
            +
                        profile_data = json.loads(completion.choices[0].message.content)
         
     | 
| 111 | 
         | 
| 112 | 
         
             
                        # Add metadata
         
     | 
| 113 | 
         
             
                        profile = {
         
     | 
| 
         | 
|
| 203 | 
         | 
| 204 | 
         
             
            if __name__ == "__main__":
         
     | 
| 205 | 
         
             
                demo = create_gradio_interface()
         
     | 
| 206 | 
         
            +
                demo.queue()  # Add queue for async support
         
     | 
| 207 | 
         
            +
                demo.launch(
         
     | 
| 208 | 
         
            +
                    server_name="0.0.0.0",
         
     | 
| 209 | 
         
            +
                    server_port=7860
         
     | 
| 210 | 
         
            +
                )
         
     |