harishforaiandml commited on
Commit
ab96168
·
verified ·
1 Parent(s): 3a96c2e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -12
app.py CHANGED
@@ -1,34 +1,59 @@
1
  import os
2
- from transformers import pipeline
3
  import gradio as gr
4
 
5
- HF_TOKEN = os.getenv("HF_TOKEN")
6
-
7
- qa = pipeline(
8
- "question-answering",
9
- model="harishforaiandml/my-pretrained-qa-model",
10
- token=HF_TOKEN # 👈 IMPORTANT FIX
11
  )
12
 
 
 
 
 
 
 
 
 
 
13
  def ask(context, question):
14
 
15
- result = qa(
16
- question=question,
17
- context=context
 
 
18
  )
19
 
20
- if result["score"] < 0.1:
 
 
 
 
 
 
 
 
 
 
 
 
21
  return "Not a valid question"
22
 
23
- return result["answer"]
 
24
 
25
  demo = gr.Interface(
26
  fn=ask,
 
27
  inputs=[
28
  gr.Textbox(lines=6, label="Context"),
29
  gr.Textbox(label="Question")
30
  ],
 
31
  outputs="text",
 
 
32
  )
33
 
34
  demo.launch()
 
1
  import os
2
+ import torch
3
  import gradio as gr
4
 
5
+ from transformers import (
6
+ AutoTokenizer,
7
+ AutoModelForQuestionAnswering
 
 
 
8
  )
9
 
10
+ MODEL_NAME = "harishforaiandml/my-pretrained-qa-model"
11
+
12
+ # Load tokenizer + model manually (SAFE for Spaces)
13
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
14
+ model = AutoModelForQuestionAnswering.from_pretrained(MODEL_NAME)
15
+
16
+ model.eval()
17
+
18
+
19
  def ask(context, question):
20
 
21
+ inputs = tokenizer(
22
+ question,
23
+ context,
24
+ return_tensors="pt",
25
+ truncation=True
26
  )
27
 
28
+ with torch.no_grad():
29
+ outputs = model(**inputs)
30
+
31
+ start_logits = outputs.start_logits
32
+ end_logits = outputs.end_logits
33
+
34
+ start_idx = torch.argmax(start_logits)
35
+ end_idx = torch.argmax(end_logits) + 1
36
+
37
+ answer_tokens = inputs["input_ids"][0][start_idx:end_idx]
38
+ answer = tokenizer.decode(answer_tokens, skip_special_tokens=True)
39
+
40
+ if answer.strip() == "":
41
  return "Not a valid question"
42
 
43
+ return answer
44
+
45
 
46
  demo = gr.Interface(
47
  fn=ask,
48
+
49
  inputs=[
50
  gr.Textbox(lines=6, label="Context"),
51
  gr.Textbox(label="Question")
52
  ],
53
+
54
  outputs="text",
55
+
56
+ title="QA Model (Your HF Model)"
57
  )
58
 
59
  demo.launch()