final_project / app.py
cotxetj's picture
Update app.py
2e52f4b
raw
history blame
2.18 kB
import gradio as gr
from wordcloud import WordCloud
import pandas as pd
import matplotlib.pyplot as plt
# Assume you have a DataFrame with columns: Topic, Article, Keywords
# You need to replace this with your actual data loading mechanism.
# For example, you might fetch data from a database or a file.
data = {
'Topic': ['Topic1', 'Topic2'],
'keywords': ['key1 key2 key3', ["cream","ice","vanilla","flavor","chocolate","kid","häagen","dazs","you","sprinting"]],
'scores' :[[], [0.16746170342156613,0.15000902432939608,0.0793086259849024,0.0642684614359449,0.05274725840837433,0.051507427048382876,0.047404471182744455,0.047404471182744455,0.03655408024186657,0.035427310538133555]]
}
data2 = {
'Topic' : ["Topic2", "Topic2"],
'Article' : ["1", "2"],
'Link' : ['https://www.google.fr', 'https://www.google.fr']
}
df = pd.DataFrame(data)
topics = df['Topic'].unique()
df2 = pd.DataFrame(data2)
def display_topics(topic):
# Filter DataFrame based on the selected topic
selected_data = df[df['Topic'] == topic]
selected_data2 = df2[df2['Topic'] == topic]
# Display relevant articles
articles = selected_data2['Article']
links = selected_data2['Link']
nb_art = min(4, len(links))
articles_ret = """## Most relevant articles
"""
for i in range(nb_art):
articles_ret += f""" * [{articles[i]}]({links[i]})
"""
# Generate word cloud for keywords
keywords = selected_data['keywords'][1]
freq = selected_data["scores"][1]
keywords_wordcloud = dict()
for i, elem in enumerate(keywords):
keywords_wordcloud[elem] = freq[i]
wordcloud = WordCloud(width=800, height=400, background_color='white').generate_from_frequencies(keywords_wordcloud)
fig, ax = plt.subplots()
plt.axis("off")
ax =plt.imshow(wordcloud, interpolation='bilinear')
return articles_ret , fig
# Define Gradio interface
iface = gr.Interface(
fn=display_topics,
inputs=gr.Dropdown(["Topic1", "Topic2"], label="Topic"),
outputs=[gr.Markdown(label="Most relevant articles"),gr.Plot(label="Main Keywords")],
live=True,
examples=[]
)
# Launch the app
iface.launch()