Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -94,34 +94,17 @@ def answer_question(news_content, question):
|
|
| 94 |
start_idx = torch.argmax(outputs.start_logits)
|
| 95 |
end_idx = torch.argmax(outputs.end_logits) + 1
|
| 96 |
|
|
|
|
| 97 |
answer = qa_tokenizer.decode(inputs.input_ids[0][start_idx:end_idx],
|
| 98 |
skip_special_tokens=True,
|
| 99 |
clean_up_tokenization_spaces=True)
|
| 100 |
|
| 101 |
confidence = torch.max(torch.softmax(outputs.start_logits, dim=1)).item()
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
words = nltk.word_tokenize(news_content)
|
| 105 |
-
sentences = nltk.sent_tokenize(news_content)
|
| 106 |
-
word_count = len(words)
|
| 107 |
-
sentence_count = len(sentences)
|
| 108 |
-
reading_time = round(word_count / 200, 2) # ~200 words/minute
|
| 109 |
-
|
| 110 |
-
stats = f"Word count: {word_count}\nSentences: {sentence_count}\nEstimated reading time: {reading_time} min"
|
| 111 |
-
|
| 112 |
-
# ====== Word Cloud ======
|
| 113 |
-
wc = WordCloud(width=800, height=400, background_color="white").generate(news_content)
|
| 114 |
-
fig, ax = plt.subplots()
|
| 115 |
-
ax.imshow(wc, interpolation="bilinear")
|
| 116 |
-
ax.axis("off")
|
| 117 |
-
buf = io.BytesIO()
|
| 118 |
-
plt.savefig(buf, format="png")
|
| 119 |
-
buf.seek(0)
|
| 120 |
-
|
| 121 |
-
return f"**Answer:** {answer.strip()}\n\n**Confidence:** {confidence:.2%}\n\n{stats}", buf
|
| 122 |
|
| 123 |
except Exception as e:
|
| 124 |
-
return f"Error: {str(e)}"
|
| 125 |
|
| 126 |
|
| 127 |
# ====================== GRADIO INTERFACE ======================
|
|
@@ -149,18 +132,18 @@ with gr.Blocks(title="Daily Mirror News Classifier") as demo:
|
|
| 149 |
outputs=[output_text, output_file,bar_chart]
|
| 150 |
)
|
| 151 |
|
| 152 |
-
|
| 153 |
gr.Markdown("Ask any question about a news article")
|
| 154 |
news_input = gr.Textbox(lines=12, label="Paste News Content", placeholder="Paste the full news article here...")
|
| 155 |
question_input = gr.Textbox(label="Your Question", placeholder="e.g. What is the main topic?")
|
| 156 |
qa_btn = gr.Button("🔍 Get Answer", variant="primary")
|
| 157 |
-
qa_output = gr.Textbox(label="Answer
|
| 158 |
-
|
| 159 |
-
|
| 160 |
qa_btn.click(
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
|
|
|
| 164 |
|
| 165 |
gr.Markdown("Built for Text Analytics Assignment - Section 02")
|
| 166 |
|
|
|
|
| 94 |
start_idx = torch.argmax(outputs.start_logits)
|
| 95 |
end_idx = torch.argmax(outputs.end_logits) + 1
|
| 96 |
|
| 97 |
+
# Clean answer - remove question repetition and special tokens
|
| 98 |
answer = qa_tokenizer.decode(inputs.input_ids[0][start_idx:end_idx],
|
| 99 |
skip_special_tokens=True,
|
| 100 |
clean_up_tokenization_spaces=True)
|
| 101 |
|
| 102 |
confidence = torch.max(torch.softmax(outputs.start_logits, dim=1)).item()
|
| 103 |
+
|
| 104 |
+
return f"**Answer:** {answer.strip()}\n\n**Confidence:** {confidence:.2%}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
except Exception as e:
|
| 107 |
+
return f"Error: {str(e)}"
|
| 108 |
|
| 109 |
|
| 110 |
# ====================== GRADIO INTERFACE ======================
|
|
|
|
| 132 |
outputs=[output_text, output_file,bar_chart]
|
| 133 |
)
|
| 134 |
|
| 135 |
+
with gr.Tab("❓ Question Answering"):
|
| 136 |
gr.Markdown("Ask any question about a news article")
|
| 137 |
news_input = gr.Textbox(lines=12, label="Paste News Content", placeholder="Paste the full news article here...")
|
| 138 |
question_input = gr.Textbox(label="Your Question", placeholder="e.g. What is the main topic?")
|
| 139 |
qa_btn = gr.Button("🔍 Get Answer", variant="primary")
|
| 140 |
+
qa_output = gr.Textbox(label="Answer", lines=5)
|
| 141 |
+
|
|
|
|
| 142 |
qa_btn.click(
|
| 143 |
+
fn=answer_question,
|
| 144 |
+
inputs=[news_input, question_input],
|
| 145 |
+
outputs=qa_output
|
| 146 |
+
)
|
| 147 |
|
| 148 |
gr.Markdown("Built for Text Analytics Assignment - Section 02")
|
| 149 |
|