Spaces:
Sleeping
Sleeping
File size: 2,379 Bytes
78e946b 33f5b69 b71d2ec 78e946b b71d2ec 78e946b b71d2ec 78e946b b71d2ec 78e946b b71d2ec |
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 |
from newspaper import Article
from newspaper import Config
import gradio as gr
import os
import openai
openai.api_key = os.getenv('api_token')
def extract_article_text(url):
USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Firefox/78.0'
config = Config()
config.browser_user_agent = USER_AGENT
config.request_timeout = 10
article = Article(url, config=config)
article.download()
article.parse()
text = article.text
return text
def get_completion(prompt, model="gpt-3.5-turbo"):
messages = [{"role": "user", "content": prompt}]
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=0.5, # this is the degree of randomness of the model's output
)
return response.choices[0].message["content"]
def prompt_summary(url,movie):
text = extract_article_text(url)
text = text[:4096]
prompt_sum = f"""
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:
1.Importance of the article:
2.Real world scenario:
3.Key takeaway:
"""
prompt_mov = f"""
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.
"""
prompt_topic = f"""
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
"""
response_top = get_completion(prompt_topic)
response_mov = get_completion(prompt_mov)
response_sum = get_completion(prompt_sum)
return response_top, response_sum, response_mov
inputs = [
gr.inputs.Textbox(label="Article URL"),
gr.inputs.Textbox(label="Which is your favorite movie?")
]
outputs = [
gr.outputs.Textbox(label="Key topics"),
gr.outputs.Textbox(label="Summary without jargon"),
gr.outputs.Textbox(label="Summary as movie synopsis")
]
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)
|