Spaces:
Sleeping
Sleeping
File size: 2,289 Bytes
96c09a4 ede2f88 2a35895 96c09a4 afc331e 2a35895 afc331e 96c09a4 afc331e 96c09a4 ede2f88 afc331e ede2f88 afc331e 96c09a4 2a35895 96c09a4 2a35895 afc331e ede2f88 96c09a4 2a35895 96c09a4 833006b ede2f88 833006b 96c09a4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | import gradio as gr
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
import os
# --- 1. Securely Get the Hugging Face Token from Space Secrets ---
auth_token = os.getenv("HF_TOKEN")
# --- DEBUGGING STEP: Check if the token was found ---
# This will print to your Space's logs.
if auth_token is not None:
# Do not print the full token for security. Just confirm it was found.
print("✅ HF_TOKEN secret was found by the application.")
else:
print("❌ HF_TOKEN secret was NOT found. Please double-check it is set in your Space Settings.")
# --- END DEBUGGING STEP ---
# --- 2. Load your Model using the Token ---
MODEL_ID = "Bur3hani/karani_ofline"
MODEL_LOADED = False
try:
if auth_token:
print(f"Attempting to load tokenizer and model from Hub: {MODEL_ID}...")
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, token=auth_token)
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_ID, token=auth_token)
print("✅ Model and Tokenizer loaded successfully.")
MODEL_LOADED = True
else:
# This will be printed if the secret is missing.
print("Skipping model loading because HF_TOKEN is missing.")
except Exception as e:
print(f"❌ An error occurred while loading the model from the Hub: {e}")
# --- 3. Define the Prediction Function ---
def get_chat_response(message, history):
if not MODEL_LOADED:
return "ERROR: The AI model failed to load. Please check the Space logs for the exact error."
input_text = ""
for user_turn, bot_turn in history:
input_text += f"user: {user_turn} bot: {bot_turn} "
input_text += f"user: {message}"
inputs = tokenizer(input_text, return_tensors="pt", max_length=512, truncation=True)
outputs = model.generate(**inputs, max_length=60, num_beams=4, early_stopping=True)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
return response
# --- 4. Build and Launch the Gradio Interface ---
demo = gr.ChatInterface(
fn=get_chat_response,
title="Karani v1 - AI Secretary",
description="A conversational AI assistant for Kiswahili, powered by a custom fine-tuned model.",
examples=[["Habari za asubuhi?"], ["Ni nini mpango wa leo?"]],
)
if __name__ == "__main__":
demo.launch() |