File size: 1,846 Bytes
45f1ea4
 
 
 
a737a33
45f1ea4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4eec1e4
45f1ea4
 
 
 
 
4eec1e4
45f1ea4
 
 
 
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
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)