Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,59 @@
|
|
| 1 |
import os
|
| 2 |
-
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 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 |
-
|
| 16 |
-
question
|
| 17 |
-
context
|
|
|
|
|
|
|
| 18 |
)
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
return "Not a valid question"
|
| 22 |
|
| 23 |
-
return
|
|
|
|
| 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()
|