Spaces:
Sleeping
Sleeping
Update app.py
Browse files10 unmodified lines11121314151617184 unmodified lines23242526272829303132333435363738394 unmodified lines4445464748495051525354555657585910 unmodified linestokenizer = AutoTokenizer.from_pretrained(base_model_name)base_model = AutoModelForCausalLM.from_pretrained(base_model_name,torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,device_map="auto" if torch.cuda.is_available() else None,)print(f"π Loading LoRA adapter: {adapter_model_name}")4 unmodified lines"text-generation",model=model,tokenizer=tokenizer,torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,device_map="auto" if torch.cuda.is_available() else None,)# ===== CHAT FUNCTION =====def chat_fn(messages, temperature=0.7, max_new_tokens=256):conversation = ""for m in messages:role = m["role"]content = m["content"]conversation += f"{role}: {content}\n"prompt = conversation + "assistant:"outputs = pipe(4 unmodified linestop_p=0.9,pad_token_id=tokenizer.eos_token_id,)reply = outputs[0]["generated_text"].split("assistant:")[-1].strip()return {"role": "assistant", "content": reply}# ===== GRADIO UI =====chatbot = gr.ChatInterface(fn=chat_fn,type="messages", # β Fix for tuple errortitle="π Travel Chatbot (LoRA v3)",description="Ask about travel destinations, recommendations, and itineraries across the Philippines.",)if __name__ == "__main__":chatbot.launch(server_name="0.0.0.0", server_port=7860)10 unmodified lines11121314151617184 unmodified lines2324252627282930313233343536373839404142434445464 unmodified lines51525354555657585960616263646566676810 unmodified linestokenizer = AutoTokenizer.from_pretrained(base_model_name)base_model = AutoModelForCausalLM.from_pretrained(base_model_name,dtype=torch.float32,device_map="cpu")print(f"π Loading LoRA adapter: {adapter_model_name}")4 unmodified lines"text-generation",model=model,tokenizer=tokenizer,dtype=torch.float32,device_map="cpu")# ===== CHAT FUNCTION =====def chat_fn(messages, temperature=0.7, max_new_tokens=256):# Handle both Gradio message formatsconversation = ""if isinstance(messages, list):for m in messages:if isinstance(m, dict): # type='messages'conversation += f"{m['role']}: {m['content']}\n"elif isinstance(m, (list, tuple)) and len(m) == 2: # old type='tuples'conversation += f"user: {m[0]}\nassistant: {m[1]}\n"elif isinstance(m, str): # fallback single string messageconversation += f"user: {m}\n"else:conversation += f"user: {messages}\n"prompt = conversation + "assistant:"outputs = pipe(4 unmodified linestop_p=0.9,pad_token_id=tokenizer.eos_token_id,)reply = outputs[0]["generated_text"].split("assistant:")[-1].strip()return {"role": "assistant", "content": reply}# ===== GRADIO UI =====chatbot = gr.ChatInterface(fn=chat_fn,type="messages", # required for Hugging Face Spacestitle="π Travel Chatbot (LoRA v3)",description="Chat with the travel assistant trained on Philippine travel data. Ask about destinations, itineraries, and tips!",)if __name__ == "__main__":print("Device set to use cpu")chatbot.launch(server_name="0.0.0.0", server_port=7860)
app.py+19-10