Ginidu2003 commited on
Commit
92f17aa
·
verified ·
1 Parent(s): cc96e3d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -6
app.py CHANGED
@@ -30,7 +30,10 @@ def preprocess_text(text):
30
 
31
  # ====================== MODELS ======================
32
  classifier_model = "Ginidu2003/Distilbert-Base-News-classifier"
33
- qa_model_name = "deepset/roberta-base-squad2"
 
 
 
34
 
35
  # Load QA pipeline using a supported method
36
  qa_pipeline = pipeline("document-question-answering", model=qa_model_name, device=-1)
@@ -75,13 +78,27 @@ def classify_csv(file):
75
  def answer_question(news_content, question):
76
  if not news_content.strip() or not question.strip():
77
  return "Please enter both news content and a question."
 
78
  try:
79
- result = qa_pipeline(question=question, context=news_content)
80
- answer = result[0]['answer'] if isinstance(result, list) else result['answer']
81
- score = result[0]['score'] if isinstance(result, list) else result['score']
82
- return f"**Answer:** {answer}\n\n**Confidence:** {score:.2%}"
 
 
 
 
 
 
 
 
 
 
 
 
 
83
  except Exception as e:
84
- return f"Error processing question: {str(e)}"
85
 
86
  # ====================== GRADIO INTERFACE ======================
87
  with gr.Blocks(title="Daily Mirror News Classifier") as demo:
 
30
 
31
  # ====================== MODELS ======================
32
  classifier_model = "Ginidu2003/Distilbert-Base-News-classifier"
33
+ from transformers import AutoTokenizer, AutoModelForQuestionAnswering
34
+
35
+ qa_tokenizer = AutoTokenizer.from_pretrained("deepset/roberta-base-squad2")
36
+ qa_model = AutoModelForQuestionAnswering.from_pretrained("deepset/roberta-base-squad2")
37
 
38
  # Load QA pipeline using a supported method
39
  qa_pipeline = pipeline("document-question-answering", model=qa_model_name, device=-1)
 
78
  def answer_question(news_content, question):
79
  if not news_content.strip() or not question.strip():
80
  return "Please enter both news content and a question."
81
+
82
  try:
83
+ inputs = qa_tokenizer(question, news_content, return_tensors="pt", truncation=True, max_length=512)
84
+
85
+ with torch.no_grad():
86
+ outputs = qa_model(**inputs)
87
+
88
+ start_scores = outputs.start_logits
89
+ end_scores = outputs.end_logits
90
+
91
+ start_idx = torch.argmax(start_scores)
92
+ end_idx = torch.argmax(end_scores) + 1
93
+
94
+ answer = qa_tokenizer.decode(inputs.input_ids[0][start_idx:end_idx])
95
+
96
+ confidence = torch.max(torch.softmax(start_scores, dim=1)).item()
97
+
98
+ return f"**Answer:** {answer}\n\n**Confidence:** {confidence:.2%}"
99
+
100
  except Exception as e:
101
+ return f"Error: {str(e)}"
102
 
103
  # ====================== GRADIO INTERFACE ======================
104
  with gr.Blocks(title="Daily Mirror News Classifier") as demo: