LR36 commited on
Commit
e13c9cc
·
verified ·
1 Parent(s): 3689c5f

Update model_utils.py

Browse files
Files changed (1) hide show
  1. model_utils.py +72 -67
model_utils.py CHANGED
@@ -1,93 +1,98 @@
1
- from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
2
- import torch
3
 
4
- # Predetermined answers for common questions
5
  PREDETERMINED_ANSWERS = {
6
- "what is negative reinforcement": """
7
  Negative reinforcement in ABA refers to the process where a behavior is strengthened by removing an aversive stimulus immediately after the behavior occurs.
8
 
9
- Example: A child whines in the car until the parent turns off loud music. If the whining increases in the future when loud music plays, the whining was negatively reinforced by removal of the aversive sound.
10
 
11
  Key points:
12
  - Removes something unpleasant
13
- - Strengthens the behavior that caused the removal
14
- - Different from punishment which decreases behavior
15
  """,
16
 
17
- "what is positive reinforcement": """
18
- Positive reinforcement occurs when a behavior is followed by the addition of a rewarding stimulus, increasing the likelihood of that behavior occurring again.
19
 
20
- Example: A child says "please" and receives a sticker. If saying "please" increases, the behavior was positively reinforced.
21
 
22
  Key characteristics:
23
  - Adds something desirable
24
- - Must be contingent on the behavior
25
  - Most effective when immediate
26
- - Should be individualized to the learner
27
  """,
28
 
29
- "what is aba": """
30
- Applied Behavior Analysis (ABA) is a scientific approach to understanding behavior and how it is affected by the environment. ABA focuses on:
31
 
32
- 1. Applying learning principles to increase useful behaviors
33
- 2. Reducing behaviors that may interfere with learning
34
- 3. Measuring behavior change objectively
 
35
 
36
- Key components:
37
- - Data-driven decision making
38
- - Individualized interventions
39
- - Focus on socially significant behaviors
40
- - Based on decades of empirical research
41
 
42
- ABA is considered the gold standard treatment for autism spectrum disorder.
 
 
43
  """
44
  }
45
 
46
- PROMPT_TEMPLATE = """As a BCBA expert, provide a detailed response:
47
- Input: {question}
48
- Guidelines:
49
- 1. Use ABA terminology correctly
50
- 2. Cite evidence where possible
51
- 3. Suggest practical interventions
52
- Response:"""
 
 
 
 
53
 
54
- def load_model():
55
- try:
56
- model_name = "google/flan-t5-large"
57
- tokenizer = AutoTokenizer.from_pretrained(model_name)
58
- model = AutoModelForSeq2SeqLM.from_pretrained(
59
- model_name,
60
- device_map="auto",
61
- torch_dtype=torch.float16
 
 
 
 
62
  )
63
- return tokenizer, model
64
- except Exception as e:
65
- print(f"Model loading failed: {e}")
66
- return None, None
67
-
68
- def generate_response(question, tokenizer, model):
69
- # Check for predetermined answers first
70
- lower_question = question.strip().lower()
71
- if lower_question in PREDETERMINED_ANSWERS:
72
- return PREDETERMINED_ANSWERS[lower_question]
73
 
74
- # If model is available, use it
75
- if tokenizer and model:
76
- try:
77
- prompt = PROMPT_TEMPLATE.format(question=question)
78
- inputs = tokenizer(prompt, return_tensors="pt", truncation=True).to(model.device)
79
-
80
- outputs = model.generate(
81
- **inputs,
82
- max_new_tokens=250,
83
- temperature=0.7,
84
- num_beams=3,
85
- top_p=0.9,
86
- early_stopping=True
87
- )
88
- return tokenizer.decode(outputs[0], skip_special_tokens=True)
89
- except Exception as e:
90
- print(f"Model generation failed: {e}")
91
 
