Spaces:
Sleeping
Sleeping
Update app.py
Browse files1 unmodified line2345576798910111314121314151617181921202122233 unmodified lines27282930313233333435363738394041424344454647424344454647484950515253545548495051525354555657585960611 unmodified lineimport torchfrom transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
# --- CONFIGURATION ---# Your adapter model ID from the Hub (from Step 2)# Point this to your MERGED MODEL repositoryhf_username = "fortify"repo_name = "gtkt-faq-gemma-adapter"repo_name = "gtkt-faq-gemma-merged"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!# 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 performance device_map="auto",)print("Merged model and tokenizer loaded successfully!")print("Model and tokenizer loaded successfully!")
# --- CREATE THE INFERENCE PIPELINE ---3 unmodified lines tokenizer=tokenizer, max_new_tokens=150)print("Inference pipeline created.")
# --- DEFINE THE INFERENCE FUNCTION ---# --- 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 training prompt = f"### Question:\n{question}\n\n### Answer:\n"
# Run the pipeline result = pipe(prompt)
# Extract and clean up the answer text 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" )# --- 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