mjpsm commited on
Commit
5972db7
·
verified ·
1 Parent(s): 9dd2063

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -10
app.py CHANGED
@@ -1,13 +1,14 @@
1
  import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import torch
 
4
 
5
  # Load model and tokenizer from Hugging Face Hub
6
  model_name = "mjpsm/Positive-Affirmations-Model"
7
  tokenizer = AutoTokenizer.from_pretrained(model_name)
8
  model = AutoModelForCausalLM.from_pretrained(model_name)
9
 
10
- # Generation function
11
  def generate_affirmation(prompt):
12
  inputs = tokenizer(prompt, return_tensors="pt")
13
  input_ids = inputs["input_ids"]
@@ -23,17 +24,14 @@ def generate_affirmation(prompt):
23
  pad_token_id=tokenizer.eos_token_id
24
  )
25
 
26
- # Decode full output
27
  full_output = tokenizer.decode(outputs[0], skip_special_tokens=True)
28
 
29
- # Remove the input prompt from the output to isolate the generated part
30
- if full_output.startswith(prompt):
31
- affirmation = full_output[len(prompt):].strip()
 
32
  else:
33
- affirmation = full_output.strip()
34
-
35
- return affirmation
36
-
37
 
38
  # Gradio interface
39
  demo = gr.Interface(
@@ -41,9 +39,11 @@ demo = gr.Interface(
41
  inputs=gr.Textbox(label="Describe the player situation (e.g., 'struggled with algebra')"),
42
  outputs=gr.Textbox(label="AI Affirmation"),
43
  title="Positive Affirmation Generator",
44
- description="Describe a learning moment, and the model will generate a motivating affirmation."
45
  )
46
 
47
  if __name__ == "__main__":
48
  demo.launch()
49
 
 
 
 
1
  import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import torch
4
+ import re
5
 
6
  # Load model and tokenizer from Hugging Face Hub
7
  model_name = "mjpsm/Positive-Affirmations-Model"
8
  tokenizer = AutoTokenizer.from_pretrained(model_name)
9
  model = AutoModelForCausalLM.from_pretrained(model_name)
10
 
11
+ # Affirmation generator
12
  def generate_affirmation(prompt):
13
  inputs = tokenizer(prompt, return_tensors="pt")
14
  input_ids = inputs["input_ids"]
 
24
  pad_token_id=tokenizer.eos_token_id
25
  )
26
 
 
27
  full_output = tokenizer.decode(outputs[0], skip_special_tokens=True)
28
 
29
+ # Extract [AFFIRMATION] ... [/AFFIRMATION] only
30
+ match = re.search(r"\[AFFIRMATION\](.*?)\[/AFFIRMATION\]", full_output, re.DOTALL)
31
+ if match:
32
+ return match.group(1).strip()
33
  else:
34
+ return "⚠️ No [AFFIRMATION] section found in the response."
 
 
 
35
 
36
  # Gradio interface
37
  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 receive an uplifting affirmation generated by AI."
43
  )
44
 
45
  if __name__ == "__main__":
46
  demo.launch()
47
 
48
+
49
+