Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from sentence_transformers import SentenceTransformer
|
| 3 |
+
from sklearn.cluster import KMeans
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import plotly.express as px
|
| 6 |
+
import umap
|
| 7 |
+
|
| 8 |
+
model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
| 9 |
+
|
| 10 |
+
def analyze_problems(text):
|
| 11 |
+
|
| 12 |
+
problems = [p.strip() for p in text.split("\n") if p.strip()]
|
| 13 |
+
|
| 14 |
+
embeddings = model.encode(problems)
|
| 15 |
+
|
| 16 |
+
# clustering
|
| 17 |
+
k = min(5, len(problems))
|
| 18 |
+
kmeans = KMeans(n_clusters=k, random_state=0).fit(embeddings)
|
| 19 |
+
|
| 20 |
+
clusters = kmeans.labels_
|
| 21 |
+
|
| 22 |
+
# dimensionality reduction for visualization
|
| 23 |
+
reducer = umap.UMAP()
|
| 24 |
+
coords = reducer.fit_transform(embeddings)
|
| 25 |
+
|
| 26 |
+
df = pd.DataFrame({
|
| 27 |
+
"problem": problems,
|
| 28 |
+
"cluster": clusters,
|
| 29 |
+
"x": coords[:,0],
|
| 30 |
+
"y": coords[:,1]
|
| 31 |
+
})
|
| 32 |
+
|
| 33 |
+
fig = px.scatter(
|
| 34 |
+
df,
|
| 35 |
+
x="x",
|
| 36 |
+
y="y",
|
| 37 |
+
color=df["cluster"].astype(str),
|
| 38 |
+
text="problem",
|
| 39 |
+
title="Problem Market Map"
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
cluster_summary = df.groupby("cluster")["problem"].apply(list).to_dict()
|
| 43 |
+
|
| 44 |
+
summary_text = ""
|
| 45 |
+
|
| 46 |
+
for c, items in cluster_summary.items():
|
| 47 |
+
summary_text += f"\nCluster {c}\n"
|
| 48 |
+
for i in items:
|
| 49 |
+
summary_text += f"- {i}\n"
|
| 50 |
+
|
| 51 |
+
return summary_text, fig
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
with open("sample_problems.txt") as f:
|
| 55 |
+
default_text = f.read()
|
| 56 |
+
|
| 57 |
+
demo = gr.Interface(
|
| 58 |
+
fn=analyze_problems,
|
| 59 |
+
inputs=gr.Textbox(value=default_text, lines=15, label="Problem Signals"),
|
| 60 |
+
outputs=[
|
| 61 |
+
gr.Textbox(label="Problem Clusters"),
|
| 62 |
+
gr.Plot(label="Problem Market Map")
|
| 63 |
+
],
|
| 64 |
+
title="Problem Discovery Engine Demo",
|
| 65 |
+
description="Detect clusters of real-world problems and visualize a Problem Market Map."
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
demo.launch()
|