DsTk / app.py
YchKhan's picture
Update app.py
c1a79d1
import gradio as gr
import pandas as pd
import numpy as np
from sklearn.manifold import TSNE
from sentence_transformers import SentenceTransformer, util
import torch
import plotly.express as px
embedder = SentenceTransformer('all-MiniLM-L6-v2')
df = pd.DataFrame(columns=['Idea', 'Author', 'Embeddings'])
def add_idea(user, idea, history, df):
embedded_idea = embedder.encode(idea, convert_to_tensor=False)
new_row = {'Idea': idea, 'Author': user, 'Embeddings': embedded_idea}
df.loc[len(df)] = new_row
print(len(df))
print(df)
history += "\n Idea : {} \n".format(idea)
return f"{history}\n", df
def map_ideas(df):
emb_list = np.array(list(df.Embeddings))
emb2D2 = TSNE(n_components=2, random_state = 42, perplexity=3).fit_transform(emb_list)
df["x"], df["y"] = [emb2D2[:, 0].tolist(), emb2D2[:, 1].tolist()]
print(df)
fig = px.scatter(
df, x='x', y='y',
title='Description samples map',
color='Author',
opacity=0.7,
text='Idea')
fig.update_traces(marker=dict(symbol='square', size=19), textfont=dict(size=10))
fig.update_layout(hovermode=False)
return fig
with gr.Blocks() as demo:
gr.Markdown("Suggest your ideas")
username = gr.Textbox(label='Name')
idea = gr.Textbox(label='Idea')
df_state = gr.State(value=df)
with gr.Row():
btn_idea = gr.Button("Send Idea")
btn_map = gr.Button("Map Ideas")
history = gr.Markdown(label="Your submitted ideas")
scattered = gr.Plot().style()
btn_idea.click(add_idea, inputs=[username, idea, history, df_state], outputs=[history, df_state])
btn_map.click(map_ideas,inputs=df_state, outputs=scattered)
demo.queue(concurrency_count=10)
demo.launch(debug=False,share=False)