Nhughes09
commited on
Commit
·
102288d
1
Parent(s):
53d866d
Fix: Use correct OpenAI-compatible API URL with comprehensive logging
Browse files
app.py
CHANGED
|
@@ -2,86 +2,144 @@ import gradio as gr
|
|
| 2 |
import requests
|
| 3 |
import logging
|
| 4 |
import sys
|
| 5 |
-
import time
|
| 6 |
import os
|
|
|
|
|
|
|
| 7 |
|
|
|
|
| 8 |
logging.basicConfig(
|
| 9 |
-
level=logging.
|
| 10 |
-
format="%(asctime)s - %(levelname)s - %(message)s",
|
| 11 |
handlers=[logging.StreamHandler(sys.stdout)]
|
| 12 |
)
|
| 13 |
logger = logging.getLogger("ChatbotBrain")
|
| 14 |
|
|
|
|
|
|
|
|
|
|
| 15 |
logger.info(f"Gradio Version: {gr.__version__}")
|
| 16 |
logger.info(f"Python Version: {sys.version}")
|
| 17 |
|
|
|
|
| 18 |
MODEL_NAME = "HuggingFaceH4/zephyr-7b-beta"
|
| 19 |
|
|
|
|
| 20 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
|
|
|
| 21 |
if HF_TOKEN:
|
| 22 |
-
logger.info("SUCCESS: HF_TOKEN
|
| 23 |
-
logger.info(f"Token
|
| 24 |
else:
|
| 25 |
-
logger.error("ERROR: HF_TOKEN not found
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
logger.info(f"
|
| 31 |
logger.info(f"API URL: {API_URL}")
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
try:
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
if response.status_code != 200:
|
| 39 |
-
logger.error(f"API
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
result = response.json()
|
| 42 |
-
logger.info(
|
|
|
|
| 43 |
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
except Exception as e:
|
| 45 |
-
logger.error(f"
|
|
|
|
| 46 |
return {"error": str(e)}
|
| 47 |
|
|
|
|
| 48 |
def respond(message, history):
|
| 49 |
-
|
| 50 |
-
logger.info(
|
| 51 |
-
logger.info(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
prompt = ""
|
| 54 |
for user_msg, assistant_msg in history:
|
| 55 |
-
|
| 56 |
-
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
"parameters": {"max_new_tokens": 512, "temperature": 0.7, "do_sample": True}
|
| 63 |
-
}
|
| 64 |
|
| 65 |
-
result = query_model(
|
| 66 |
|
| 67 |
if "error" in result:
|
| 68 |
-
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
logger.info(
|
| 76 |
-
|
| 77 |
-
return text
|
| 78 |
|
|
|
|
|
|
|
| 79 |
logger.info("Building Gradio Interface...")
|
| 80 |
|
| 81 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 82 |
gr.Markdown("# CPU Chatbot")
|
| 83 |
gr.Markdown(f"### Powered by {MODEL_NAME}")
|
| 84 |
-
gr.Markdown("Check Container Logs to see AI
|
| 85 |
|
| 86 |
chatbot = gr.Chatbot(height=500)
|
| 87 |
msg = gr.Textbox(placeholder="Ask me anything...", label="Your message")
|
|
@@ -105,7 +163,8 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 105 |
bot_respond, chatbot, chatbot
|
| 106 |
)
|
| 107 |
|
|
|
|
|
|
|
| 108 |
if __name__ == "__main__":
|
| 109 |
logger.info("Launching Gradio App...")
|
| 110 |
demo.launch()
|
| 111 |
-
|
|
|
|
| 2 |
import requests
|
| 3 |
import logging
|
| 4 |
import sys
|
|
|
|
| 5 |
import os
|
| 6 |
+
import json
|
| 7 |
+
import traceback
|
| 8 |
|
| 9 |
+
# --- EXTREME LOGGING SETUP ---
|
| 10 |
logging.basicConfig(
|
| 11 |
+
level=logging.DEBUG,
|
| 12 |
+
format="%(asctime)s - %(levelname)s - [%(funcName)s] %(message)s",
|
| 13 |
handlers=[logging.StreamHandler(sys.stdout)]
|
| 14 |
)
|
| 15 |
logger = logging.getLogger("ChatbotBrain")
|
| 16 |
|
| 17 |
+
logger.info("=" * 60)
|
| 18 |
+
logger.info("STARTUP: CPU Chatbot Initializing...")
|
| 19 |
+
logger.info("=" * 60)
|
| 20 |
logger.info(f"Gradio Version: {gr.__version__}")
|
| 21 |
logger.info(f"Python Version: {sys.version}")
|
| 22 |
|
| 23 |
+
# --- CONFIGURATION ---
|
| 24 |
MODEL_NAME = "HuggingFaceH4/zephyr-7b-beta"
|
| 25 |
|
| 26 |
+
# Read HF_TOKEN from environment variable (set in Space Secrets)
|
| 27 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 28 |
+
logger.info("Checking for HF_TOKEN in environment...")
|
| 29 |
if HF_TOKEN:
|
| 30 |
+
logger.info(f"SUCCESS: HF_TOKEN found! Length: {len(HF_TOKEN)} chars")
|
| 31 |
+
logger.info(f"Token preview: {HF_TOKEN[:15]}...{HF_TOKEN[-5:]}")
|
| 32 |
else:
|
| 33 |
+
logger.error("CRITICAL ERROR: HF_TOKEN not found in environment!")
|
| 34 |
+
logger.error("Please add HF_TOKEN to your Space Secrets")
|
| 35 |
|
| 36 |
+
# Use the OpenAI-compatible chat completions endpoint
|
| 37 |
+
API_URL = "https://router.huggingface.co/v1/chat/completions"
|
| 38 |
+
HEADERS = {
|
| 39 |
+
"Authorization": f"Bearer {HF_TOKEN}",
|
| 40 |
+
"Content-Type": "application/json"
|
| 41 |
+
} if HF_TOKEN else {}
|
| 42 |
|
| 43 |
+
logger.info(f"Model: {MODEL_NAME}")
|
| 44 |
logger.info(f"API URL: {API_URL}")
|
| 45 |
+
logger.info(f"Headers configured: {list(HEADERS.keys())}")
|
| 46 |
|
| 47 |
+
|
| 48 |
+
def query_model(messages):
|
| 49 |
+
"""Send a request to the HuggingFace Inference API with detailed logging."""
|
| 50 |
+
logger.info("-" * 40)
|
| 51 |
+
logger.info("QUERY_MODEL: Starting API call...")
|
| 52 |
+
|
| 53 |
+
payload = {
|
| 54 |
+
"model": MODEL_NAME,
|
| 55 |
+
"messages": messages,
|
| 56 |
+
"max_tokens": 512,
|
| 57 |
+
"temperature": 0.7
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
logger.debug(f"Request URL: {API_URL}")
|
| 61 |
+
logger.debug(f"Request Headers: Authorization=Bearer {HF_TOKEN[:10] if HF_TOKEN else 'MISSING'}...")
|
| 62 |
+
logger.debug(f"Request Payload: {json.dumps(payload, indent=2)}")
|
| 63 |
+
|
| 64 |
try:
|
| 65 |
+
logger.info("Sending POST request to API...")
|
| 66 |
+
response = requests.post(API_URL, headers=HEADERS, json=payload, timeout=120)
|
| 67 |
+
|
| 68 |
+
logger.info(f"Response Status Code: {response.status_code}")
|
| 69 |
+
logger.debug(f"Response Headers: {dict(response.headers)}")
|
| 70 |
+
|
| 71 |
if response.status_code != 200:
|
| 72 |
+
logger.error(f"API ERROR: Status {response.status_code}")
|
| 73 |
+
logger.error(f"Response Body: {response.text}")
|
| 74 |
+
logger.error(f"URL Used: {API_URL}")
|
| 75 |
+
logger.error(f"Model Used: {MODEL_NAME}")
|
| 76 |
+
return {"error": f"API returned {response.status_code}: {response.text[:500]}"}
|
| 77 |
+
|
| 78 |
result = response.json()
|
| 79 |
+
logger.info("SUCCESS: Got valid JSON response")
|
| 80 |
+
logger.debug(f"Response JSON: {json.dumps(result, indent=2)}")
|
| 81 |
return result
|
| 82 |
+
|
| 83 |
+
except requests.exceptions.Timeout:
|
| 84 |
+
logger.error("TIMEOUT: Request took longer than 120 seconds")
|
| 85 |
+
return {"error": "Request timed out after 120 seconds"}
|
| 86 |
+
except requests.exceptions.ConnectionError as e:
|
| 87 |
+
logger.error(f"CONNECTION ERROR: {e}")
|
| 88 |
+
return {"error": f"Connection error: {e}"}
|
| 89 |
except Exception as e:
|
| 90 |
+
logger.error(f"UNEXPECTED ERROR: {e}")
|
| 91 |
+
logger.error(traceback.format_exc())
|
| 92 |
return {"error": str(e)}
|
| 93 |
|
| 94 |
+
|
| 95 |
def respond(message, history):
|
| 96 |
+
"""Generate a response from the AI model."""
|
| 97 |
+
logger.info("=" * 60)
|
| 98 |
+
logger.info("RESPOND: New user message received")
|
| 99 |
+
logger.info(f"User Message: {message}")
|
| 100 |
+
logger.info(f"History Length: {len(history)} messages")
|
| 101 |
+
|
| 102 |
+
# Build messages array (OpenAI format)
|
| 103 |
+
messages = []
|
| 104 |
+
messages.append({"role": "system", "content": "You are a helpful AI assistant."})
|
| 105 |
|
|
|
|
| 106 |
for user_msg, assistant_msg in history:
|
| 107 |
+
messages.append({"role": "user", "content": user_msg})
|
| 108 |
+
if assistant_msg:
|
| 109 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 110 |
|
| 111 |
+
messages.append({"role": "user", "content": message})
|
| 112 |
|
| 113 |
+
logger.info(f"Built {len(messages)} messages for API")
|
| 114 |
+
logger.debug(f"Messages: {json.dumps(messages, indent=2)}")
|
|
|
|
|
|
|
| 115 |
|
| 116 |
+
result = query_model(messages)
|
| 117 |
|
| 118 |
if "error" in result:
|
| 119 |
+
error_msg = result["error"]
|
| 120 |
+
logger.error(f"Returning error to user: {error_msg}")
|
| 121 |
+
return f"Error: {error_msg}"
|
| 122 |
|
| 123 |
+
# Extract response from OpenAI-compatible format
|
| 124 |
+
try:
|
| 125 |
+
response_text = result["choices"][0]["message"]["content"]
|
| 126 |
+
logger.info(f"AI Response: {response_text[:200]}...")
|
| 127 |
+
except (KeyError, IndexError) as e:
|
| 128 |
+
logger.error(f"Failed to parse response: {e}")
|
| 129 |
+
logger.error(f"Raw result: {result}")
|
| 130 |
+
response_text = f"Error parsing response: {result}"
|
| 131 |
|
| 132 |
+
logger.info("=" * 60)
|
| 133 |
+
return response_text
|
|
|
|
| 134 |
|
| 135 |
+
|
| 136 |
+
# --- GRADIO UI ---
|
| 137 |
logger.info("Building Gradio Interface...")
|
| 138 |
|
| 139 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 140 |
gr.Markdown("# CPU Chatbot")
|
| 141 |
gr.Markdown(f"### Powered by {MODEL_NAME}")
|
| 142 |
+
gr.Markdown("Check Container Logs to see detailed AI processing!")
|
| 143 |
|
| 144 |
chatbot = gr.Chatbot(height=500)
|
| 145 |
msg = gr.Textbox(placeholder="Ask me anything...", label="Your message")
|
|
|
|
| 163 |
bot_respond, chatbot, chatbot
|
| 164 |
)
|
| 165 |
|
| 166 |
+
logger.info("Gradio Interface built successfully!")
|
| 167 |
+
|
| 168 |
if __name__ == "__main__":
|
| 169 |
logger.info("Launching Gradio App...")
|
| 170 |
demo.launch()
|
|
|