Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import requests
|
2 |
import torch
|
3 |
import gradio as gr
|
4 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
5 |
from datetime import datetime
|
6 |
|
7 |
# GPT-2 setup
|
@@ -10,27 +10,21 @@ model_name = "gpt2"
|
|
10 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
11 |
model = AutoModelForCausalLM.from_pretrained(model_name).to(device)
|
12 |
|
13 |
-
# Set pad_token_id if not already defined
|
14 |
-
tokenizer.pad_token = tokenizer.eos_token # Set pad_token to eos_token if not defined
|
15 |
-
model.config.pad_token_id = tokenizer.pad_token_id
|
16 |
-
|
17 |
# NewsAPI Setup (Replace with your own API key)
|
18 |
news_api_key = "35cbd14c45184a109fc2bbb5fff7fb1b" # Replace with your NewsAPI key
|
19 |
|
20 |
def fetch_trending_topics(search_term="artificial intelligence OR machine learning", page=1, page_size=9):
|
21 |
try:
|
22 |
-
# Fetch AI and Machine Learning related news from NewsAPI with search term
|
23 |
url = f"https://newsapi.org/v2/everything?q={search_term}&sortBy=publishedAt&pageSize={page_size + 5}&page={page}&language=en&apiKey={news_api_key}"
|
24 |
response = requests.get(url)
|
25 |
data = response.json()
|
26 |
-
|
27 |
-
# Check for valid response
|
28 |
if response.status_code == 200 and "articles" in data:
|
29 |
trending_topics = []
|
30 |
seen_titles = set()
|
31 |
for article in data["articles"]:
|
32 |
title = article["title"]
|
33 |
-
if title not in seen_titles:
|
34 |
seen_titles.add(title)
|
35 |
trending_topics.append({
|
36 |
"title": title,
|
@@ -41,32 +35,30 @@ def fetch_trending_topics(search_term="artificial intelligence OR machine learni
|
|
41 |
|
42 |
if not trending_topics:
|
43 |
return [{"title": "No news available", "description": "", "url": "", "publishedAt": ""}]
|
44 |
-
|
45 |
return trending_topics
|
46 |
else:
|
47 |
-
print(f"Error: {data.get('message', 'No articles found')}")
|
48 |
return [{"title": "No news available", "description": "", "url": "", "publishedAt": ""}]
|
49 |
except Exception as e:
|
50 |
-
print(f"Error fetching news: {e}")
|
51 |
return [{"title": "Error fetching news", "description": "", "url": "", "publishedAt": ""}]
|
52 |
|
53 |
# Analyze the trending topic using GPT-2
|
54 |
def generate_analysis(trending_topic):
|
55 |
input_text = f"Provide a concise analysis about the following topic: '{trending_topic['title']}'. Please summarize its significance in the AI and Machine Learning field."
|
56 |
|
57 |
-
# Tokenize and generate text with
|
58 |
inputs = tokenizer(input_text, return_tensors="pt").to(device)
|
59 |
-
|
60 |
|
|
|
61 |
analysis = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
62 |
|
63 |
return analysis
|
64 |
|
65 |
-
# Combine both functions for Gradio
|
66 |
def analyze_trends(search_term="artificial intelligence OR machine learning", page=1, page_size=9):
|
67 |
trending_topics = fetch_trending_topics(search_term=search_term, page=page, page_size=page_size)
|
68 |
topic_analysis = []
|
69 |
-
|
70 |
for topic in trending_topics:
|
71 |
if topic["title"] not in ["Error fetching news", "No news available"]:
|
72 |
analysis = generate_analysis(topic)
|
@@ -85,22 +77,19 @@ def analyze_trends(search_term="artificial intelligence OR machine learning", pa
|
|
85 |
"url": topic["url"],
|
86 |
"publishedAt": topic["publishedAt"],
|
87 |
})
|
|
|
|
|
88 |
|
89 |
-
# Limit the results to the specified page size
|
90 |
-
return topic_analysis[:page_size] # Ensure only the specified number of articles are returned
|
91 |
-
|
92 |
-
# Gradio UI with 3 Columns Layout for Displaying News
|
93 |
def display_news_cards(search_term="artificial intelligence OR machine learning", page=1, page_size=9):
|
94 |
analysis_results = analyze_trends(search_term=search_term, page=page, page_size=page_size)
|
95 |
-
current_date = datetime.now().strftime("%d-%m-%Y")
|
96 |
|
97 |
display = f"### **AI & Machine Learning News for {current_date}**\n\n"
|
98 |
-
|
99 |
-
# Create a 3-column layout
|
100 |
display += "<div style='display:flex; flex-wrap:wrap; justify-content:space-between;'>"
|
101 |
for news_item in analysis_results:
|
102 |
display += f"""
|
103 |
-
<div style='flex: 1 1 30%; border:1px solid black; margin:10px; padding:10px; box-sizing:border-box;'
|
104 |
<b>{news_item['title']}</b><br/>
|
105 |
<i>{news_item['publishedAt']}</i><br/><br/>
|
106 |
{news_item['description']}<br/><br/>
|
@@ -112,28 +101,20 @@ def display_news_cards(search_term="artificial intelligence OR machine learning"
|
|
112 |
|
113 |
return display
|
114 |
|
115 |
-
# Gradio UI with Header, Search Option, and Submit Button
|
116 |
def gradio_interface():
|
117 |
with gr.Blocks() as demo:
|
118 |
-
# Header with background color
|
119 |
gr.Markdown("""<h1 style='text-align:center; color:white; background-color:#007BFF; padding:20px; border-radius:10px;'>AI & Machine Learning News Analyzer</h1>""", elem_id="header")
|
120 |
|
121 |
-
# Search Bar and Submit Button
|
122 |
search_term = gr.Textbox(label="Search for News", placeholder="Search 'AI' or 'Machine Learning'", value="artificial intelligence OR machine learning")
|
123 |
page = gr.Slider(minimum=1, maximum=5, step=1, label="Page Number", value=1)
|
124 |
page_size = gr.Slider(minimum=6, maximum=15, step=3, label="News per Page", value=9)
|
125 |
|
126 |
-
# Button to fetch and analyze news
|
127 |
analyze_button = gr.Button("Submit")
|
128 |
-
|
129 |
-
# Output area for displaying the news
|
130 |
news_output = gr.HTML()
|
131 |
|
132 |
-
# Link the button click to the display function
|
133 |
analyze_button.click(display_news_cards, inputs=[search_term, page, page_size], outputs=news_output)
|
134 |
|
135 |
return demo
|
136 |
|
137 |
-
# Launch the Gradio UI
|
138 |
if __name__ == "__main__":
|
139 |
-
gradio_interface().launch(share=True
|
|
|
1 |
import requests
|
2 |
import torch
|
3 |
import gradio as gr
|
4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, GenerationConfig
|
5 |
from datetime import datetime
|
6 |
|
7 |
# GPT-2 setup
|
|
|
10 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
11 |
model = AutoModelForCausalLM.from_pretrained(model_name).to(device)
|
12 |
|
|
|
|
|
|
|
|
|
13 |
# NewsAPI Setup (Replace with your own API key)
|
14 |
news_api_key = "35cbd14c45184a109fc2bbb5fff7fb1b" # Replace with your NewsAPI key
|
15 |
|
16 |
def fetch_trending_topics(search_term="artificial intelligence OR machine learning", page=1, page_size=9):
|
17 |
try:
|
|
|
18 |
url = f"https://newsapi.org/v2/everything?q={search_term}&sortBy=publishedAt&pageSize={page_size + 5}&page={page}&language=en&apiKey={news_api_key}"
|
19 |
response = requests.get(url)
|
20 |
data = response.json()
|
21 |
+
|
|
|
22 |
if response.status_code == 200 and "articles" in data:
|
23 |
trending_topics = []
|
24 |
seen_titles = set()
|
25 |
for article in data["articles"]:
|
26 |
title = article["title"]
|
27 |
+
if title not in seen_titles:
|
28 |
seen_titles.add(title)
|
29 |
trending_topics.append({
|
30 |
"title": title,
|
|
|
35 |
|
36 |
if not trending_topics:
|
37 |
return [{"title": "No news available", "description": "", "url": "", "publishedAt": ""}]
|
38 |
+
|
39 |
return trending_topics
|
40 |
else:
|
|
|
41 |
return [{"title": "No news available", "description": "", "url": "", "publishedAt": ""}]
|
42 |
except Exception as e:
|
|
|
43 |
return [{"title": "Error fetching news", "description": "", "url": "", "publishedAt": ""}]
|
44 |
|
45 |
# Analyze the trending topic using GPT-2
|
46 |
def generate_analysis(trending_topic):
|
47 |
input_text = f"Provide a concise analysis about the following topic: '{trending_topic['title']}'. Please summarize its significance in the AI and Machine Learning field."
|
48 |
|
49 |
+
# Tokenize and generate text with generation config
|
50 |
inputs = tokenizer(input_text, return_tensors="pt").to(device)
|
51 |
+
generation_config = GenerationConfig(max_length=80, num_return_sequences=1, do_sample=True, top_k=50, top_p=0.95)
|
52 |
|
53 |
+
outputs = model.generate(**inputs, generation_config=generation_config)
|
54 |
analysis = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
55 |
|
56 |
return analysis
|
57 |
|
|
|
58 |
def analyze_trends(search_term="artificial intelligence OR machine learning", page=1, page_size=9):
|
59 |
trending_topics = fetch_trending_topics(search_term=search_term, page=page, page_size=page_size)
|
60 |
topic_analysis = []
|
61 |
+
|
62 |
for topic in trending_topics:
|
63 |
if topic["title"] not in ["Error fetching news", "No news available"]:
|
64 |
analysis = generate_analysis(topic)
|
|
|
77 |
"url": topic["url"],
|
78 |
"publishedAt": topic["publishedAt"],
|
79 |
})
|
80 |
+
|
81 |
+
return topic_analysis[:page_size]
|
82 |
|
|
|
|
|
|
|
|
|
83 |
def display_news_cards(search_term="artificial intelligence OR machine learning", page=1, page_size=9):
|
84 |
analysis_results = analyze_trends(search_term=search_term, page=page, page_size=page_size)
|
85 |
+
current_date = datetime.now().strftime("%d-%m-%Y")
|
86 |
|
87 |
display = f"### **AI & Machine Learning News for {current_date}**\n\n"
|
88 |
+
|
|
|
89 |
display += "<div style='display:flex; flex-wrap:wrap; justify-content:space-between;'>"
|
90 |
for news_item in analysis_results:
|
91 |
display += f"""
|
92 |
+
<div style='flex: 1 1 30%; border:1px solid black; margin:10px; padding:10px; box-sizing:border-box;'>
|
93 |
<b>{news_item['title']}</b><br/>
|
94 |
<i>{news_item['publishedAt']}</i><br/><br/>
|
95 |
{news_item['description']}<br/><br/>
|
|
|
101 |
|
102 |
return display
|
103 |
|
|
|
104 |
def gradio_interface():
|
105 |
with gr.Blocks() as demo:
|
|
|
106 |
gr.Markdown("""<h1 style='text-align:center; color:white; background-color:#007BFF; padding:20px; border-radius:10px;'>AI & Machine Learning News Analyzer</h1>""", elem_id="header")
|
107 |
|
|
|
108 |
search_term = gr.Textbox(label="Search for News", placeholder="Search 'AI' or 'Machine Learning'", value="artificial intelligence OR machine learning")
|
109 |
page = gr.Slider(minimum=1, maximum=5, step=1, label="Page Number", value=1)
|
110 |
page_size = gr.Slider(minimum=6, maximum=15, step=3, label="News per Page", value=9)
|
111 |
|
|
|
112 |
analyze_button = gr.Button("Submit")
|
|
|
|
|
113 |
news_output = gr.HTML()
|
114 |
|
|
|
115 |
analyze_button.click(display_news_cards, inputs=[search_term, page, page_size], outputs=news_output)
|
116 |
|
117 |
return demo
|
118 |
|
|
|
119 |
if __name__ == "__main__":
|
120 |
+
gradio_interface().launch() # Remove share=True if you don't need public links
|