Spaces:
Sleeping
Sleeping
Commit ·
7ddf08a
1
Parent(s): 9d85b05
Updated
Browse files
main.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
# ==============================================================
|
| 2 |
-
# Tech Disciples AI Backend —
|
| 3 |
# ==============================================================
|
| 4 |
|
| 5 |
import os
|
|
@@ -8,7 +8,7 @@ import torch
|
|
| 8 |
from fastapi import FastAPI, Request, Header, HTTPException
|
| 9 |
from fastapi.responses import JSONResponse
|
| 10 |
from pydantic import BaseModel
|
| 11 |
-
from transformers import pipeline
|
| 12 |
|
| 13 |
# --------------------------------------------------------------
|
| 14 |
# Logging Setup
|
|
@@ -56,10 +56,10 @@ async def global_exception_handler(request: Request, exc: Exception):
|
|
| 56 |
# --------------------------------------------------------------
|
| 57 |
class ChatRequest(BaseModel):
|
| 58 |
query: str
|
| 59 |
-
tone: str | None = "neutral"
|
| 60 |
|
| 61 |
# --------------------------------------------------------------
|
| 62 |
-
# Hugging Face Configuration
|
| 63 |
# --------------------------------------------------------------
|
| 64 |
HF_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 65 |
|
|
@@ -68,7 +68,6 @@ if not HF_TOKEN:
|
|
| 68 |
else:
|
| 69 |
logger.info("✅ Hugging Face token detected.")
|
| 70 |
|
| 71 |
-
# Device setup
|
| 72 |
device = 0 if torch.cuda.is_available() else -1
|
| 73 |
logger.info(f"🧠 Using device: {'GPU' if device == 0 else 'CPU'}")
|
| 74 |
|
|
@@ -76,52 +75,62 @@ logger.info(f"🧠 Using device: {'GPU' if device == 0 else 'CPU'}")
|
|
| 76 |
# Model Pipeline Initialization
|
| 77 |
# --------------------------------------------------------------
|
| 78 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
chat_pipe = pipeline(
|
| 80 |
"text-generation",
|
| 81 |
-
model=
|
| 82 |
-
|
| 83 |
device=device,
|
| 84 |
)
|
| 85 |
-
|
|
|
|
| 86 |
except Exception as e:
|
| 87 |
chat_pipe = None
|
| 88 |
-
logger.error(f"❌ Failed to load
|
| 89 |
|
| 90 |
# --------------------------------------------------------------
|
| 91 |
-
# System
|
| 92 |
# --------------------------------------------------------------
|
| 93 |
SYSTEM_TEMPLATE = """
|
| 94 |
-
You are Tech Disciples AI —
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
-
|
| 99 |
-
- Use
|
| 100 |
-
-
|
| 101 |
-
-
|
| 102 |
-
-
|
| 103 |
-
- Always sound like a wise, grounded, caring mentor.
|
| 104 |
"""
|
| 105 |
|
| 106 |
# --------------------------------------------------------------
|
| 107 |
# Helper Function
|
| 108 |
# --------------------------------------------------------------
|
| 109 |
def run_conversational(pipe, user_input: str, tone: str = "neutral") -> str:
|
| 110 |
-
"""Executes text generation safely and returns formatted output with personality control."""
|
| 111 |
if not pipe:
|
| 112 |
return "⚠️ Model pipeline not initialized."
|
| 113 |
|
| 114 |
-
# Define tone modifiers
|
| 115 |
tone_instruction = {
|
| 116 |
-
"neutral": "Keep tone calm
|
| 117 |
-
"friendly": "Use
|
| 118 |
-
"motivational": "
|
| 119 |
-
"scholarly": "
|
| 120 |
-
"pastoral": "
|
| 121 |
-
}.get(tone.lower(), "Keep tone calm and
|
| 122 |
|
| 123 |
try:
|
| 124 |
-
# Combine the system and tone template with the user input
|
| 125 |
prompt = (
|
| 126 |
f"{SYSTEM_TEMPLATE}\n"
|
| 127 |
f"{tone_instruction}\n\n"
|
|
@@ -131,35 +140,30 @@ def run_conversational(pipe, user_input: str, tone: str = "neutral") -> str:
|
|
| 131 |
|
| 132 |
output = pipe(
|
| 133 |
prompt,
|
| 134 |
-
max_new_tokens=
|
| 135 |
-
temperature=0.
|
| 136 |
top_p=0.9,
|
| 137 |
repetition_penalty=1.1,
|
| 138 |
do_sample=True,
|
| 139 |
)
|
| 140 |
|
| 141 |
-
# Parse generated text
|
| 142 |
if isinstance(output, list) and len(output) > 0:
|
| 143 |
text = output[0].get("generated_text", str(output))
|
| 144 |
if "Tech Disciples AI:" in text:
|
| 145 |
text = text.split("Tech Disciples AI:")[-1].strip()
|
| 146 |
return text
|
| 147 |
-
|
| 148 |
return str(output)
|
| 149 |
-
|
| 150 |
except Exception as e:
|
| 151 |
-
logger.error(f"
|
| 152 |
return f"⚠️ Model error: {str(e)}"
|
| 153 |
|
| 154 |
# --------------------------------------------------------------
|
| 155 |
-
# API
|
| 156 |
# --------------------------------------------------------------
|
| 157 |
@app.post("/ai-chat")
|
| 158 |
async def ai_chat(req: ChatRequest, authorization: str | None = Header(None)):
|
| 159 |
-
"""
|
| 160 |
-
Main conversational endpoint for Tech Disciples AI.
|
| 161 |
-
Accepts 'query' and optional 'tone' parameters.
|
| 162 |
-
"""
|
| 163 |
check_auth(authorization)
|
| 164 |
reply = run_conversational(chat_pipe, req.query, req.tone)
|
| 165 |
return {"reply": reply, "tone_used": req.tone}
|
|
|
|
| 1 |
# ==============================================================
|
| 2 |
+
# Tech Disciples AI Backend — ChristGPT Integration Edition
|
| 3 |
# ==============================================================
|
| 4 |
|
| 5 |
import os
|
|
|
|
| 8 |
from fastapi import FastAPI, Request, Header, HTTPException
|
| 9 |
from fastapi.responses import JSONResponse
|
| 10 |
from pydantic import BaseModel
|
| 11 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 12 |
|
| 13 |
# --------------------------------------------------------------
|
| 14 |
# Logging Setup
|
|
|
|
| 56 |
# --------------------------------------------------------------
|
| 57 |
class ChatRequest(BaseModel):
|
| 58 |
query: str
|
| 59 |
+
tone: str | None = "neutral"
|
| 60 |
|
| 61 |
# --------------------------------------------------------------
|
| 62 |
+
# Hugging Face Model Configuration
|
| 63 |
# --------------------------------------------------------------
|
| 64 |
HF_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 65 |
|
|
|
|
| 68 |
else:
|
| 69 |
logger.info("✅ Hugging Face token detected.")
|
| 70 |
|
|
|
|
| 71 |
device = 0 if torch.cuda.is_available() else -1
|
| 72 |
logger.info(f"🧠 Using device: {'GPU' if device == 0 else 'CPU'}")
|
| 73 |
|
|
|
|
| 75 |
# Model Pipeline Initialization
|
| 76 |
# --------------------------------------------------------------
|
| 77 |
try:
|
| 78 |
+
MODEL_NAME = "aao331/ChristGPT-13B-V2-GPTQ"
|
| 79 |
+
|
| 80 |
+
logger.info(f"🚀 Loading model: {MODEL_NAME}")
|
| 81 |
+
|
| 82 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, use_auth_token=HF_TOKEN)
|
| 83 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 84 |
+
MODEL_NAME,
|
| 85 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 86 |
+
device_map="auto",
|
| 87 |
+
use_auth_token=HF_TOKEN,
|
| 88 |
+
low_cpu_mem_usage=True
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
chat_pipe = pipeline(
|
| 92 |
"text-generation",
|
| 93 |
+
model=model,
|
| 94 |
+
tokenizer=tokenizer,
|
| 95 |
device=device,
|
| 96 |
)
|
| 97 |
+
|
| 98 |
+
logger.info("✅ ChristGPT model successfully loaded and ready.")
|
| 99 |
except Exception as e:
|
| 100 |
chat_pipe = None
|
| 101 |
+
logger.error(f"❌ Failed to load ChristGPT model: {e}")
|
| 102 |
|
| 103 |
# --------------------------------------------------------------
|
| 104 |
+
# System Template
|
| 105 |
# --------------------------------------------------------------
|
| 106 |
SYSTEM_TEMPLATE = """
|
| 107 |
+
You are Tech Disciples AI — a spiritually grounded assistant powered by biblical wisdom and faith-based intelligence.
|
| 108 |
+
|
| 109 |
+
Core values:
|
| 110 |
+
- Speak with humility, kindness, and sincerity.
|
| 111 |
+
- Offer counsel rooted in scripture, not personal opinion.
|
| 112 |
+
- Use gentle, thoughtful language even when correcting.
|
| 113 |
+
- Reflect Christian virtues — patience, love, faith, hope.
|
| 114 |
+
- Avoid condemnation, bias, or debate.
|
| 115 |
+
- When asked about general knowledge, respond wisely and truthfully.
|
|
|
|
| 116 |
"""
|
| 117 |
|
| 118 |
# --------------------------------------------------------------
|
| 119 |
# Helper Function
|
| 120 |
# --------------------------------------------------------------
|
| 121 |
def run_conversational(pipe, user_input: str, tone: str = "neutral") -> str:
|
|
|
|
| 122 |
if not pipe:
|
| 123 |
return "⚠️ Model pipeline not initialized."
|
| 124 |
|
|
|
|
| 125 |
tone_instruction = {
|
| 126 |
+
"neutral": "Keep tone calm and balanced.",
|
| 127 |
+
"friendly": "Use warmth, encouragement, and care.",
|
| 128 |
+
"motivational": "Inspire faith, hope, and perseverance.",
|
| 129 |
+
"scholarly": "Speak with scriptural and logical reasoning.",
|
| 130 |
+
"pastoral": "Provide gentle, spiritual comfort.",
|
| 131 |
+
}.get(tone.lower(), "Keep tone calm and balanced.")
|
| 132 |
|
| 133 |
try:
|
|
|
|
| 134 |
prompt = (
|
| 135 |
f"{SYSTEM_TEMPLATE}\n"
|
| 136 |
f"{tone_instruction}\n\n"
|
|
|
|
| 140 |
|
| 141 |
output = pipe(
|
| 142 |
prompt,
|
| 143 |
+
max_new_tokens=250,
|
| 144 |
+
temperature=0.5,
|
| 145 |
top_p=0.9,
|
| 146 |
repetition_penalty=1.1,
|
| 147 |
do_sample=True,
|
| 148 |
)
|
| 149 |
|
|
|
|
| 150 |
if isinstance(output, list) and len(output) > 0:
|
| 151 |
text = output[0].get("generated_text", str(output))
|
| 152 |
if "Tech Disciples AI:" in text:
|
| 153 |
text = text.split("Tech Disciples AI:")[-1].strip()
|
| 154 |
return text
|
| 155 |
+
|
| 156 |
return str(output)
|
| 157 |
+
|
| 158 |
except Exception as e:
|
| 159 |
+
logger.error(f"Pipeline generation error: {e}")
|
| 160 |
return f"⚠️ Model error: {str(e)}"
|
| 161 |
|
| 162 |
# --------------------------------------------------------------
|
| 163 |
+
# API Endpoint
|
| 164 |
# --------------------------------------------------------------
|
| 165 |
@app.post("/ai-chat")
|
| 166 |
async def ai_chat(req: ChatRequest, authorization: str | None = Header(None)):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
check_auth(authorization)
|
| 168 |
reply = run_conversational(chat_pipe, req.query, req.tone)
|
| 169 |
return {"reply": reply, "tone_used": req.tone}
|