Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,74 @@
|
|
| 1 |
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
-
from transformers import pipeline
|
| 4 |
import torch
|
|
|
|
| 5 |
import re
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# ====== Load Model ======
|
| 8 |
device = 0 if torch.cuda.is_available() else -1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
pipe = pipeline(
|
| 10 |
"text-generation",
|
| 11 |
-
model=
|
| 12 |
device=device,
|
| 13 |
)
|
|
|
|
| 14 |
|
| 15 |
# ====== Chat Function ======
|
| 16 |
def chat_with_model(message, history):
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
context = "The following is a conversation between a user and an AI assistant inspired by the Bhagavad Gita.\n"
|
| 19 |
for user, bot in history:
|
| 20 |
context += f"User: {user}\nAssistant: {bot}\n"
|
| 21 |
context += f"User: {message}\nAssistant:"
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
#
|
|
|
|
|
|
|
| 24 |
output = pipe(
|
| 25 |
context,
|
| 26 |
max_new_tokens=200,
|
|
@@ -30,31 +78,48 @@ def chat_with_model(message, history):
|
|
| 30 |
repetition_penalty=1.1,
|
| 31 |
truncation=True,
|
| 32 |
)[0]["generated_text"]
|
|
|
|
| 33 |
|
| 34 |
-
# Extract assistant reply
|
| 35 |
reply = output[len(context):].strip()
|
| 36 |
-
|
| 37 |
-
# Clean junk or repeated tokens
|
| 38 |
reply = re.sub(r"(ContentLoaded|<\/?[^>]+>|[\r\n]{2,})", " ", reply)
|
| 39 |
reply = re.sub(r"\s{2,}", " ", reply).strip()
|
| 40 |
-
|
| 41 |
-
# Cut off weird repetitions
|
| 42 |
reply = reply.split("User:")[0].split("Assistant:")[0].strip()
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
history.append((message, reply))
|
| 45 |
-
return "", history
|
| 46 |
|
| 47 |
|
| 48 |
# ====== Gradio Interface ======
|
| 49 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
|
| 50 |
-
gr.Markdown("## 💬 Qwen0.5-3B-Gita — Conversational Assistant")
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
msg.submit(chat_with_model, [msg, chatbot], [msg, chatbot])
|
| 57 |
-
clear.click(lambda: None, None, chatbot, queue=False)
|
| 58 |
|
| 59 |
# ====== Launch ======
|
| 60 |
if __name__ == "__main__":
|
|
|
|
| 1 |
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
+
from transformers import pipeline, AutoTokenizer, AutoConfig, AutoModelForCausalLM
|
| 4 |
import torch
|
| 5 |
+
import os
|
| 6 |
import re
|
| 7 |
+
import json
|
| 8 |
+
import time
|
| 9 |
+
from datetime import datetime
|
| 10 |
|
| 11 |
# ====== Load Model ======
|
| 12 |
device = 0 if torch.cuda.is_available() else -1
|
| 13 |
+
model_name = "rahul7star/Qwen0.5-3B-Gita"
|
| 14 |
+
|
| 15 |
+
log_lines = []
|
| 16 |
+
|
| 17 |
+
def log(msg):
|
| 18 |
+
"""Append timestamped message to log."""
|
| 19 |
+
line = f"[{datetime.now().strftime('%H:%M:%S')}] {msg}"
|
| 20 |
+
print(line)
|
| 21 |
+
log_lines.append(line)
|
| 22 |
+
|
| 23 |
+
log("🔍 Initializing model load sequence...")
|
| 24 |
+
log(f"Using model: {model_name}")
|
| 25 |
+
log(f"Detected device: {'GPU' if device == 0 else 'CPU'}")
|
| 26 |
+
|
| 27 |
+
# Inspect model folder (once downloaded from HF cache)
|
| 28 |
+
hf_cache = os.path.expanduser("~/.cache/huggingface/hub")
|
| 29 |
+
log(f"Model will be loaded from local cache directory: {hf_cache}")
|
| 30 |
+
|
| 31 |
+
try:
|
| 32 |
+
config = AutoConfig.from_pretrained(model_name)
|
| 33 |
+
log("✅ Loaded configuration file:")
|
| 34 |
+
log(json.dumps(config.to_dict(), indent=2)[:800] + " ...")
|
| 35 |
+
except Exception as e:
|
| 36 |
+
log(f"⚠️ Could not read model config: {e}")
|
| 37 |
+
|
| 38 |
+
try:
|
| 39 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 40 |
+
log("✅ Tokenizer loaded successfully.")
|
| 41 |
+
log(f"Tokenizer vocab size: {tokenizer.vocab_size}")
|
| 42 |
+
log(f"Tokenizer files found in: {tokenizer.pretrained_vocab_files_map}")
|
| 43 |
+
except Exception as e:
|
| 44 |
+
log(f"⚠️ Could not load tokenizer: {e}")
|
| 45 |
+
|
| 46 |
+
# Load model pipeline
|
| 47 |
+
start_load = time.time()
|
| 48 |
pipe = pipeline(
|
| 49 |
"text-generation",
|
| 50 |
+
model=model_name,
|
| 51 |
device=device,
|
| 52 |
)
|
| 53 |
+
log(f"✅ Model pipeline loaded in {time.time() - start_load:.2f} seconds.")
|
| 54 |
|
| 55 |
# ====== Chat Function ======
|
| 56 |
def chat_with_model(message, history):
|
| 57 |
+
log_lines.clear()
|
| 58 |
+
log("💭 Starting chat generation process...")
|
| 59 |
+
log(f"User message: {message}")
|
| 60 |
+
|
| 61 |
+
# 1️⃣ Build conversation context
|
| 62 |
context = "The following is a conversation between a user and an AI assistant inspired by the Bhagavad Gita.\n"
|
| 63 |
for user, bot in history:
|
| 64 |
context += f"User: {user}\nAssistant: {bot}\n"
|
| 65 |
context += f"User: {message}\nAssistant:"
|
| 66 |
+
log("📄 Built conversation context:")
|
| 67 |
+
log(context)
|
| 68 |
|
| 69 |
+
# 2️⃣ Encode and run model
|
| 70 |
+
log("🧠 Encoding input and generating response...")
|
| 71 |
+
start_time = time.time()
|
| 72 |
output = pipe(
|
| 73 |
context,
|
| 74 |
max_new_tokens=200,
|
|
|
|
| 78 |
repetition_penalty=1.1,
|
| 79 |
truncation=True,
|
| 80 |
)[0]["generated_text"]
|
| 81 |
+
log(f"⏱️ Inference took {time.time() - start_time:.2f} seconds")
|
| 82 |
|
| 83 |
+
# 3️⃣ Extract clean assistant reply
|
| 84 |
reply = output[len(context):].strip()
|
|
|
|
|
|
|
| 85 |
reply = re.sub(r"(ContentLoaded|<\/?[^>]+>|[\r\n]{2,})", " ", reply)
|
| 86 |
reply = re.sub(r"\s{2,}", " ", reply).strip()
|
|
|
|
|
|
|
| 87 |
reply = reply.split("User:")[0].split("Assistant:")[0].strip()
|
| 88 |
|
| 89 |
+
log("🪄 Raw model output processed successfully.")
|
| 90 |
+
log(f"Model reply (cleaned): {reply}")
|
| 91 |
+
|
| 92 |
+
# 4️⃣ Log tokenizer + model folders
|
| 93 |
+
try:
|
| 94 |
+
model_dir = pipe.model.name_or_path
|
| 95 |
+
log(f"📂 Model files are read from: {model_dir}")
|
| 96 |
+
if os.path.exists(model_dir):
|
| 97 |
+
for root, dirs, files in os.walk(model_dir):
|
| 98 |
+
for file in files[:5]: # show first 5 files only
|
| 99 |
+
log(f" - {os.path.join(root, file)}")
|
| 100 |
+
break
|
| 101 |
+
except Exception as e:
|
| 102 |
+
log(f"⚠️ Could not list model folder files: {e}")
|
| 103 |
+
|
| 104 |
+
# 5️⃣ Finalize
|
| 105 |
history.append((message, reply))
|
| 106 |
+
return "", history, "\n".join(log_lines)
|
| 107 |
|
| 108 |
|
| 109 |
# ====== Gradio Interface ======
|
| 110 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
|
| 111 |
+
gr.Markdown("## 💬 Qwen0.5-3B-Gita — Conversational Assistant with Debug Log")
|
| 112 |
|
| 113 |
+
with gr.Row():
|
| 114 |
+
with gr.Column(scale=2):
|
| 115 |
+
chatbot = gr.Chatbot(height=500)
|
| 116 |
+
msg = gr.Textbox(placeholder="Ask about the Gita, life, or philosophy...", label="Your Message")
|
| 117 |
+
clear = gr.Button("Clear")
|
| 118 |
+
with gr.Column(scale=1):
|
| 119 |
+
log_box = gr.Textbox(label="Detailed Model Log", lines=25, interactive=False)
|
| 120 |
|
| 121 |
+
msg.submit(chat_with_model, [msg, chatbot], [msg, chatbot, log_box])
|
| 122 |
+
clear.click(lambda: (None, None, ""), None, [chatbot, log_box], queue=False)
|
| 123 |
|
| 124 |
# ====== Launch ======
|
| 125 |
if __name__ == "__main__":
|