Spaces:
Sleeping
Sleeping
Jeff Myers II
commited on
Commit
·
7908b9a
1
Parent(s):
c247815
Completed Prototype
Browse files
app.py
CHANGED
|
@@ -8,9 +8,9 @@ from Gemma import GemmaLLM
|
|
| 8 |
class Cooldown:
|
| 9 |
...
|
| 10 |
|
| 11 |
-
cooldown = Cooldown()
|
| 12 |
-
news = News()
|
| 13 |
-
model = GemmaLLM()
|
| 14 |
|
| 15 |
# %%
|
| 16 |
with gr.Blocks() as demo:
|
|
@@ -20,7 +20,7 @@ with gr.Blocks() as demo:
|
|
| 20 |
###### State Variables and Components Initialization
|
| 21 |
######
|
| 22 |
|
| 23 |
-
init = (
|
| 24 |
|
| 25 |
gr.Markdown("## News Articles"),
|
| 26 |
|
|
@@ -52,7 +52,7 @@ with gr.Blocks() as demo:
|
|
| 52 |
|
| 53 |
)
|
| 54 |
|
| 55 |
-
(
|
| 56 |
|
| 57 |
heading,
|
| 58 |
|
|
@@ -71,7 +71,7 @@ with gr.Blocks() as demo:
|
|
| 71 |
|
| 72 |
) = init
|
| 73 |
|
| 74 |
-
def hide_news():
|
| 75 |
num_headlines = gr.Slider(visible=False)
|
| 76 |
category = gr.Radio(visible=False)
|
| 77 |
get = gr.Button(visible=False)
|
|
@@ -86,28 +86,28 @@ with gr.Blocks() as demo:
|
|
| 86 |
|
| 87 |
return num_headlines, category, get, headline, description, show, summarize, content, ready
|
| 88 |
|
| 89 |
-
def show_news():
|
| 90 |
num_headlines = gr.Slider(label="Number of Articles", minimum=1, maximum=10, value=3, step=1, visible=True)
|
| 91 |
category = gr.Radio(label="Category (optional)", choices=news.__CATEGORIES__, visible=True)
|
| 92 |
get = gr.Button("Get Articles", visible=True)
|
| 93 |
|
| 94 |
return num_headlines, category, get
|
| 95 |
|
| 96 |
-
def show_headline(headlines, descriptions):
|
| 97 |
headline = gr.Radio(label="News Headlines", choices=headlines, value=headlines[0], interactive=True, visible=True)
|
| 98 |
description = gr.Textbox(label="Headline Description", value=descriptions[0], visible=True)
|
| 99 |
show = gr.Button("Show Content", visible=True)
|
| 100 |
|
| 101 |
return headline, description, show
|
| 102 |
|
| 103 |
-
def show_content(
|
| 104 |
-
summarize = gr.Checkbox(label="Show Summary?", value=
|
| 105 |
-
content = gr.Textbox(label="
|
| 106 |
ready = gr.Button("Begin Quiz", visible=True)
|
| 107 |
|
| 108 |
return summarize, content, ready
|
| 109 |
|
| 110 |
-
def format_mcq(mcq):
|
| 111 |
if not mcq or not isinstance(mcq, dict):
|
| 112 |
print(f"Multiple choice question object is a {type(mcq)} but should be {type(dict())}.")
|
| 113 |
return "Invalid multiple choice question.", None
|
|
@@ -127,7 +127,7 @@ with gr.Blocks() as demo:
|
|
| 127 |
|
| 128 |
return question, options, answer
|
| 129 |
|
| 130 |
-
def hide_quiz():
|
| 131 |
quiz = [gr.Radio(visible=False) for _ in range(10)]
|
| 132 |
submit = gr.Button(visible=False)
|
| 133 |
|
|
@@ -136,7 +136,7 @@ with gr.Blocks() as demo:
|
|
| 136 |
|
| 137 |
return read, evaluation, submit, *quiz
|
| 138 |
|
| 139 |
-
def show_quiz(mcqs):
|
| 140 |
quiz = [(mcq["question"], mcq["false_answers"], mcq["correct_answer"]) for mcq in mcqs]
|
| 141 |
quiz = [(question, random.sample(distractors + [answer], 4), answer) for question, distractors, answer in quiz]
|
| 142 |
questions, options, answers = zip(*quiz) if quiz else ([], [], [])
|
|
@@ -152,7 +152,7 @@ with gr.Blocks() as demo:
|
|
| 152 |
|
| 153 |
return submit, list(answers), *quiz
|
| 154 |
|
| 155 |
-
def show_eval(eva):
|
| 156 |
evaluation = gr.Textbox(label="Evaluation", value=eva, visible=True)
|
| 157 |
read = gr.Button("Read Articles", visible=True)
|
| 158 |
|
|
@@ -162,7 +162,7 @@ with gr.Blocks() as demo:
|
|
| 162 |
###### Get and Display News Articles
|
| 163 |
######
|
| 164 |
|
| 165 |
-
def get_headline(category, num_headlines):
|
| 166 |
articles = news.get_top_headlines(category=category, num_headlines=num_headlines)
|
| 167 |
headlines, descriptions = zip(*[(article['title'], article.get('description', 'No description available.')) for article in articles])
|
| 168 |
show = show_headline(headlines, descriptions)
|
|
@@ -171,7 +171,7 @@ with gr.Blocks() as demo:
|
|
| 171 |
|
| 172 |
get.click(get_headline, inputs=[category, num_headlines], outputs=[articles, descriptions, headline, description, show])
|
| 173 |
|
| 174 |
-
def get_description(descriptions, headline):
|
| 175 |
description = "No description available."
|
| 176 |
|
| 177 |
if not descriptions: print("Descriptions are empty.")
|
|
@@ -183,7 +183,7 @@ with gr.Blocks() as demo:
|
|
| 183 |
|
| 184 |
headline.change(get_description, inputs=[descriptions, headline], outputs=[description])
|
| 185 |
|
| 186 |
-
def get_article(articles, headline):
|
| 187 |
headlines = [a['title'] for a in articles]
|
| 188 |
|
| 189 |
if headline not in headlines: return {}
|
|
@@ -191,33 +191,29 @@ with gr.Blocks() as demo:
|
|
| 191 |
|
| 192 |
show.click(get_article, inputs=[articles, headline], outputs=[article])
|
| 193 |
|
| 194 |
-
def get_content(
|
| 195 |
-
|
| 196 |
-
idx = articles.index(article)
|
| 197 |
-
articles[idx]["summary"] = model.get_summary(article, 1)
|
| 198 |
-
article = articles[idx]
|
| 199 |
-
|
| 200 |
-
return articles, article, *show_content(article.get("summary", "No summary available."))
|
| 201 |
|
| 202 |
-
article.change(get_content, inputs=[
|
| 203 |
|
| 204 |
-
def toggle_summary(article, summarize):
|
| 205 |
-
content = "No article available."
|
| 206 |
-
|
| 207 |
if not article: print("Selected article is empty.")
|
| 208 |
elif not isinstance(article, dict): print(f"Selected article is a {type(article)} but should be {type(dict())}.")
|
| 209 |
-
elif summarize:
|
| 210 |
-
|
|
|
|
|
|
|
|
|
|
| 211 |
|
| 212 |
-
return
|
| 213 |
|
| 214 |
-
summarize.change(toggle_summary, inputs=[article, summarize], outputs=[content])
|
| 215 |
|
| 216 |
######
|
| 217 |
###### Quiz Generation and Evaluation
|
| 218 |
######
|
| 219 |
|
| 220 |
-
def get_quiz(content):
|
| 221 |
multichoicequests = []
|
| 222 |
|
| 223 |
if not content: mcqs = multichoicequests
|
|
@@ -240,7 +236,7 @@ with gr.Blocks() as demo:
|
|
| 240 |
ready.click(get_quiz, inputs=[content], outputs=[
|
| 241 |
heading, num_headlines, category, get, headline, description, show, summarize, content, ready, submit, answers, *quiz])
|
| 242 |
|
| 243 |
-
def get_evaluation(answers, *quiz):
|
| 244 |
results = -1
|
| 245 |
|
| 246 |
if not answers: print("Answers are empty.")
|
|
@@ -262,7 +258,7 @@ with gr.Blocks() as demo:
|
|
| 262 |
|
| 263 |
submit.click(get_evaluation, inputs=[answers, *quiz], outputs=[evaluation, read])
|
| 264 |
|
| 265 |
-
def read_articles():
|
| 266 |
return gr.Markdown("## News Articles"), *show_news(), *hide_quiz()
|
| 267 |
|
| 268 |
read.click(read_articles, outputs=[heading, num_headlines, category, get, read, evaluation, submit, *quiz])
|
|
|
|
| 8 |
class Cooldown:
|
| 9 |
...
|
| 10 |
|
| 11 |
+
cooldown = Cooldown() ################################## News fetching cooldown in seconds
|
| 12 |
+
news = News() ########################################## Initialize the News object
|
| 13 |
+
model = GemmaLLM() ##################################### Initialize the Gemma model
|
| 14 |
|
| 15 |
# %%
|
| 16 |
with gr.Blocks() as demo:
|
|
|
|
| 20 |
###### State Variables and Components Initialization
|
| 21 |
######
|
| 22 |
|
| 23 |
+
init = ( ########################################### Initialize the Gradio interface with components and state variables
|
| 24 |
|
| 25 |
gr.Markdown("## News Articles"),
|
| 26 |
|
|
|
|
| 52 |
|
| 53 |
)
|
| 54 |
|
| 55 |
+
( ################################################## State variables and components for news articles and quiz
|
| 56 |
|
| 57 |
heading,
|
| 58 |
|
|
|
|
| 71 |
|
| 72 |
) = init
|
| 73 |
|
| 74 |
+
def hide_news(): ################################### Hide news-related components
|
| 75 |
num_headlines = gr.Slider(visible=False)
|
| 76 |
category = gr.Radio(visible=False)
|
| 77 |
get = gr.Button(visible=False)
|
|
|
|
| 86 |
|
| 87 |
return num_headlines, category, get, headline, description, show, summarize, content, ready
|
| 88 |
|
| 89 |
+
def show_news(): ################################### Show news-related components
|
| 90 |
num_headlines = gr.Slider(label="Number of Articles", minimum=1, maximum=10, value=3, step=1, visible=True)
|
| 91 |
category = gr.Radio(label="Category (optional)", choices=news.__CATEGORIES__, visible=True)
|
| 92 |
get = gr.Button("Get Articles", visible=True)
|
| 93 |
|
| 94 |
return num_headlines, category, get
|
| 95 |
|
| 96 |
+
def show_headline(headlines, descriptions): ######## Show news headlines and descriptions
|
| 97 |
headline = gr.Radio(label="News Headlines", choices=headlines, value=headlines[0], interactive=True, visible=True)
|
| 98 |
description = gr.Textbox(label="Headline Description", value=descriptions[0], visible=True)
|
| 99 |
show = gr.Button("Show Content", visible=True)
|
| 100 |
|
| 101 |
return headline, description, show
|
| 102 |
|
| 103 |
+
def show_content(content): ######################### Show article content and summary
|
| 104 |
+
summarize = gr.Checkbox(label="Show Summary?", value=False, interactive=True, visible=True)
|
| 105 |
+
content = gr.Textbox(label="Content", value=content, visible=True)
|
| 106 |
ready = gr.Button("Begin Quiz", visible=True)
|
| 107 |
|
| 108 |
return summarize, content, ready
|
| 109 |
|
| 110 |
+
def format_mcq(mcq): ############################### Format multiple choice question for quiz
|
| 111 |
if not mcq or not isinstance(mcq, dict):
|
| 112 |
print(f"Multiple choice question object is a {type(mcq)} but should be {type(dict())}.")
|
| 113 |
return "Invalid multiple choice question.", None
|
|
|
|
| 127 |
|
| 128 |
return question, options, answer
|
| 129 |
|
| 130 |
+
def hide_quiz(): ################################### Hide quiz-related components
|
| 131 |
quiz = [gr.Radio(visible=False) for _ in range(10)]
|
| 132 |
submit = gr.Button(visible=False)
|
| 133 |
|
|
|
|
| 136 |
|
| 137 |
return read, evaluation, submit, *quiz
|
| 138 |
|
| 139 |
+
def show_quiz(mcqs): ############################### Show quiz-related components
|
| 140 |
quiz = [(mcq["question"], mcq["false_answers"], mcq["correct_answer"]) for mcq in mcqs]
|
| 141 |
quiz = [(question, random.sample(distractors + [answer], 4), answer) for question, distractors, answer in quiz]
|
| 142 |
questions, options, answers = zip(*quiz) if quiz else ([], [], [])
|
|
|
|
| 152 |
|
| 153 |
return submit, list(answers), *quiz
|
| 154 |
|
| 155 |
+
def show_eval(eva): ################################ Show evaluation of user's response to the quiz
|
| 156 |
evaluation = gr.Textbox(label="Evaluation", value=eva, visible=True)
|
| 157 |
read = gr.Button("Read Articles", visible=True)
|
| 158 |
|
|
|
|
| 162 |
###### Get and Display News Articles
|
| 163 |
######
|
| 164 |
|
| 165 |
+
def get_headline(category, num_headlines): ######### Get news headlines based on selected category and number
|
| 166 |
articles = news.get_top_headlines(category=category, num_headlines=num_headlines)
|
| 167 |
headlines, descriptions = zip(*[(article['title'], article.get('description', 'No description available.')) for article in articles])
|
| 168 |
show = show_headline(headlines, descriptions)
|
|
|
|
| 171 |
|
| 172 |
get.click(get_headline, inputs=[category, num_headlines], outputs=[articles, descriptions, headline, description, show])
|
| 173 |
|
| 174 |
+
def get_description(descriptions, headline): ####### Get description for the selected headline
|
| 175 |
description = "No description available."
|
| 176 |
|
| 177 |
if not descriptions: print("Descriptions are empty.")
|
|
|
|
| 183 |
|
| 184 |
headline.change(get_description, inputs=[descriptions, headline], outputs=[description])
|
| 185 |
|
| 186 |
+
def get_article(articles, headline): ############### Get article for the selected headline
|
| 187 |
headlines = [a['title'] for a in articles]
|
| 188 |
|
| 189 |
if headline not in headlines: return {}
|
|
|
|
| 191 |
|
| 192 |
show.click(get_article, inputs=[articles, headline], outputs=[article])
|
| 193 |
|
| 194 |
+
def get_content(article): ########################## Get content for the selected article
|
| 195 |
+
return article, *show_content(article.get("content", "Content not available."))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
+
article.change(get_content, inputs=[article], outputs=[article, summarize, content, ready])
|
| 198 |
|
| 199 |
+
def toggle_summary(articles, article, summarize): ## Toggle between showing summary and full content
|
|
|
|
|
|
|
| 200 |
if not article: print("Selected article is empty.")
|
| 201 |
elif not isinstance(article, dict): print(f"Selected article is a {type(article)} but should be {type(dict())}.")
|
| 202 |
+
elif not summarize: return article.get("content", "Content not available.")
|
| 203 |
+
elif "summary" not in article:
|
| 204 |
+
idx = articles.index(article)
|
| 205 |
+
articles[idx]["summary"] = model.get_summary(article, 1)
|
| 206 |
+
article = articles[idx]
|
| 207 |
|
| 208 |
+
return article.get("summary", "Summary not available.")
|
| 209 |
|
| 210 |
+
summarize.change(toggle_summary, inputs=[articles, article, summarize], outputs=[content])
|
| 211 |
|
| 212 |
######
|
| 213 |
###### Quiz Generation and Evaluation
|
| 214 |
######
|
| 215 |
|
| 216 |
+
def get_quiz(content): ############################# Generate quiz questions from the article content
|
| 217 |
multichoicequests = []
|
| 218 |
|
| 219 |
if not content: mcqs = multichoicequests
|
|
|
|
| 236 |
ready.click(get_quiz, inputs=[content], outputs=[
|
| 237 |
heading, num_headlines, category, get, headline, description, show, summarize, content, ready, submit, answers, *quiz])
|
| 238 |
|
| 239 |
+
def get_evaluation(answers, *quiz): ################ Evaluate the user's responses to the quiz
|
| 240 |
results = -1
|
| 241 |
|
| 242 |
if not answers: print("Answers are empty.")
|
|
|
|
| 258 |
|
| 259 |
submit.click(get_evaluation, inputs=[answers, *quiz], outputs=[evaluation, read])
|
| 260 |
|
| 261 |
+
def read_articles(): ############################### Reset the interface to read articles again
|
| 262 |
return gr.Markdown("## News Articles"), *show_news(), *hide_quiz()
|
| 263 |
|
| 264 |
read.click(read_articles, outputs=[heading, num_headlines, category, get, read, evaluation, submit, *quiz])
|