Update handler.py
Browse files- handler.py +11 -18
handler.py
CHANGED
|
@@ -3,21 +3,6 @@ import openai
|
|
| 3 |
from environs import Env
|
| 4 |
from typing import List, Dict, Any
|
| 5 |
|
| 6 |
-
|
| 7 |
-
import requests
|
| 8 |
-
|
| 9 |
-
def download_env_file(url: str, local_path: str):
|
| 10 |
-
response = requests.get(url)
|
| 11 |
-
response.raise_for_status() # Ensure we notice bad responses
|
| 12 |
-
with open(local_path, 'wb') as f:
|
| 13 |
-
f.write(response.content)
|
| 14 |
-
|
| 15 |
-
# Download the .env file
|
| 16 |
-
env_file_url = "https://www.dropbox.com/scl/fi/21ldek2cdsak2v3mhyy5x/openai.env?rlkey=nxdkd8l8esdy8npa3vfgvqkhp&st=s2f2zzwl&dl=1" # Adjusted URL for direct download
|
| 17 |
-
local_env_path = "openai.env"
|
| 18 |
-
download_env_file(env_file_url, local_env_path)
|
| 19 |
-
|
| 20 |
-
|
| 21 |
# Load environment variables
|
| 22 |
env = Env()
|
| 23 |
env.read_env("openai.env")
|
|
@@ -28,7 +13,7 @@ MODEL = env.str("MODEL", "gpt-3.5-turbo")
|
|
| 28 |
AI_RESPONSE_TIMEOUT = env.int("AI_RESPONSE_TIMEOUT", 20)
|
| 29 |
|
| 30 |
class EndpointHandler:
|
| 31 |
-
def __init__(self
|
| 32 |
pass
|
| 33 |
|
| 34 |
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
|
@@ -40,8 +25,6 @@ class EndpointHandler:
|
|
| 40 |
except Exception as e:
|
| 41 |
return {"error": str(e)}
|
| 42 |
|
| 43 |
-
# Rest of the code remains the same
|
| 44 |
-
|
| 45 |
def process_json_input(self, json_data):
|
| 46 |
if "FromUserKavasQuestions" in json_data and "Chatmood" in json_data:
|
| 47 |
prompt = self.create_conversation_starter_prompt(
|
|
@@ -60,6 +43,16 @@ class EndpointHandler:
|
|
| 60 |
else:
|
| 61 |
raise ValueError("Invalid JSON structure.")
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
def create_conversation_starter_prompt(self, user_questions, chatmood):
|
| 64 |
formatted_info = " ".join([f"{qa['Question']} - {qa['Answer']}" for qa in user_questions if qa['Answer']])
|
| 65 |
prompt = (f"Based on user profile info and a {chatmood} mood, "
|
|
|
|
| 3 |
from environs import Env
|
| 4 |
from typing import List, Dict, Any
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
# Load environment variables
|
| 7 |
env = Env()
|
| 8 |
env.read_env("openai.env")
|
|
|
|
| 13 |
AI_RESPONSE_TIMEOUT = env.int("AI_RESPONSE_TIMEOUT", 20)
|
| 14 |
|
| 15 |
class EndpointHandler:
|
| 16 |
+
def __init__(self):
|
| 17 |
pass
|
| 18 |
|
| 19 |
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
|
|
|
| 25 |
except Exception as e:
|
| 26 |
return {"error": str(e)}
|
| 27 |
|
|
|
|
|
|
|
| 28 |
def process_json_input(self, json_data):
|
| 29 |
if "FromUserKavasQuestions" in json_data and "Chatmood" in json_data:
|
| 30 |
prompt = self.create_conversation_starter_prompt(
|
|
|
|
| 43 |
else:
|
| 44 |
raise ValueError("Invalid JSON structure.")
|
| 45 |
|
| 46 |
+
def process_hf_input(self, hf_data):
|
| 47 |
+
print("Received HF Data:", hf_data) # Debugging line
|
| 48 |
+
if "inputs" in hf_data:
|
| 49 |
+
actual_data = hf_data["inputs"]
|
| 50 |
+
print("Processing actual data:", actual_data) # Debugging line
|
| 51 |
+
return self.process_json_input(actual_data)
|
| 52 |
+
else:
|
| 53 |
+
return {"error": "Invalid Hugging Face JSON structure."}
|
| 54 |
+
|
| 55 |
+
|
| 56 |
def create_conversation_starter_prompt(self, user_questions, chatmood):
|
| 57 |
formatted_info = " ".join([f"{qa['Question']} - {qa['Answer']}" for qa in user_questions if qa['Answer']])
|
| 58 |
prompt = (f"Based on user profile info and a {chatmood} mood, "
|