Spaces:
Build error
Build error
Update model_utils.py
Browse files- model_utils.py +72 -67
model_utils.py
CHANGED
|
@@ -1,93 +1,98 @@
|
|
| 1 |
-
|
| 2 |
-
import torch
|
| 3 |
|
| 4 |
-
#
|
| 5 |
PREDETERMINED_ANSWERS = {
|
| 6 |
-
"
|
| 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
|
| 10 |
|
| 11 |
Key points:
|
| 12 |
- Removes something unpleasant
|
| 13 |
-
- Strengthens the behavior
|
| 14 |
-
- Different from punishment
|
| 15 |
""",
|
| 16 |
|
| 17 |
-
"
|
| 18 |
-
Positive reinforcement occurs when a behavior is followed by
|
| 19 |
|
| 20 |
-
Example: A child says "please" and receives
|
| 21 |
|
| 22 |
Key characteristics:
|
| 23 |
- Adds something desirable
|
| 24 |
-
- Must be contingent on
|
| 25 |
- Most effective when immediate
|
| 26 |
-
- Should be individualized to the learner
|
| 27 |
""",
|
| 28 |
|
| 29 |
-
"
|
| 30 |
-
Applied Behavior Analysis (ABA) is a scientific approach to understanding behavior
|
| 31 |
|
| 32 |
-
1.
|
| 33 |
-
2.
|
| 34 |
-
3.
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
| 43 |
"""
|
| 44 |
}
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
)
|
| 63 |
-
|
| 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 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 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 |
-
#
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|