Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -68,7 +68,6 @@ for name, filename in model_files.items():
|
|
| 68 |
except:
|
| 69 |
print(f"⚠️ Skipping {name}")
|
| 70 |
|
| 71 |
-
# --- 3. PREDICTION FUNCTION ---
|
| 72 |
# --- 3. PREDICTION FUNCTION ---
|
| 73 |
def predict(text, model_name):
|
| 74 |
if not text: return "Please enter text", {}, []
|
|
@@ -81,9 +80,10 @@ def predict(text, model_name):
|
|
| 81 |
vectors_reduced = svd.transform(vectors) # SVD Matrix (Dense)
|
| 82 |
model = models[model_name]
|
| 83 |
|
| 84 |
-
# ---
|
| 85 |
-
# We look at the TF-IDF vector
|
| 86 |
feature_array = np.array(vectorizer.get_feature_names_out())
|
|
|
|
| 87 |
tfidf_sorting = np.argsort(vectors.toarray()).flatten()[::-1]
|
| 88 |
|
| 89 |
# Get top 10 words that actually have a score > 0
|
|
@@ -104,16 +104,16 @@ def predict(text, model_name):
|
|
| 104 |
top_label = LABELS[pred_idx]
|
| 105 |
confidences = {LABELS[pred_idx]: 1.0}
|
| 106 |
|
|
|
|
| 107 |
return top_label, confidences, keywords
|
| 108 |
|
| 109 |
except Exception as e:
|
| 110 |
return f"Error: {str(e)}", {}, []
|
| 111 |
-
|
| 112 |
# --- 4. LAUNCH ---
|
| 113 |
# IMPORTANT: allowed_origins="*" fixes the 405 error
|
| 114 |
demo = gr.Interface(
|
| 115 |
fn=predict,
|
| 116 |
inputs=[gr.Textbox(), gr.Dropdown(choices=list(models.keys()))],
|
| 117 |
-
outputs=[gr.Label(), gr.Label()]
|
| 118 |
)
|
| 119 |
demo.launch()
|
|
|
|
| 68 |
except:
|
| 69 |
print(f"⚠️ Skipping {name}")
|
| 70 |
|
|
|
|
| 71 |
# --- 3. PREDICTION FUNCTION ---
|
| 72 |
def predict(text, model_name):
|
| 73 |
if not text: return "Please enter text", {}, []
|
|
|
|
| 80 |
vectors_reduced = svd.transform(vectors) # SVD Matrix (Dense)
|
| 81 |
model = models[model_name]
|
| 82 |
|
| 83 |
+
# --- EXTRACT KEYWORDS ---
|
| 84 |
+
# We look at the TF-IDF vector to find the strongest words
|
| 85 |
feature_array = np.array(vectorizer.get_feature_names_out())
|
| 86 |
+
# Sort by score (descending)
|
| 87 |
tfidf_sorting = np.argsort(vectors.toarray()).flatten()[::-1]
|
| 88 |
|
| 89 |
# Get top 10 words that actually have a score > 0
|
|
|
|
| 104 |
top_label = LABELS[pred_idx]
|
| 105 |
confidences = {LABELS[pred_idx]: 1.0}
|
| 106 |
|
| 107 |
+
# Return 3 items: Label, Confidences, Keywords List
|
| 108 |
return top_label, confidences, keywords
|
| 109 |
|
| 110 |
except Exception as e:
|
| 111 |
return f"Error: {str(e)}", {}, []
|
|
|
|
| 112 |
# --- 4. LAUNCH ---
|
| 113 |
# IMPORTANT: allowed_origins="*" fixes the 405 error
|
| 114 |
demo = gr.Interface(
|
| 115 |
fn=predict,
|
| 116 |
inputs=[gr.Textbox(), gr.Dropdown(choices=list(models.keys()))],
|
| 117 |
+
outputs=[gr.Label(), gr.Label(), gr.JSON()]
|
| 118 |
)
|
| 119 |
demo.launch()
|