Spaces:
Sleeping
Sleeping
Update app.py
Browse files1 unmodified line234567891011121314151617181920212223243 unmodified lines2829303132333435363738394041424344454647484950515253545556571 unmodified lineimport torchfrom transformers import AutoTokenizer, AutoModelForCausalLM, pipeline# --- CONFIGURATION ---# Your adapter model ID from the Hub (from Step 2)hf_username = "fortify"repo_name = "gtkt-faq-gemma-adapter"model_id = f"{hf_username}/{repo_name}"# --- LOAD MODEL & TOKENIZER ---# This is now a simple, standard loading process.# No quantization, no PEFT, no bitsandbytes needed!tokenizer = AutoTokenizer.from_pretrained(model_id)model = AutoModelForCausalLM.from_pretrained(model_id,torch_dtype=torch.bfloat16, # Use bfloat16 for performancedevice_map="auto",)print("Merged model and tokenizer loaded successfully!")# --- CREATE THE INFERENCE PIPELINE ---3 unmodified linestokenizer=tokenizer,max_new_tokens=150)# --- DEFINE THE INFERENCE FUNCTION ---def generate_answer(question):prompt = f"### Question:\n{question}\n\n### Answer:\n"result = pipe(prompt)response_text = result[0]['generated_text']answer_part = response_text.split("### Answer:\n")[-1]return answer_part.strip()# --- CREATE THE GRADIO INTERFACE ---with gr.Blocks() as demo:gr.Markdown("# FAQ Chatbot\nEnter your question below to get an answer from the fine-tuned AI.")with gr.Row():question_box = gr.Textbox(label="Your Question")answer_box = gr.Textbox(label="AI Answer", interactive=False)submit_btn = gr.Button("Ask")submit_btn.click(fn=generate_answer,inputs=question_box,outputs=answer_box,api_name="ask_faq")demo.launch()1 unmodified line2345678910111213141516171819202122233 unmodified lines27282930313233343536373839404142434445464748495051525354555657585960611 unmodified lineimport torchfrom transformers import AutoTokenizer, AutoModelForCausalLM, pipeline# --- CONFIGURATION ---# Point this to your MERGED MODEL repositoryhf_username = "fortify"repo_name = "gtkt-faq-gemma-merged"model_id = f"{hf_username}/{repo_name}"# --- LOAD MODEL & TOKENIZER ---# This is a standard loading process.print("Loading model and tokenizer...")tokenizer = AutoTokenizer.from_pretrained(model_id)model = AutoModelForCausalLM.from_pretrained(model_id,torch_dtype=torch.bfloat16, # Use bfloat16 for performancedevice_map="auto",)print("Model and tokenizer loaded successfully!")# --- CREATE THE INFERENCE PIPELINE ---3 unmodified linestokenizer=tokenizer,max_new_tokens=150)print("Inference pipeline created.")# --- DEFINE THE CORE INFERENCE FUNCTION ---# This is the function that will be exposed via the API.def generate_answer(question):# Create the prompt using the same format as trainingprompt = f"### Question:\n{question}\n\n### Answer:\n"# Run the pipelineresult = pipe(prompt)# Extract and clean up the answer textresponse_text = result[0]['generated_text']answer_part = response_text.split("### Answer:\n")[-1]return answer_part.strip()# --- USE GR.INTERFACE TO CREATE THE APP AND API ---# This is the most robust way to create a simple UI with a reliable API.# It automatically creates a POST endpoint at /api/predict.demo = gr.Interface(fn=generate_answer,inputs=gr.Textbox(label="Your Question", lines=3),outputs=gr.Textbox(label="AI Answer", interactive=False),title="AI-Powered FAQ Bot",description="Ask a question and the fine-tuned Gemma model will provide an answer from the knowledge base.")# Launch the appprint("Launching Gradio Interface...")demo.launch()
app.py+25-21