Spaces:
Runtime error
Runtime error
Create codette_openai_fallback.py
Browse files- codette_openai_fallback.py +58 -0
codette_openai_fallback.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# codette_openai_fallback.py
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import logging
|
| 5 |
+
import openai
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 8 |
+
import torch
|
| 9 |
+
|
| 10 |
+
# Load environment variables (local or HF secrets)
|
| 11 |
+
load_dotenv()
|
| 12 |
+
logger = logging.getLogger("CodetteFallback")
|
| 13 |
+
logger.setLevel(logging.INFO)
|
| 14 |
+
|
| 15 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 16 |
+
OPENAI_MODEL = "ft:gpt-4o-2024-08-06:raiffs-bits:pidette:B9TLP9QA"
|
| 17 |
+
SYSTEM_PROMPT = "You are Codette, an intelligent, empathetic assistant with advanced reasoning."
|
| 18 |
+
|
| 19 |
+
LOCAL_MODEL_NAME = os.getenv("CODETTE_LOCAL_MODEL", "Raiff1982/Codette")
|
| 20 |
+
|
| 21 |
+
# Attempt to load local model
|
| 22 |
+
try:
|
| 23 |
+
local_tokenizer = AutoTokenizer.from_pretrained(LOCAL_MODEL_NAME)
|
| 24 |
+
local_model = AutoModelForCausalLM.from_pretrained(LOCAL_MODEL_NAME)
|
| 25 |
+
logger.info("[CodetteFallback] Local model loaded.")
|
| 26 |
+
except Exception as e:
|
| 27 |
+
logger.warning(f"[CodetteFallback] Local fallback unavailable: {e}")
|
| 28 |
+
local_model = None
|
| 29 |
+
local_tokenizer = None
|
| 30 |
+
|
| 31 |
+
def query_codette_with_fallback(prompt: str, user_id: str = "anon") -> str:
|
| 32 |
+
try:
|
| 33 |
+
logger.info(f"[Codette:OpenAI] Query from {user_id}: {prompt}")
|
| 34 |
+
response = openai.ChatCompletion.create(
|
| 35 |
+
model=OPENAI_MODEL,
|
| 36 |
+
messages=[
|
| 37 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
| 38 |
+
{"role": "user", "content": prompt}
|
| 39 |
+
],
|
| 40 |
+
temperature=0.7,
|
| 41 |
+
max_tokens=1024,
|
| 42 |
+
user=user_id
|
| 43 |
+
)
|
| 44 |
+
return response["choices"][0]["message"]["content"]
|
| 45 |
+
|
| 46 |
+
except Exception as e:
|
| 47 |
+
logger.warning(f"[Codette:OpenAI fallback triggered] {e}")
|
| 48 |
+
|
| 49 |
+
if local_model and local_tokenizer:
|
| 50 |
+
try:
|
| 51 |
+
inputs = local_tokenizer(prompt, return_tensors="pt")
|
| 52 |
+
outputs = local_model.generate(**inputs, max_length=1024)
|
| 53 |
+
return local_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 54 |
+
except Exception as inner_e:
|
| 55 |
+
logger.error(f"[Codette:Local fallback failed] {inner_e}")
|
| 56 |
+
return "Codette couldnβt generate a response due to internal issues."
|
| 57 |
+
|
| 58 |
+
return "Codette is currently unavailable. Please check connectivity or model settings."
|