deepseek / app.py
cheberle's picture
f
d154bbf
raw
history blame
2 kB
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)
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 classify_food(text):
"""
Classify or extract food-related terms from the input text.
"""
prompt = f"Below is some text. Please identify and classify food-related terms.\nText: {text}\nFood classification or extraction:"
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=64,
temperature=0.7, # Adjust temperature for creativity
top_p=0.9, # Adjust top_p for diversity
)
answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
return answer
with gr.Blocks() as demo:
gr.Markdown("## Qwen + LoRA Adapter: Food Classification/Extraction Demo")
input_box = gr.Textbox(lines=3, label="Enter text containing food items")
output_box = gr.Textbox(lines=3, label="Model's classification or extraction output")
classify_btn = gr.Button("Analyze Food Terms")
classify_btn.click(fn=classify_food, inputs=input_box, outputs=output_box)
if __name__ == "__main__":
demo.launch()