Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- README.md +9 -6
- app.py +186 -0
- requirements.txt +5 -0
README.md
CHANGED
@@ -1,13 +1,16 @@
|
|
1 |
---
|
2 |
-
title: Ambiance
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
-
sdk_version:
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: apache-2.0
|
11 |
---
|
12 |
|
13 |
-
|
|
|
|
|
|
|
|
1 |
---
|
2 |
+
title: Lighting Ambiance Classifier
|
3 |
+
emoji: π
|
4 |
+
colorFrom: yellow
|
5 |
+
colorTo: green
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 4.0.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: apache-2.0
|
11 |
---
|
12 |
|
13 |
+
# π Lighting Ambiance Classifier
|
14 |
+
|
15 |
+
Classify lighting preferences into bright, cozy, or natural categories.
|
16 |
+
Supports English and Japanese input!
|
app.py
ADDED
@@ -0,0 +1,186 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from sentence_transformers import SentenceTransformer
|
3 |
+
from sentence_transformers.util import cos_sim
|
4 |
+
import torch
|
5 |
+
import logging
|
6 |
+
|
7 |
+
# Configure logging
|
8 |
+
logging.basicConfig(level=logging.INFO)
|
9 |
+
logger = logging.getLogger(__name__)
|
10 |
+
|
11 |
+
# Global model variable
|
12 |
+
model = None
|
13 |
+
|
14 |
+
def load_model():
|
15 |
+
"""Load the sentence transformer model"""
|
16 |
+
global model
|
17 |
+
if model is None:
|
18 |
+
try:
|
19 |
+
logger.info("Loading sentence transformer model...")
|
20 |
+
model = SentenceTransformer('nabil-tazi/autotrain-c0qmd-8o6a3')
|
21 |
+
logger.info("Model loaded successfully!")
|
22 |
+
except Exception as e:
|
23 |
+
logger.error(f"Failed to load model: {e}")
|
24 |
+
raise e
|
25 |
+
return model
|
26 |
+
|
27 |
+
def classify_ambiance(user_input):
|
28 |
+
"""Classify lighting ambiance from user input"""
|
29 |
+
|
30 |
+
if not user_input or not user_input.strip():
|
31 |
+
return "β Please enter some text", {}, ""
|
32 |
+
|
33 |
+
try:
|
34 |
+
# Load model if not already loaded
|
35 |
+
current_model = load_model()
|
36 |
+
|
37 |
+
# Your three reference ambiances
|
38 |
+
references = ["bright", "cozy", "natural"]
|
39 |
+
|
40 |
+
# Get embeddings
|
41 |
+
user_embedding = current_model.encode([user_input.strip()])
|
42 |
+
ref_embeddings = current_model.encode(references)
|
43 |
+
|
44 |
+
# Calculate similarities
|
45 |
+
similarities = cos_sim(user_embedding, ref_embeddings)[0]
|
46 |
+
|
47 |
+
# Get best match
|
48 |
+
best_idx = similarities.argmax()
|
49 |
+
best_ambiance = references[best_idx]
|
50 |
+
confidence = float(similarities[best_idx])
|
51 |
+
|
52 |
+
# Format all scores for debugging
|
53 |
+
all_scores = {ref: round(float(sim), 4) for ref, sim in zip(references, similarities)}
|
54 |
+
|
55 |
+
# Create result with emoji
|
56 |
+
emoji_map = {"bright": "βοΈ", "cozy": "π―οΈ", "natural": "πΏ"}
|
57 |
+
result_text = f"## {emoji_map.get(best_ambiance, 'π‘')} **{best_ambiance.upper()}**\n**Confidence:** {confidence:.3f}"
|
58 |
+
|
59 |
+
# Create confidence bar
|
60 |
+
confidence_bar = f"**Confidence Level:** {'β' * int(confidence * 20)}{'β' * (20 - int(confidence * 20))} {confidence:.1%}"
|
61 |
+
|
62 |
+
logger.info(f"Classified '{user_input}' as '{best_ambiance}' with confidence {confidence:.3f}")
|
63 |
+
|
64 |
+
return result_text, all_scores, confidence_bar
|
65 |
+
|
66 |
+
except Exception as e:
|
67 |
+
error_msg = f"β Error: {str(e)}"
|
68 |
+
logger.error(f"Classification error: {e}")
|
69 |
+
return error_msg, {}, ""
|
70 |
+
|
71 |
+
# Create Gradio interface
|
72 |
+
with gr.Blocks(
|
73 |
+
title="π Lighting Ambiance Classifier",
|
74 |
+
theme=gr.themes.Soft(),
|
75 |
+
css="""
|
76 |
+
.gradio-container {
|
77 |
+
max-width: 800px !important;
|
78 |
+
margin: auto !important;
|
79 |
+
}
|
80 |
+
.result-box {
|
81 |
+
background: linear-gradient(45deg, #f0f0f0, #ffffff);
|
82 |
+
border-radius: 10px;
|
83 |
+
padding: 20px;
|
84 |
+
}
|
85 |
+
"""
|
86 |
+
) as demo:
|
87 |
+
|
88 |
+
# Header
|
89 |
+
gr.Markdown(
|
90 |
+
"""
|
91 |
+
# π Lighting Ambiance Classifier
|
92 |
+
|
93 |
+
**Classify your lighting preferences into three categories:**
|
94 |
+
- βοΈ **Bright**: Well-lit, luminous, clear lighting
|
95 |
+
- π―οΈ **Cozy**: Warm, dim, soft, ambient lighting
|
96 |
+
- πΏ **Natural**: Daylight, sunlight, organic lighting
|
97 |
+
|
98 |
+
**Supports both English and Japanese!** πΊπΈπ―π΅
|
99 |
+
"""
|
100 |
+
)
|
101 |
+
|
102 |
+
with gr.Row():
|
103 |
+
with gr.Column(scale=2):
|
104 |
+
# Input section
|
105 |
+
gr.Markdown("### π¬ Enter your lighting preference:")
|
106 |
+
input_text = gr.Textbox(
|
107 |
+
label="Your lighting preference",
|
108 |
+
placeholder="e.g., 'not bright', 'ζγγγͺγ', 'cozy lighting', 'θͺηΆγͺε
γ欲γγ'",
|
109 |
+
lines=3,
|
110 |
+
max_lines=5
|
111 |
+
)
|
112 |
+
|
113 |
+
with gr.Row():
|
114 |
+
submit_btn = gr.Button("π Classify", variant="primary", size="lg")
|
115 |
+
clear_btn = gr.Button("ποΈ Clear", variant="secondary")
|
116 |
+
|
117 |
+
with gr.Column(scale=2):
|
118 |
+
# Output section
|
119 |
+
gr.Markdown("### π― Classification Result:")
|
120 |
+
result = gr.Markdown(value="Enter text and click classify!", elem_classes=["result-box"])
|
121 |
+
confidence_bar = gr.Markdown(value="")
|
122 |
+
|
123 |
+
# Detailed scores
|
124 |
+
with gr.Row():
|
125 |
+
scores = gr.JSON(label="π Detailed Similarity Scores", visible=True)
|
126 |
+
|
127 |
+
# Example inputs
|
128 |
+
gr.Markdown("### π‘ Try these examples:")
|
129 |
+
with gr.Row():
|
130 |
+
examples = gr.Examples(
|
131 |
+
examples=[
|
132 |
+
["not bright"],
|
133 |
+
["ζγγγͺγ"],
|
134 |
+
["I want cozy lighting"],
|
135 |
+
["θͺηΆγͺε
γ欲γγ"],
|
136 |
+
["make it brighter"],
|
137 |
+
["ζγγγγ"],
|
138 |
+
["romantic atmosphere"],
|
139 |
+
["δ½ζ₯γγγγζγγ"],
|
140 |
+
["candle light"],
|
141 |
+
["ε€ͺι½ε
γΏγγ"],
|
142 |
+
["harsh fluorescent"],
|
143 |
+
["εͺγγη
§ζ"]
|
144 |
+
],
|
145 |
+
inputs=input_text,
|
146 |
+
examples_per_page=6
|
147 |
+
)
|
148 |
+
|
149 |
+
# Footer
|
150 |
+
gr.Markdown(
|
151 |
+
"""
|
152 |
+
---
|
153 |
+
**Model:** Fine-tuned multilingual sentence transformer trained on English-Japanese lighting preference pairs.
|
154 |
+
|
155 |
+
**How it works:** The model compares your input text with the three ambiance categories and returns the most similar one with a confidence score.
|
156 |
+
"""
|
157 |
+
)
|
158 |
+
|
159 |
+
# Event handlers
|
160 |
+
def clear_all():
|
161 |
+
return "", "Enter text and click classify!", {}, ""
|
162 |
+
|
163 |
+
submit_btn.click(
|
164 |
+
fn=classify_ambiance,
|
165 |
+
inputs=input_text,
|
166 |
+
outputs=[result, scores, confidence_bar]
|
167 |
+
)
|
168 |
+
|
169 |
+
input_text.submit(
|
170 |
+
fn=classify_ambiance,
|
171 |
+
inputs=input_text,
|
172 |
+
outputs=[result, scores, confidence_bar]
|
173 |
+
)
|
174 |
+
|
175 |
+
clear_btn.click(
|
176 |
+
fn=clear_all,
|
177 |
+
outputs=[input_text, result, scores, confidence_bar]
|
178 |
+
)
|
179 |
+
|
180 |
+
# Launch the app
|
181 |
+
if __name__ == "__main__":
|
182 |
+
demo.launch(
|
183 |
+
server_name="0.0.0.0",
|
184 |
+
server_port=7860,
|
185 |
+
show_error=True
|
186 |
+
)
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
sentence-transformers>=2.2.0
|
2 |
+
torch
|
3 |
+
gradio>=4.0.0
|
4 |
+
transformers
|
5 |
+
numpy
|