chandrujobs commited on
Commit
d8778e3
·
verified ·
1 Parent(s): 9f050d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -14
app.py CHANGED
@@ -1,7 +1,7 @@
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,21 +10,27 @@ model_name = "gpt2"
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,
@@ -38,24 +44,27 @@ def fetch_trending_topics(search_term="artificial intelligence OR machine learni
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
 
@@ -78,16 +87,20 @@ def analyze_trends(search_term="artificial intelligence OR machine learning", pa
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/>
@@ -101,20 +114,30 @@ def display_news_cards(search_term="artificial intelligence OR machine learning"
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
 
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
  tokenizer = AutoTokenizer.from_pretrained(model_name)
11
  model = AutoModelForCausalLM.from_pretrained(model_name).to(device)
12
 
13
+ # Set pad_token_id to avoid errors
14
+ model.config.pad_token_id = model.config.eos_token_id
15
+
16
  # NewsAPI Setup (Replace with your own API key)
17
  news_api_key = "35cbd14c45184a109fc2bbb5fff7fb1b" # Replace with your NewsAPI key
18
 
19
  def fetch_trending_topics(search_term="artificial intelligence OR machine learning", page=1, page_size=9):
20
  try:
21
+ # Fetch AI and Machine Learning related news from NewsAPI with search term
22
+ url = f"https://newsapi.org/v2/everything?q={search_term}&sortBy=publishedAt&pageSize={page_size + 5}&page={page}&language=en&apiKey={news_api_key}" # Fetch extra to avoid duplicates
23
  response = requests.get(url)
24
  data = response.json()
25
 
26
+ # Check for valid response
27
  if response.status_code == 200 and "articles" in data:
28
+ # Collect articles without duplicates
29
  trending_topics = []
30
  seen_titles = set()
31
  for article in data["articles"]:
32
  title = article["title"]
33
+ if title not in seen_titles: # Avoid duplicate titles
34
  seen_titles.add(title)
35
  trending_topics.append({
36
  "title": title,
 
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 a max limit on tokens
58
  inputs = tokenizer(input_text, return_tensors="pt").to(device)
59
+ outputs = model.generate(**inputs, max_length=80, num_return_sequences=1, do_sample=True, top_k=50, top_p=0.95)
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(page=1, page_size=9):
67
+ search_term = "artificial intelligence OR machine learning" # Fixed search term
68
  trending_topics = fetch_trending_topics(search_term=search_term, page=page, page_size=page_size)
69
  topic_analysis = []
70
 
 
87
  "publishedAt": topic["publishedAt"],
88
  })
89
 
90
+ # Limit the results to the specified page size
91
+ return topic_analysis[:page_size] # Ensure only the specified number of articles are returned
92
 
93
+ # Gradio UI with 3 Columns Layout for Displaying News
94
+ def display_news_cards(page=1, page_size=9):
95
+ analysis_results = analyze_trends(page=page, page_size=page_size)
96
+ current_date = datetime.now().strftime("%d-%m-%Y") # Format: DD-MM-YYYY
97
 
98
  display = f"### **AI & Machine Learning News for {current_date}**\n\n"
99
 
100
+ # Create a 3-column layout
101
  display += "<div style='display:flex; flex-wrap:wrap; justify-content:space-between;'>"
102
  for news_item in analysis_results:
103
+ # Each news box in a flex box with equal width
104
  display += f"""
105
  <div style='flex: 1 1 30%; border:1px solid black; margin:10px; padding:10px; box-sizing:border-box;'>
106
  <b>{news_item['title']}</b><br/>
 
114
 
115
  return display
116
 
117
+ # Gradio UI with Header, Search Option, and Submit Button
118
  def gradio_interface():
119
  with gr.Blocks() as demo:
120
+ # Header with background color
121
  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")
122
 
123
+ # Fixed search term displayed to the user
124
+ gr.Markdown("<p style='text-align:center;'>Search term: <b>artificial intelligence OR machine learning</b></p>")
125
+
126
+ # Sliders for page number and news per page
127
  page = gr.Slider(minimum=1, maximum=5, step=1, label="Page Number", value=1)
128
  page_size = gr.Slider(minimum=6, maximum=15, step=3, label="News per Page", value=9)
129
 
130
+ # Button to fetch and analyze news
131
  analyze_button = gr.Button("Submit")
132
+
133
+ # Output area for displaying the news
134
  news_output = gr.HTML()
135
 
136
+ # Link the button click to the display function
137
+ analyze_button.click(display_news_cards, inputs=[page, page_size], outputs=news_output)
138
 
139
  return demo
140
 
141
+ # Launch the Gradio UI
142
  if __name__ == "__main__":
143
+ gradio_interface().launch(share=True) # Set share=True for public link