File size: 3,793 Bytes
2fd9b5e 4d0ae17 45b0be7 29a2d9a b633f92 29a2d9a f0d1a4f 9fa9eb3 a77da05 f0d1a4f a77da05 f0d1a4f 4d0ae17 29a2d9a f0d1a4f f02c363 9fa9eb3 4d0ae17 979e9c8 f02c363 45b0be7 a77da05 4d0ae17 979e9c8 f02c363 45b0be7 a77da05 4d0ae17 a77da05 4d0ae17 29a2d9a a77da05 4d0ae17 29a2d9a a77da05 9d9c822 29a2d9a a77da05 29a2d9a 6bc37bb 4d0ae17 a77da05 6bc37bb 4d0ae17 a77da05 4d0ae17 a77da05 6bc37bb 4d0ae17 a77da05 6bc37bb 4d0ae17 a77da05 6bc37bb 4d0ae17 a77da05 6bc37bb 4d0ae17 6bc37bb |
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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
import gradio as gr
import requests
import threading
from api import get_news, summarize_text
from analysis import analyze_sentiment, generate_sentiment_graph, generate_wordcloud
from utils import generate_tts
import plotly.express as px
import pandas as pd
def generate_sentiment_chart(sentiments):
df = pd.DataFrame({"Sentiment": sentiments}) # Fixed column mismatch
df["Count"] = 1 # Count occurrences
df_grouped = df.groupby("Sentiment").count().reset_index()
fig = px.pie(df_grouped, names="Sentiment", values="Count",
color="Sentiment",
color_discrete_map={"Positive": "#2ECC71", "Negative": "#E74C3C", "Neutral": "#3498DB"},
hole=0.4, title="Sentiment Analysis")
return fig
# Optimized Processing Function
def process_news(company_name, query):
if not company_name:
return "β Please enter a company name.", None, None, None, None
# Fetch news in a separate thread to reduce wait time
articles = get_news(company_name, query)
if not articles:
return "β No news found!", None, None, None, None
# Summarization and Sentiment Analysis (Parallel Processing)
summaries, sentiments = [], []
def summarize_and_analyze(article):
summary = summarize_text(article["content"]) # Removed max_length to avoid errors
sentiment = analyze_sentiment(summary)
summaries.append(summary)
sentiments.append(sentiment)
threads = []
for article in articles[:3]:
thread = threading.Thread(target=summarize_and_analyze, args=(article,))
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
# Generate sentiment graph using Plotly
sentiment_graph = generate_sentiment_chart(sentiments)
# Generate word cloud
wordcloud_path = generate_wordcloud(" ".join(summaries))
# Generate Text-to-Speech
tts_audio = generate_tts("\n".join(summaries[:2]))
return summaries, sentiments, wordcloud_path, sentiment_graph, tts_audio
# Modern UI Design (Inspired by Your Image)
custom_css = """
body {
background: linear-gradient(90deg, #6a11cb 0%, #2575fc 100%);
font-family: 'Arial', sans-serif;
}
.gradio-container {
max-width: 800px;
margin: auto;
background: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(10px);
border-radius: 15px;
padding: 20px;
box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.2);
}
h1 {
text-align: center;
color: #fff;
font-size: 28px;
}
input, button {
border-radius: 10px !important;
padding: 10px !important;
}
button {
background: #ff7eb3 !important;
color: white !important;
font-size: 16px;
}
"""
# Gradio UI with Modern Design
with gr.Blocks(css=custom_css) as demo:
gr.Markdown("<h1>π° AI News Summarizer & Sentiment Analyzer</h1>")
with gr.Row():
company_input = gr.Textbox(label="Enter Company Name", placeholder="Tesla, Google, Microsoft...")
query_input = gr.Textbox(label="Query (optional)", placeholder="Electric cars, AI, Stocks...")
submit_button = gr.Button("π Get News")
with gr.Row():
output_text = gr.Textbox(label="Summarized News", interactive=False)
with gr.Row():
sentiment_output = gr.Textbox(label="Sentiment Analysis", interactive=False)
sentiment_graph = gr.Plot(label="Sentiment Analysis Graph") # Using Plotly
with gr.Row():
wordcloud_output = gr.Image(label="Word Cloud")
tts_audio = gr.Audio(label="π Listen to Summary")
submit_button.click(process_news, inputs=[company_input, query_input],
outputs=[output_text, sentiment_output, wordcloud_output, sentiment_graph, tts_audio])
# Launch Gradio App
demo.launch(share=True)
|