saicharantej commited on
Commit
b71d2ec
·
1 Parent(s): 08e7e89

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -22
app.py CHANGED
@@ -1,9 +1,9 @@
1
  from newspaper import Article
2
  from newspaper import Config
3
- import nltk
4
- nltk.download('punkt')
5
- from gradio.mix import Series
6
  import gradio as gr
 
 
 
7
  def extract_article_text(url):
8
  USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Firefox/78.0'
9
  config = Config()
@@ -15,26 +15,46 @@ def extract_article_text(url):
15
  article.parse()
16
  text = article.text
17
  return text
18
- extractor = gr.Interface(extract_article_text, 'text', 'text')
19
- summarizer = gr.Interface.load("huggingface/facebook/bart-large-cnn")
20
 
21
- sample_url = [['https://www.technologyreview.com/2021/07/22/1029973/deepmind-alphafold-protein-folding-biology-disease-drugs-proteome/'],
22
- ['https://www.technologyreview.com/2021/07/21/1029860/disability-rights-employment-discrimination-ai-hiring/'],
23
- ['https://www.technologyreview.com/2021/07/09/1028140/ai-voice-actors-sound-human/']]
 
 
 
 
 
24
 
25
- desc = '''
26
- Generate summary of an article using bart-large-cnn model by Facebook.
27
- '''
 
 
 
 
 
 
28
 
29
- iface = Series(extractor, summarizer,
30
- inputs = gr.inputs.Textbox(
31
- lines = 2,
32
- label = 'URL'
33
- ),
34
- outputs = 'text',
35
- title = 'Article Cortex: Transformers powered article summarization engine',
36
- theme = 'huggingface',
37
- description = desc,
38
- examples=sample_url,allow_flagging="manual",flagging_options=["random summary", "useful summary"])
39
 
40
- iface.launch(debug=True)
 
 
 
 
 
 
 
 
 
 
 
 
1
  from newspaper import Article
2
  from newspaper import Config
 
 
 
3
  import gradio as gr
4
+ # Set up OpenAI API credentials
5
+ import openai
6
+ openai.api_key = os.getenv('api_token')
7
  def extract_article_text(url):
8
  USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Firefox/78.0'
9
  config = Config()
 
15
  article.parse()
16
  text = article.text
17
  return text
 
 
18
 
19
+ def get_completion(prompt, model="gpt-3.5-turbo"):
20
+ messages = [{"role": "user", "content": prompt}]
21
+ response = openai.ChatCompletion.create(
22
+ model=model,
23
+ messages=messages,
24
+ temperature=0.5, # this is the degree of randomness of the model's output
25
+ )
26
+ return response.choices[0].message["content"]
27
 
28
+ def prompt_summary(url,movie):
29
+ text = extract_article_text(url)
30
+ text = text[:4096]
31
+ prompt_sum = f"""
32
+ Summarize the text {text} as if a 8-year old kid understands. The summary should be atmost 200 words and should help the kid understand how the summary could help him solve a real-world problem. Here is the format:
33
+ 1.Importance of the article:
34
+ 2.Real world scenario:
35
+ 3.Key takeaway:
36
+ """
37
 
38
+ prompt_mov = f"""
39
+ Convert the technical article {text} into a short story involving the characters from the movie {movie}. The story should not exceed 200 words and should be written in a way that captures the essence of the article while also making it engaging and entertaining.
40
+ """
41
+ prompt_topic = f"""
42
+ Extract 5 key topics from the text {text}. The topics should clearly tell the user why it is important to read the article. Length of the topic should be limited to a single word
43
+ """
44
+ response_top = get_completion(prompt_topic)
45
+ response_mov = get_completion(prompt_mov)
46
+ response_sum = get_completion(prompt_sum)
47
+ return response_top, response_sum, response_mov
48
 
49
+ inputs = [
50
+ gr.inputs.Textbox(label="Article URL"),
51
+ gr.inputs.Textbox(label="Which is your favorite movie?")
52
+ ]
53
+
54
+ outputs = [
55
+ gr.outputs.Textbox(label="Key topics"),
56
+ gr.outputs.Textbox(label="Summary without jargon"),
57
+ gr.outputs.Textbox(label="Summary as movie synopsis")
58
+ ]
59
+
60
+ gr.Interface(prompt_summary, inputs, outputs, title="Article Cortex", description="Helps you understand any technical article as if it were a movie synopsis.").launch(debug=True)