File size: 2,198 Bytes
b0e6d60 4460d3d 553c8c4 c60d44f 553c8c4 b0e39c2 553c8c4 b0e39c2 553c8c4 80b5781 29eca75 553c8c4 29eca75 d154bbf 29eca75 dc47816 17b5cd4 c60d44f 80b5781 c60d44f 80b5781 553c8c4 d154bbf 17b5cd4 d154bbf 553c8c4 17b5cd4 553c8c4 4460d3d 80b5781 c60d44f 80b5781 17b5cd4 4460d3d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
import gradio as gr
import torch
from peft import PeftModel, PeftConfig
from transformers import AutoModelForCausalLM, AutoTokenizer
# Repos
BASE_MODEL = "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B"
ADAPTER_REPO = "cheberle/autotrain-35swc-b4r9z"
# 1. Load the PEFT config to confirm the base model
peft_config = PeftConfig.from_pretrained(ADAPTER_REPO)
print("PEFT Base Model:", peft_config.base_model_name_or_path)
# 2. Load the tokenizer & base model
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, trust_remote_code=True, language='de')
base_model = AutoModelForCausalLM.from_pretrained(
BASE_MODEL,
revision="4831ee1375be5b4ff5a4abf7984e13628db44e35",
ignore_mismatched_sizes=True,
trust_remote_code=True,
device_map="auto",
)
# 3. Load your LoRA adapter weights onto the base model
model = PeftModel.from_pretrained(
base_model,
ADAPTER_REPO,
ignore_mismatched_sizes=True,
)
def extract_food_term(text):
"""
Extract or simplify a German food term to a single word or best descriptor.
"""
prompt = f"Extrahiere das beste ein Wort oder den Begriff, der dieses Nahrungsmittel beschreibt:\nInput: {text}\nOutput:"
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=16, # Limit output to a single word
temperature=0.0, # Deterministic output
top_p=1.0, # Focus on the most likely word
do_sample=False, # Disable sampling
)
answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
# Extract only the generated output
answer = answer.split("Output:")[1].strip()
return answer
with gr.Blocks() as demo:
gr.Markdown("## Qwen + LoRA Adapter: Lebensmittelbegriffserkennung Demo")
input_box = gr.Textbox(lines=1, label="Geben Sie ein Nahrungsmittel ein (z.B., 'Blaubeertorte')")
output_box = gr.Textbox(lines=1, label="Beste ein Wort-Beschreibung")
extract_btn = gr.Button("Begriff extrahieren")
extract_btn.click(fn=extract_food_term, inputs=input_box, outputs=output_box)
if __name__ == "__main__":
demo.launch() |