CannaTech commited on
Commit
2857734
·
verified ·
1 Parent(s): 23ceb7d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -28
app.py CHANGED
@@ -1,42 +1,46 @@
1
  import gradio as gr
2
- from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline
3
 
4
- # 1. Choose a bilingual or multilingual QA model
5
- MODEL_NAME = "deepset/xlm-roberta-large-squad2"
6
 
7
- # 2. Load model + tokenizer
8
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
9
- model = AutoModelForQuestionAnswering.from_pretrained(MODEL_NAME)
10
 
11
- # 3. Initialize QA pipeline
12
- qa_pipeline = pipeline("question-answering", model=model, tokenizer=tokenizer)
13
 
14
- # 4. Load or define custom knowledge base
15
- with open("knowledge.txt", "r", encoding="utf-8") as f:
16
- knowledge_text = f.read()
17
-
18
- # 5. Define function to answer questions
19
- def answer_question(question):
20
- if not question.strip():
21
- return "Please ask a valid question."
22
- try:
23
- result = qa_pipeline(question=question, context=knowledge_text)
24
- return result["answer"]
25
- except Exception as e:
26
- return f"Error: {str(e)}"
 
 
 
 
 
27
 
28
- # 6. Build Gradio interface
29
  iface = gr.Interface(
30
- fn=answer_question,
31
- inputs=gr.Textbox(lines=2, placeholder="Enter your question here..."),
32
- outputs="text",
33
- title="Budtender LLM (Bilingual QA)",
34
  description=(
35
- "A bilingual Q&A model trained on Spanish and English data. "
36
- "Ask your cannabis-related questions here!"
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()