Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ import logging
|
|
| 3 |
from datetime import datetime
|
| 4 |
from typing import Dict, List, Optional, Any
|
| 5 |
import gradio as gr
|
| 6 |
-
import
|
| 7 |
|
| 8 |
# Configure logging
|
| 9 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -43,24 +43,28 @@ structured information into the following categories:
|
|
| 43 |
|
| 44 |
Format the output as clean, structured JSON. Include confidence scores for each extracted piece of information.
|
| 45 |
Mark any inferred information clearly."""
|
| 46 |
-
|
| 47 |
class ProfileBuilder:
|
| 48 |
def __init__(self):
|
| 49 |
self.conversation_history = []
|
| 50 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
async def process_message(self, message: str, api_key: str) -> Dict[str, Any]:
|
| 53 |
"""Process a user message through conversation phase."""
|
| 54 |
try:
|
| 55 |
-
if not self.
|
| 56 |
-
self.api_key
|
| 57 |
-
openai.api_key = api_key
|
| 58 |
|
| 59 |
# Add message to history
|
| 60 |
self.conversation_history.append({"role": "user", "content": message})
|
| 61 |
|
| 62 |
-
# Get AI response
|
| 63 |
-
response = await
|
| 64 |
model="gpt-4o-mini",
|
| 65 |
messages=[
|
| 66 |
{"role": "system", "content": CONVERSATION_PROMPT},
|
|
@@ -81,14 +85,17 @@ class ProfileBuilder:
|
|
| 81 |
async def generate_profile(self) -> Dict[str, Any]:
|
| 82 |
"""Process conversation history into structured profile."""
|
| 83 |
try:
|
|
|
|
|
|
|
|
|
|
| 84 |
# Convert conversation history to text
|
| 85 |
conversation_text = "\n".join(
|
| 86 |
f"{msg['role']}: {msg['content']}"
|
| 87 |
for msg in self.conversation_history
|
| 88 |
)
|
| 89 |
|
| 90 |
-
# Extract structured information
|
| 91 |
-
response = await
|
| 92 |
model="gpt-4o-mini",
|
| 93 |
messages=[
|
| 94 |
{"role": "system", "content": EXTRACTION_PROMPT},
|
|
@@ -194,4 +201,4 @@ def create_gradio_interface():
|
|
| 194 |
|
| 195 |
if __name__ == "__main__":
|
| 196 |
demo = create_gradio_interface()
|
| 197 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 3 |
from datetime import datetime
|
| 4 |
from typing import Dict, List, Optional, Any
|
| 5 |
import gradio as gr
|
| 6 |
+
from openai import OpenAI # Updated import
|
| 7 |
|
| 8 |
# Configure logging
|
| 9 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 43 |
|
| 44 |
Format the output as clean, structured JSON. Include confidence scores for each extracted piece of information.
|
| 45 |
Mark any inferred information clearly."""
|
|
|
|
| 46 |
class ProfileBuilder:
|
| 47 |
def __init__(self):
|
| 48 |
self.conversation_history = []
|
| 49 |
+
self.client = None
|
| 50 |
+
|
| 51 |
+
def _initialize_client(self, api_key: str) -> None:
|
| 52 |
+
"""Initialize OpenAI client with API key."""
|
| 53 |
+
if not api_key.startswith("sk-"):
|
| 54 |
+
raise ValueError("Invalid API key format")
|
| 55 |
+
self.client = OpenAI(api_key=api_key)
|
| 56 |
|
| 57 |
async def process_message(self, message: str, api_key: str) -> Dict[str, Any]:
|
| 58 |
"""Process a user message through conversation phase."""
|
| 59 |
try:
|
| 60 |
+
if not self.client:
|
| 61 |
+
self._initialize_client(api_key)
|
|
|
|
| 62 |
|
| 63 |
# Add message to history
|
| 64 |
self.conversation_history.append({"role": "user", "content": message})
|
| 65 |
|
| 66 |
+
# Get AI response using new client format
|
| 67 |
+
response = await self.client.chat.completions.create(
|
| 68 |
model="gpt-4o-mini",
|
| 69 |
messages=[
|
| 70 |
{"role": "system", "content": CONVERSATION_PROMPT},
|
|
|
|
| 85 |
async def generate_profile(self) -> Dict[str, Any]:
|
| 86 |
"""Process conversation history into structured profile."""
|
| 87 |
try:
|
| 88 |
+
if not self.client:
|
| 89 |
+
raise ValueError("OpenAI client not initialized")
|
| 90 |
+
|
| 91 |
# Convert conversation history to text
|
| 92 |
conversation_text = "\n".join(
|
| 93 |
f"{msg['role']}: {msg['content']}"
|
| 94 |
for msg in self.conversation_history
|
| 95 |
)
|
| 96 |
|
| 97 |
+
# Extract structured information using new client format
|
| 98 |
+
response = await self.client.chat.completions.create(
|
| 99 |
model="gpt-4o-mini",
|
| 100 |
messages=[
|
| 101 |
{"role": "system", "content": EXTRACTION_PROMPT},
|
|
|
|
| 201 |
|
| 202 |
if __name__ == "__main__":
|
| 203 |
demo = create_gradio_interface()
|
| 204 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|