Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files- .gitattributes +2 -0
- README.md +2 -8
- app.py +136 -0
- bg_music.mp3 +3 -0
- campus_bg.png +3 -0
- requirements.txt +6 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
bg_music.mp3 filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
campus_bg.png filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
|
@@ -1,12 +1,6 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
|
| 4 |
-
colorFrom: green
|
| 5 |
-
colorTo: indigo
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.45.0
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
---
|
| 11 |
-
|
| 12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: attachment-style-game
|
| 3 |
+
app_file: app.py
|
|
|
|
|
|
|
| 4 |
sdk: gradio
|
| 5 |
sdk_version: 5.45.0
|
|
|
|
|
|
|
| 6 |
---
|
|
|
|
|
|
app.py
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from datasets import load_dataset
|
| 4 |
+
from huggingface_hub import InferenceClient
|
| 5 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 6 |
+
from langchain_community.vectorstores import Chroma
|
| 7 |
+
|
| 8 |
+
# 1. Connect to Hugging Face Inference API (free hosted models)
|
| 9 |
+
# You MUST add your Hugging Face token in the Space "Secrets" panel as HF_TOKEN
|
| 10 |
+
client = InferenceClient("microsoft/phi-2", token=None) # if hosted on your HF account, no token needed
|
| 11 |
+
|
| 12 |
+
def generate_text(prompt, max_tokens=150):
|
| 13 |
+
"""Call Hugging Face Inference API to generate text."""
|
| 14 |
+
response = client.text_generation(prompt, max_new_tokens=max_tokens)
|
| 15 |
+
return response
|
| 16 |
+
|
| 17 |
+
# 2. Load dataset for RAG
|
| 18 |
+
data = load_dataset("Amod/mental_health_counseling_conversations")
|
| 19 |
+
docs = [f"Q: {entry['Context']}\nA: {entry['Response']}" for entry in data['train']]
|
| 20 |
+
embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 21 |
+
vectordb = Chroma.from_texts(texts=docs, embedding=embedding_model)
|
| 22 |
+
|
| 23 |
+
# 3. Traits dictionary
|
| 24 |
+
traits = {"Secure": 0, "Anxious": 0, "Avoidant": 0}
|
| 25 |
+
MAX_SCENES = 5
|
| 26 |
+
|
| 27 |
+
# Narrative instructions for LLM
|
| 28 |
+
narrator_instructions = (
|
| 29 |
+
"You are a supportive narrative AI creating an interactive story for the player.\n"
|
| 30 |
+
"Write in second person (\"you\" as Alex) and describe feelings and events vividly.\n"
|
| 31 |
+
"The story should feel emotionally deep and realistic, suitable for an adult audience (age 25-35).\n"
|
| 32 |
+
"Focus on how attachment styles influence Alex's feelings and decisions in a romantic relationship.\n"
|
| 33 |
+
"In each scene, after narrating the situation, offer the player choices labeled A), B), (and possibly C)).\n"
|
| 34 |
+
"Each choice should represent a different attachment response (secure, avoidant, anxious).\n"
|
| 35 |
+
"Do NOT reveal the outcome of the choices yet; just present the options.\n"
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# --- STORY FUNCTIONS ---
|
| 39 |
+
def parse_choices(text):
|
| 40 |
+
"""Split LLM output into narrative and choices."""
|
| 41 |
+
choice_start = text.find("A)")
|
| 42 |
+
if choice_start == -1:
|
| 43 |
+
return text, []
|
| 44 |
+
narrative = text[:choice_start].strip()
|
| 45 |
+
choice_lines = text[choice_start:].splitlines()
|
| 46 |
+
choices = [line.strip() for line in choice_lines if re.match(r"^[A-C]\)", line.strip())]
|
| 47 |
+
return narrative, choices
|
| 48 |
+
|
| 49 |
+
def generate_initial_scene():
|
| 50 |
+
intro = (
|
| 51 |
+
"Alex is a 26-year-old graduate student who recently moved to the UK. "
|
| 52 |
+
"They are in a romantic relationship, but lately Alex feels insecure. "
|
| 53 |
+
"One morning, Alex wakes up with a knot in their stomach. "
|
| 54 |
+
"Their partner didnβt reply to a message from last night. "
|
| 55 |
+
"Thoughts race through Alexβs mind..."
|
| 56 |
+
)
|
| 57 |
+
text = generate_text(narrator_instructions + intro + "\nWhat does Alex do?")
|
| 58 |
+
return parse_choices(text)
|
| 59 |
+
|
| 60 |
+
def next_step(selected_choice, story_so_far, scene_index, traits_dict):
|
| 61 |
+
if not selected_choice:
|
| 62 |
+
return story_so_far, [], scene_index, traits_dict
|
| 63 |
+
|
| 64 |
+
choice_label = selected_choice[0]
|
| 65 |
+
choice_text = selected_choice[3:].strip()
|
| 66 |
+
scene_index += 1
|
| 67 |
+
|
| 68 |
+
# Update traits
|
| 69 |
+
text_lower = choice_text.lower()
|
| 70 |
+
if any(word in text_lower for word in ["talk", "share", "open", "honest", "calm"]):
|
| 71 |
+
traits_dict["Secure"] += 1
|
| 72 |
+
if any(word in text_lower for word in ["ignore", "avoid", "distance", "later", "alone"]):
|
| 73 |
+
traits_dict["Avoidant"] += 1
|
| 74 |
+
if any(word in text_lower for word in ["worry", "jealous", "clingy", "panic", "angry", "upset"]):
|
| 75 |
+
traits_dict["Anxious"] += 1
|
| 76 |
+
|
| 77 |
+
# Retrieve advice
|
| 78 |
+
results = vectordb.similarity_search(choice_text + " relationship anxiety", k=1)
|
| 79 |
+
advice = results[0].page_content.split("A:")[1].strip() if results else ""
|
| 80 |
+
|
| 81 |
+
# Prompt for next scene
|
| 82 |
+
last_line = story_so_far.splitlines()[-1] if story_so_far else ""
|
| 83 |
+
prompt = narrator_instructions
|
| 84 |
+
prompt += f"Previously: {last_line}\n"
|
| 85 |
+
prompt += f"The player chose option {choice_label}: {choice_text}.\n"
|
| 86 |
+
if advice:
|
| 87 |
+
prompt += f"[Helpful thought: {advice}]\n"
|
| 88 |
+
prompt += "Continue the story and provide new choices.\n"
|
| 89 |
+
|
| 90 |
+
text = generate_text(prompt)
|
| 91 |
+
return *parse_choices(text), scene_index, traits_dict
|
| 92 |
+
|
| 93 |
+
def summarize_story(story, traits_dict):
|
| 94 |
+
prompt = (
|
| 95 |
+
f"Alex's story is ending. Traits: Secure={traits_dict['Secure']}, "
|
| 96 |
+
f"Anxious={traits_dict['Anxious']}, Avoidant={traits_dict['Avoidant']}.\n"
|
| 97 |
+
"Write a reflective ending about what Alex learned about relationships."
|
| 98 |
+
)
|
| 99 |
+
return generate_text(prompt, max_tokens=120)
|
| 100 |
+
|
| 101 |
+
# --- GRADIO UI ---
|
| 102 |
+
with gr.Blocks(css=".gradio-container {background-color:#f5f7fa;} #story_box {padding:15px; background:#fff; border-radius:10px;}") as demo:
|
| 103 |
+
story_state = gr.State("")
|
| 104 |
+
scene_state = gr.State(0)
|
| 105 |
+
traits_state = gr.State(traits.copy())
|
| 106 |
+
|
| 107 |
+
gr.HTML("<h1>π Attachment Style Interactive Story</h1><p>Explore how attachment styles shape relationships.</p>")
|
| 108 |
+
|
| 109 |
+
# Background music
|
| 110 |
+
gr.HTML("""
|
| 111 |
+
<audio src='file=bg_music.mp3' autoplay loop hidden></audio>
|
| 112 |
+
""")
|
| 113 |
+
|
| 114 |
+
story_box = gr.Markdown("*(Loading story...)*", elem_id="story_box")
|
| 115 |
+
choices_radio = gr.Radio(label="Choose:", choices=[], visible=False)
|
| 116 |
+
next_btn = gr.Button("Next")
|
| 117 |
+
|
| 118 |
+
def start_game():
|
| 119 |
+
narrative, choices = generate_initial_scene()
|
| 120 |
+
return narrative, choices, 1, {"Secure":0,"Anxious":0,"Avoidant":0}
|
| 121 |
+
|
| 122 |
+
def play_step(choice, story, scene, traits_dict):
|
| 123 |
+
if scene >= MAX_SCENES:
|
| 124 |
+
return story + "\n\n" + summarize_story(story, traits_dict), [], scene, traits_dict
|
| 125 |
+
narrative, choices, new_scene, traits_dict = next_step(choice, story, scene, traits_dict)
|
| 126 |
+
story += "\n\n" + narrative
|
| 127 |
+
if new_scene >= MAX_SCENES:
|
| 128 |
+
story += "\n\n" + summarize_story(story, traits_dict)
|
| 129 |
+
return story, [], new_scene, traits_dict
|
| 130 |
+
return story, choices, new_scene, traits_dict
|
| 131 |
+
|
| 132 |
+
demo.load(start_game, inputs=None, outputs=[story_box, choices_radio, scene_state, traits_state])
|
| 133 |
+
next_btn.click(play_step, inputs=[choices_radio, story_state, scene_state, traits_state],
|
| 134 |
+
outputs=[story_box, choices_radio, scene_state, traits_state])
|
| 135 |
+
|
| 136 |
+
demo.launch()
|
bg_music.mp3
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a3eb4a7dfec8d62ed6bc9dde06a87ca4e8b225ccfa4031343a1febb85d2f47f1
|
| 3 |
+
size 4289097
|
campus_bg.png
ADDED
|
Git LFS Details
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
datasets
|
| 3 |
+
gradio
|
| 4 |
+
langchain
|
| 5 |
+
langchain-community
|
| 6 |
+
sentence-transformers
|