silvermete0r's picture
Update app.py
0d9a157 verified
import gradio as gr
import keras
import keras_nlp
from huggingface_hub import hf_hub_download
repo_id = "silvermete0r/Gemma2_2B_QazPerry"
filename = "Gemma2_2B_QazPerry.keras"
# Load the model
model_path = hf_hub_download(repo_id=repo_id, filename=filename)
gemma_lm = keras.models.load_model(model_path, custom_objects={"GemmaCausalLM": keras_nlp.models.GemmaCausalLM})
# Explicitly compile the model if necessary
try:
gemma_lm.compile()
except:
print("Skipping compilation, model is inference-only.")
# Text generation function
def ask_qaz_perry(prompt: str) -> str:
default_response = "Мен QazPerry сіздің сұрағыңызды түсінбедім. Өтінемін, сұрағыңызды қайталап, басқаша жеткізіңізші."
prompt_template = "Instruction: {}\n\nResponse:\n"
translate_template = "Instruction: Translate this text to Kazakh Language : {}\n\nResponse in Kazakh:\n"
instruction_response = gemma_lm.generate(prompt_template.format(prompt), max_length=1024)
instruction_response = instruction_response.split('<|end_header_id|>')[-1]
instruction_response = instruction_response.strip()
instruction_response = ". ".join(instruction_response.split('.')[:-1]) + "." if instruction_response[-1] != '.' else instruction_response
final_response = gemma_lm.generate(translate_template.format(instruction_response), max_length=1024)
final_response = final_response.split('Response in Kazakh:')[-1].strip()
final_response = final_response.split('\n')[0]
final_response = final_response.strip()
final_response = default_response if len(final_response) < 10 else final_response
return final_response
# Gradio Interface
interface = gr.Interface(
fn=ask_qaz_perry,
inputs=gr.Textbox(lines=5, placeholder="Enter your prompt here..."),
outputs="text",
title="Gemma 2B - QazPerry Demo",
description="A text-generation demo using the fine-tuned Gemma 2B model for Kazakh.",
)
if __name__ == "__main__":
interface.launch()