Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,42 +1,46 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import
|
3 |
|
4 |
-
#
|
5 |
-
MODEL_NAME = "
|
6 |
|
7 |
-
#
|
8 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
9 |
-
model =
|
10 |
|
11 |
-
#
|
12 |
-
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
#
|
29 |
iface = gr.Interface(
|
30 |
-
fn=
|
31 |
-
inputs=gr.Textbox(lines=
|
32 |
-
outputs="
|
33 |
-
title="
|
34 |
description=(
|
35 |
-
"A bilingual
|
36 |
-
"
|
37 |
)
|
38 |
)
|
39 |
|
40 |
-
# 7. Launch app
|
41 |
if __name__ == "__main__":
|
42 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, pipeline
|
3 |
|
4 |
+
# Use a modern, instruction-tuned model. FLAN-T5 Base is lightweight enough for free CPU usage.
|
5 |
+
MODEL_NAME = "google/flan-t5-base"
|
6 |
|
7 |
+
# Load the tokenizer and model
|
8 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
9 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
|
10 |
|
11 |
+
# Set up the text-to-text generation pipeline
|
12 |
+
llm_pipeline = pipeline("text2text-generation", model=model, tokenizer=tokenizer)
|
13 |
|
14 |
+
def budtender_response(user_input: str) -> str:
|
15 |
+
"""
|
16 |
+
This function crafts a prompt to instruct the model to act as BudtenderPro,
|
17 |
+
a bilingual, professional budtender for adult and medical cannabis customers in New Mexico.
|
18 |
+
"""
|
19 |
+
# Construct a detailed system prompt for context.
|
20 |
+
prompt = (
|
21 |
+
"You are BudtenderPro, a friendly, knowledgeable, and culturally sensitive budtender for "
|
22 |
+
"both adult and medical cannabis customers in New Mexico. You are fluent in both English and Spanish. "
|
23 |
+
"You provide professional advice on strains, dosages, and product recommendations, keeping in mind legal regulations and community values. "
|
24 |
+
"Answer the user's question with clear, respectful, and useful guidance. \n\n"
|
25 |
+
f"User Question: {user_input}"
|
26 |
+
)
|
27 |
+
|
28 |
+
# Generate the response. Adjust parameters (max_length, temperature) as needed.
|
29 |
+
outputs = llm_pipeline(prompt, max_length=256, do_sample=True, temperature=0.7)
|
30 |
+
answer = outputs[0]['generated_text']
|
31 |
+
return answer.strip()
|
32 |
|
33 |
+
# Set up the Gradio interface
|
34 |
iface = gr.Interface(
|
35 |
+
fn=budtender_response,
|
36 |
+
inputs=gr.Textbox(lines=3, placeholder="Ask your cannabis-related question here..."),
|
37 |
+
outputs=gr.Textbox(label="BudtenderPro Response"),
|
38 |
+
title="BudtenderPro LLM Demo",
|
39 |
description=(
|
40 |
+
"A modern, bilingual budtender assistant for adult and medical cannabis questions in New Mexico. "
|
41 |
+
"Powered by Google FLAN-T5 Base, this demo leverages an up-to-date Hugging Face workflow to provide professional guidance."
|
42 |
)
|
43 |
)
|
44 |
|
|
|
45 |
if __name__ == "__main__":
|
46 |
iface.launch()
|