saicharantej's picture
Update app.py
4eec1e4
Raw
History Blame Contribute Delete
1.85 kB
import gradio as gr
import openai
from transformers import pipeline
from newspaper import Article
import os
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
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
def generate_explanation(concept):
model_engine = "text-davinci-003"
prompt = f"Explain {concept} to a 10-year old in simple terms."
response = openai.Completion.create(engine=model_engine, prompt=prompt, max_tokens=2048)
return response.choices[0].text.strip()
def article_or_concept(input_type, article_data,concept_data):
if input_type == "Article":
summary = summarizer(article_data, max_length=100)
explanation = generate_explanation(summary[0]['summary_text'])
print (summary)
return explanation
else:
explanation = generate_explanation(concept_data)
return explanation
article_url_input = gr.inputs.Textbox(label="Enter article URL:")
concept_input = gr.inputs.Textbox(label="Enter a concept:")
input_type = gr.inputs.Dropdown(choices=["Article", "Concept"], label="Select input type:")
output_text = gr.outputs.Textbox(label="Explanation:")
interface = gr.Interface(fn=article_or_concept, inputs=[input_type, article_url_input, concept_input], outputs=output_text, title="Machine Learning Granny",
theme = 'huggingface',
description="Input either a ML article URL or a concept to receive an explanation that even a grandmother can understand.")
interface.launch(debug=True)