92
- # Fallback response if model fails
93
- return "I'm unable to generate a response right now. Here are some common ABA terms I can explain:\n- What is ABA?\n- What is negative reinforcement?\n- What is positive reinforcement?\n\nPlease ask about one of these or try again later."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
 
2
 
3
+ # All predetermined answers (now inside app.py for simplicity)
4
  PREDETERMINED_ANSWERS = {
5
+ "negative reinforcement": """
6
  Negative reinforcement in ABA refers to the process where a behavior is strengthened by removing an aversive stimulus immediately after the behavior occurs.
7
 
8
+ Example: A child whines in the car until the parent turns off loud music. If whining increases in the future when music plays, the behavior was negatively reinforced.
9
 
10
  Key points:
11
  - Removes something unpleasant
12
+ - Strengthens the behavior
13
+ - Different from punishment
14
  """,
15
 
16
+ "positive reinforcement": """
17
+ Positive reinforcement occurs when a behavior is followed by a rewarding stimulus, increasing the likelihood of that behavior recurring.
18
 
19
+ Example: A child says "please" and receives praise. If "please" is used more often, the behavior was positively reinforced.
20
 
21
  Key characteristics:
22
  - Adds something desirable
23
+ - Must be contingent on behavior
24
  - Most effective when immediate
 
25
  """,
26
 
27
+ "aba": """
28
+ Applied Behavior Analysis (ABA) is a scientific approach to understanding behavior. Key aspects:
29
 
30
+ 1. Increases useful behaviors
31
+ 2. Reduces interfering behaviors
32
+ 3. Uses data-driven decisions
33
+ 4. Individualized interventions
34
 
35
+ Considered gold-standard for autism treatment.
36
+ """,
37
+
38
+ "differential reinforcement": """
39
+ Differential reinforcement reinforces specific behaviors while withholding reinforcement for others. Types include:
40
 
41
+ - DRA: Reinforcing alternative behaviors
42
+ - DRO: Reinforcing when problem behavior is absent
43
+ - DRI: Reinforcing incompatible behaviors
44
  """
45
  }
46
 
47
+ def get_answer(question):
48
+ """Check for predetermined answers first, with flexible matching"""
49
+ question_lower = question.lower().strip()
50
+
51
+ # Check if any predefined question is contained in the user's question
52
+ for key in PREDETERMINED_ANSWERS:
53
+ if key in question_lower:
54
+ return PREDETERMINED_ANSWERS[key]
55
+
56
+ # Fallback for unknown questions
57
+ return "I can answer questions about: " + ", ".join([k for k in PREDETERMINED_ANSWERS.keys()])
58
 
59
+ # Gradio interface
60
+ with gr.Blocks() as demo:
61
+ gr.Markdown("""
62
+ # ABA Therapy Assistant
63
+ Get instant answers about Applied Behavior Analysis
64
+ """)
65
+
66
+ with gr.Row():
67
+ question = gr.Textbox(
68
+ label="Ask about ABA",
69
+ placeholder="e.g. What is negative reinforcement?",
70
+ lines=2
71
  )
72
+ submit_btn = gr.Button("Get Answer", variant="primary")
 
 
 
 
 
 
 
 
 
73
 
74
+ answer = gr.Textbox(
75
+ label="Answer",
76
+ interactive=False,
77
+ lines=6,
78
+ elem_classes=["answer-box"]
79
+ )
 
 
 
 
 
 
 
 
 
 
 
80
 
81
+ # Add examples (but not as clickable suggestions)
82
+ gr.Markdown("### Common Questions:")
83
+ gr.Examples(
84
+ examples=[
85
+ "What is negative reinforcement?",
86
+ "Explain positive reinforcement",
87
+ "Tell me about ABA",
88
+ "What's differential reinforcement?"
89
+ ],
90
+ inputs=question,
91
+ label="Try asking:",
92
+ fn=get_answer,
93
+ outputs=answer
94
+ )
95
+
96
+ submit_btn.click(get_answer, inputs=question, outputs=answer)
97
+
98
+ demo.launch()