Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,8 +8,11 @@ model_name = "mjpsm/Positive-Affirmations-Model"
|
|
8 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
10 |
|
11 |
-
#
|
12 |
-
def generate_affirmation(
|
|
|
|
|
|
|
13 |
inputs = tokenizer(prompt, return_tensors="pt")
|
14 |
input_ids = inputs["input_ids"]
|
15 |
|
@@ -26,12 +29,16 @@ def generate_affirmation(prompt):
|
|
26 |
|
27 |
full_output = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
28 |
|
29 |
-
#
|
30 |
match = re.search(r"\[AFFIRMATION\](.*?)\[/AFFIRMATION\]", full_output, re.DOTALL)
|
31 |
if match:
|
32 |
return match.group(1).strip()
|
33 |
else:
|
34 |
-
|
|
|
|
|
|
|
|
|
35 |
|
36 |
# Gradio interface
|
37 |
demo = gr.Interface(
|
@@ -39,7 +46,7 @@ demo = gr.Interface(
|
|
39 |
inputs=gr.Textbox(label="Describe the player situation (e.g., 'struggled with algebra')"),
|
40 |
outputs=gr.Textbox(label="AI Affirmation"),
|
41 |
title="Positive Affirmation Generator",
|
42 |
-
description="Describe a learning moment, and
|
43 |
)
|
44 |
|
45 |
if __name__ == "__main__":
|
@@ -48,3 +55,5 @@ if __name__ == "__main__":
|
|
48 |
|
49 |
|
50 |
|
|
|
|
|
|
8 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
10 |
|
11 |
+
# Generation function
|
12 |
+
def generate_affirmation(description):
|
13 |
+
# Structured prompt to guide model output
|
14 |
+
prompt = f"[SUBJECT] learning [/SUBJECT] [STREAK] current performance context [/STREAK] [CONTEXT] {description} [/CONTEXT] [AFFIRMATION]"
|
15 |
+
|
16 |
inputs = tokenizer(prompt, return_tensors="pt")
|
17 |
input_ids = inputs["input_ids"]
|
18 |
|
|
|
29 |
|
30 |
full_output = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
31 |
|
32 |
+
# Try to extract text between [AFFIRMATION] and [/AFFIRMATION]
|
33 |
match = re.search(r"\[AFFIRMATION\](.*?)\[/AFFIRMATION\]", full_output, re.DOTALL)
|
34 |
if match:
|
35 |
return match.group(1).strip()
|
36 |
else:
|
37 |
+
# Fallback: try to extract everything after [AFFIRMATION]
|
38 |
+
fallback_match = re.search(r"\[AFFIRMATION\](.*)", full_output, re.DOTALL)
|
39 |
+
if fallback_match:
|
40 |
+
return fallback_match.group(1).strip()
|
41 |
+
return "⚠️ No affirmation found in the response."
|
42 |
|
43 |
# Gradio interface
|
44 |
demo = gr.Interface(
|
|
|
46 |
inputs=gr.Textbox(label="Describe the player situation (e.g., 'struggled with algebra')"),
|
47 |
outputs=gr.Textbox(label="AI Affirmation"),
|
48 |
title="Positive Affirmation Generator",
|
49 |
+
description="Describe a learning moment, and receive an uplifting affirmation generated by AI."
|
50 |
)
|
51 |
|
52 |
if __name__ == "__main__":
|
|
|
55 |
|
56 |
|
57 |
|
58 |
+
|
59 |
+
|