ENGRLAKSHMI commited on
Commit
ec8e443
Β·
1 Parent(s): 95bfcbd

Add Gradio app for article summarization

Browse files
Files changed (4) hide show
  1. app.py +16 -0
  2. article_utils.py +14 -0
  3. qa_utils.py +12 -0
  4. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from article_utils import extract_article_text, summarize_article
3
+ from qa_utils import answer_question
4
+
5
+ def summarize_from_url(url):
6
+ try:
7
+ text = extract_article_text(url)
8
+ summary = summarize_article(text)
9
+ return summary
10
+ except Exception as e:
11
+ return f"Error: {e}"
12
+
13
+ demo = gr.Interface(fn=summarize_from_url, inputs="text", outputs="text", title="Article Summarizer")
14
+
15
+ if __name__ == "__main__":
16
+ demo.launch()
article_utils.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from newspaper import Article
2
+ from transformers import pipeline
3
+
4
+ article_summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6") # Replace with your actual token
5
+
6
+ def extract_article_text(url):
7
+ article = Article(url)
8
+ article.download()
9
+ article.parse()
10
+ return article.text
11
+
12
+ def summarize_article(text):
13
+ summary = article_summarizer(text, max_length=150, min_length=50, do_sample=False)
14
+ return summary[0]['summary_text']
qa_utils.py ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+ import os
3
+
4
+ # Set your Hugging Face token as an environment variable
5
+ # Make sure this token is valid and has the necessary permissions
6
+ # or, pass the token directly to the pipeline function
7
+ qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad") # Replace with your actual token
8
+
9
+
10
+ def answer_question(question, context):
11
+ result = qa_pipeline(question=question, context=context)
12
+ return result['answer']
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ # requirements.txt
2
+ %%writefile requirements.txt
3
+ gradio
4
+ newspaper3k
5
+ transformers
6
+ torch