Spaces:
Paused
Paused
Create app1.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171# app.pyimport gradio as grfrom transformers import AutoTokenizer, AutoConfig, AutoModelForCausalLM, pipelinefrom peft import PeftModelimport torchimport osimport reimport jsonimport timefrom datetime import datetimefrom huggingface_hub import hf_hub_download, model_info
# ====== Settings ======device = 0 if torch.cuda.is_available() else -1base_model_name = "Qwen/Qwen2.5-0.5B" # Base model for LoRAfinetuned_repo = "rahul7star/Qwen2.5-3B-Gita"
log_lines = []
def log(msg): """Append timestamped message to log.""" line = f"[{datetime.now().strftime('%H:%M:%S')}] {msg}" print(line) log_lines.append(line)
# ====== Start Logging ======log("π Initializing model load sequence...")log(f"Base model: {base_model_name}")log(f"Fine-tuned LoRA repo: {finetuned_repo}")log(f"Device detected: {'GPU' if device==0 else 'CPU'}")hf_cache = os.path.expanduser("~/.cache/huggingface/hub")log(f"Model cache directory: {hf_cache}")
# ====== Inspect Hugging Face repo ======try: info = model_info(finetuned_repo) log("π¦ Hugging Face repo info loaded:") log(f" - Model ID: {info.id}") log(f" - Private: {info.private}") log(f" - Last modified: {info.last_modified}") log(f" - Files count: {len(info.siblings)}") for s in info.siblings[:5]: log(f" Β· {s.rfilename}")except Exception as e: log(f"β οΈ Could not fetch model info: {e}")
# ====== Load base model and tokenizer ======try: tokenizer = AutoTokenizer.from_pretrained(base_model_name, trust_remote_code=True) if tokenizer.pad_token is None: tokenizer.pad_token = tokenizer.eos_token log(f"β
Tokenizer loaded: vocab size {tokenizer.vocab_size}")except Exception as e: log(f"β Failed to load tokenizer: {e}") tokenizer = None
try: base_model = AutoModelForCausalLM.from_pretrained( base_model_name, trust_remote_code=True, torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32, device_map="auto" if torch.cuda.is_available() else None, ) log(f"β
Base model loaded from {base_model_name}")except Exception as e: log(f"β Failed to load base model: {e}") base_model = None
# ====== Load fine-tuned LoRA weights ======model = Nonepipe = Nonetry: if base_model is not None: model = PeftModel.from_pretrained(base_model, finetuned_repo) model.eval() log(f"β
LoRA fine-tuned model loaded from {finetuned_repo}") log(f"π§© Model architecture: {getattr(model.config, 'architectures', ['Unknown'])}")
pipe = pipeline( "text-generation", model=model, tokenizer=tokenizer, device=device, ) log("β
Pipeline ready for inference")except Exception as e: log(f"β Failed to load LoRA model: {e}")
# ====== Try to extract training info ======def extract_training_info(repo_name): data = {} try: readme_path = hf_hub_download(repo_name, filename="README.md") with open(readme_path, "r", encoding="utf-8") as f: text = f.read() matches = re.findall(r"(rahul7star/\w+|dataset|fine[- ]?tune|trained on|data:)", text, re.I) if matches: data["readme_mentions"] = matches[:5] log(f"β
README mentions dataset/fine-tune: {matches[:5]}") else: log("βΉοΈ No dataset reference found in README") except Exception as e: log(f"β οΈ README not found or unreadable: {e}") return data
training_info = extract_training_info(finetuned_repo)
# ====== Chat Function ======def chat_with_model(message, history): log_lines.clear() log("π Starting chat generation...") log(f"User message: {message}")
if pipe is None: return "", history, "β οΈ Model pipeline not loaded. Check logs."
# Build context context = "The following is a conversation between a user and an AI assistant inspired by the Bhagavad Gita.\n" for user, bot in history: context += f"User: {user}\nAssistant: {bot}\n" context += f"User: {message}\nAssistant:" log("π Context built:") log(context)
# Generate log("π§ Generating response...") start_time = time.time() try: output = pipe( context, max_new_tokens=200, do_sample=True, temperature=0.7, top_p=0.9, repetition_penalty=1.1, )[0]["generated_text"] log(f"β±οΈ Inference took {time.time() - start_time:.2f}s") except Exception as e: log(f"β Generation failed: {e}") return "", history, "\n".join(log_lines)
# Clean output reply = output[len(context):].strip() reply = re.sub(r"(ContentLoaded|<\/?[^>]+>|[\r\n]{2,})", " ", reply) reply = re.sub(r"\s{2,}", " ", reply).strip() reply = reply.split("User:")[0].split("Assistant:")[0].strip()
log("πͺ Output cleaned successfully") log(f"Model reply: {reply}")
history.append((message, reply)) return "", history, "\n".join(log_lines)
# ====== Gradio Interface ======with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo: gr.Markdown("## π¬ Qwen Gita β LoRA Fine-tuned Conversational Assistant")
with gr.Row(): with gr.Column(scale=2): chatbot = gr.Chatbot(height=500) msg = gr.Textbox(placeholder="Ask about the Gita, life, or philosophy...", label="Your Message") clear = gr.Button("Clear") with gr.Column(scale=1): log_box = gr.Textbox(label="Detailed Model Log", lines=25, interactive=False)
msg.submit(chat_with_model, [msg, chatbot], [msg, chatbot, log_box]) clear.click(lambda: (None, None, ""), None, [chatbot, log_box], queue=False)
# ====== Launch ======if __name__ == "__main__": demo.launch(server_name="0.0.0.0", server_port=7860)app1.pyAdded+171