-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenerate_visualizations.py
More file actions
83 lines (61 loc) · 2.39 KB
/
Copy pathgenerate_visualizations.py
File metadata and controls
83 lines (61 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import os
import json
import numpy as np
import pandas as pd
import hdbscan
from openai import OpenAI
from sentence_transformers import SentenceTransformer
import ssl
import visualizer
from flask import Flask, request, jsonify, Response
import textrazor
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
# Code meant for future use to get topics
# def generate_topic_list(chunks):
# # Bypass SSL certificate verification
# ssl._create_default_https_context = ssl._create_unverified_context
# textrazor_api_key = os.getenv("TEXT_RAZOR_API_KEY")
# textrazor.api_key = textrazor_api_key
# chunk_topic_list = []
# for i in range(len(chunks)):
# client = textrazor.TextRazor(extractors=["entities", "topics"])
# response = client.analyze(chunks[i])
# count = 0
# topics = []
# for topic in response.topics():
# topics.append(topic.label)
# count += 1
# if count == 10:
# break
# chunk_topic_list.append(topics)
# return chunk_topic_list
def generate_like_articles_2(chunks):
from sklearn.cluster import KMeans
local_model = SentenceTransformer("./model_folder", trust_remote_code=True, device="cpu", config_kwargs={"use_memory_efficient_attention": False, "unpad_inputs": False})
embeddings = local_model.encode(chunks)
similarities = local_model.similarity(embeddings, embeddings)
kmeans = KMeans(n_clusters=2, random_state=0)
kmeans.fit(embeddings)
labels = kmeans.labels_
centroids = kmeans.cluster_centers_
df = pd.DataFrame(list(zip(chunks, labels)), columns=['Chunks', 'Labels'])
result = df.groupby('Labels')['Chunks'].agg(' '.join).reset_index()
results = visualizer.get_visualizations_json(result['Chunks'].to_list())
return results
@app.route('/execute', methods=['POST'])
def execute():
data = request.get_json() # Receive JSON input
if not isinstance(data, list):
return jsonify({"error": "Invalid input format. Expecting a list."}), 400
# topics_list = generate_topic_list(data)
# Bypass SSL certificate verification
ssl._create_default_https_context = ssl._create_unverified_context
results = generate_like_articles_2(data)
return Response(
json.dumps(results, ensure_ascii=False),
content_type="application/json"
)
if __name__ == '__main__':
app.run()