File size: 2,112 Bytes
705f89d
2857734
a500005
2857734
 
a500005
2857734
a500005
2857734
a500005
2857734
 
a500005
2857734
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a500005
2857734
a500005
2857734
 
 
 
a500005
2857734
 
a500005
705f89d
 
 
a500005
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
import gradio as gr
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, pipeline

# Use a modern, instruction-tuned model. FLAN-T5 Base is lightweight enough for free CPU usage.
MODEL_NAME = "google/flan-t5-base"

# Load the tokenizer and model
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)

# Set up the text-to-text generation pipeline
llm_pipeline = pipeline("text2text-generation", model=model, tokenizer=tokenizer)

def budtender_response(user_input: str) -> str:
    """
    This function crafts a prompt to instruct the model to act as BudtenderPro,
    a bilingual, professional budtender for adult and medical cannabis customers in New Mexico.
    """
    # Construct a detailed system prompt for context.
    prompt = (
        "You are BudtenderPro, a friendly, knowledgeable, and culturally sensitive budtender for "
        "both adult and medical cannabis customers in New Mexico. You are fluent in both English and Spanish. "
        "You provide professional advice on strains, dosages, and product recommendations, keeping in mind legal regulations and community values. "
        "Answer the user's question with clear, respectful, and useful guidance. \n\n"
        f"User Question: {user_input}"
    )
    
    # Generate the response. Adjust parameters (max_length, temperature) as needed.
    outputs = llm_pipeline(prompt, max_length=256, do_sample=True, temperature=0.7)
    answer = outputs[0]['generated_text']
    return answer.strip()

# Set up the Gradio interface
iface = gr.Interface(
    fn=budtender_response,
    inputs=gr.Textbox(lines=3, placeholder="Ask your cannabis-related question here..."),
    outputs=gr.Textbox(label="BudtenderPro Response"),
    title="BudtenderPro LLM Demo",
    description=(
        "A modern, bilingual budtender assistant for adult and medical cannabis questions in New Mexico. "
        "Powered by Google FLAN-T5 Base, this demo leverages an up-to-date Hugging Face workflow to provide professional guidance."
    )
)

if __name__ == "__main__":
    iface.launch()