Fralet commited on
Commit
63b60f3
·
verified ·
1 Parent(s): 553472c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -20
app.py CHANGED
@@ -1,26 +1,22 @@
1
  import streamlit as st
2
  import pandas as pd
3
  from transformers import pipeline
4
- import torch
5
 
6
  # Load translation and summarization pipelines
7
  translator = pipeline("translation_ru_to_en", model="Helsinki-NLP/opus-mt-ru-en")
8
  summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
 
9
 
10
- torch.manual_seed(0)
11
- generator = pipeline('text-generation', model = 'openai-community/gpt2')
12
-
13
-
14
-
15
- # Function to translate and summarize text
16
- def translate_and_summarize(text):
17
  translated_text = translator(text)[0]['translation_text']
18
- summary = 'News Alert. ' + summarizer(translated_text, max_length=140, min_length=110, do_sample=False)[0]['summary_text']
19
- return summary
 
20
 
21
  # Streamlit interface
22
  def main():
23
- st.title("CSV Translator and Summarizer")
24
 
25
  # File uploader
26
  uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
@@ -29,19 +25,15 @@ def main():
29
  data = pd.read_csv(uploaded_file)
30
  # Check if 'Description' and 'Published' columns exist
31
  if 'Description' in data.columns and 'Published' in data.columns:
32
- # Apply translation and summarization based on 'Published' column
33
- data['Summary'] = data.apply(
34
- lambda row: translate_and_summarize(row['Description']) if pd.isna(row['Published']) else "", axis=1
35
  )
36
- prompt = "Generate title for this news" + data['Summary']
37
-
38
- data['Title'] = generator(prompt, max_length = 30)
39
-
40
  # Display data in a table
41
- st.write(data[['ID', 'Title', 'Summary', 'Title']])
42
 
43
  else:
44
  st.error("Uploaded CSV does not contain required 'Description' and 'Published' columns.")
45
 
46
  if __name__ == "__main__":
47
- main()
 
1
  import streamlit as st
2
  import pandas as pd
3
  from transformers import pipeline
 
4
 
5
  # Load translation and summarization pipelines
6
  translator = pipeline("translation_ru_to_en", model="Helsinki-NLP/opus-mt-ru-en")
7
  summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
8
+ title_generator = pipeline("text2text-generation", model="facebook/bart-large-cnn")
9
 
10
+ # Function to translate, summarize, and generate title
11
+ def translate_summarize_and_generate_title(text):
 
 
 
 
 
12
  translated_text = translator(text)[0]['translation_text']
13
+ summary = 'News Alert. ' + summarizer(translated_text, max_length=140, min_length=110, do_sample=False)[0]['summary_text']
14
+ title = title_generator(translated_text, max_length=10, min_length=5, do_sample=False)[0]['generated_text']
15
+ return summary, title
16
 
17
  # Streamlit interface
18
  def main():
19
+ st.title("CSV Translator, Summarizer, and Title Generator")
20
 
21
  # File uploader
22
  uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
 
25
  data = pd.read_csv(uploaded_file)
26
  # Check if 'Description' and 'Published' columns exist
27
  if 'Description' in data.columns and 'Published' in data.columns:
28
+ # Apply translation, summarization, and title generation based on 'Published' column
29
+ data[['Summary', 'Generated Title']] = data.apply(
30
+ lambda row: translate_summarize_and_generate_title(row['Description']) if pd.isna(row['Published']) else ("", ""), axis=1, result_type='expand'
31
  )
 
 
 
 
32
  # Display data in a table
33
+ st.write(data[['ID', 'Title', 'Generated Title', 'Summary']])
34
 
35
  else:
36
  st.error("Uploaded CSV does not contain required 'Description' and 'Published' columns.")
37
 
38
  if __name__ == "__main__":
39
+ main()