Spaces:
Sleeping
Sleeping
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) | |