saicharantej commited on
Commit
45f1ea4
·
1 Parent(s): 4a7cdcc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -0
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import openai
3
+ from transformers import pipeline
4
+ from newspaper import Article
5
+
6
+ openai.api_key = os.getenv('api_token')
7
+
8
+ def extract_article_text(url):
9
+ USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Firefox/78.0'
10
+ config = Config()
11
+ config.browser_user_agent = USER_AGENT
12
+ config.request_timeout = 10
13
+
14
+ article = Article(url, config=config)
15
+ article.download()
16
+ article.parse()
17
+ text = article.text
18
+ return text
19
+
20
+ summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
21
+
22
+ def generate_explanation(concept):
23
+ model_engine = "text-davinci-003"
24
+ prompt = f"Explain {concept} to a 10-year old in simple terms."
25
+ response = openai.Completion.create(engine=model_engine, prompt=prompt, max_tokens=2048)
26
+ return response.choices[0].text.strip()
27
+
28
+ def article_or_concept(input_type, article_data,concept_data):
29
+ if input_type == "Article":
30
+ summary = summarizer(article_data, max_length=100)
31
+ explanation = generate_explanation(summary[0]['summary_text'])
32
+ print (summary)
33
+ return explanation
34
+ else:
35
+ explanation = generate_explanation(concept_data)
36
+ return explanation
37
+
38
+ article_url_input = gr.inputs.Textbox(label="Enter article URL:")
39
+ concept_input = gr.inputs.Textbox(label="Enter a concept:")
40
+
41
+ input_type = gr.inputs.Dropdown(choices=["Article", "Concept"], label="Select input type:")
42
+
43
+ output_text = gr.outputs.Textbox(label="Explanation:")
44
+
45
+ interface = gr.Interface(fn=article_or_concept, inputs=[input_type, article_url_input, concept_input], outputs=output_text, title="Machine Learning Granny",
46
+ theme = 'huggingface',
47
+ description="Input either a ML article URL or a concept to receive an explanation that even a grandmother can understand.")
48
+
49
+ interface.launch(debug=True)