Ravi21 commited on
Commit
a7a53ff
1 Parent(s): bc14224

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -24
app.py CHANGED
@@ -1,7 +1,6 @@
1
- import pandas as pd
2
- import numpy as np
3
  import gradio as gr
4
  import torch
 
5
  from transformers import AutoModelForMultipleChoice, AutoTokenizer
6
 
7
  model_id = "deepset/deberta-v3-large-squad2"
@@ -11,36 +10,51 @@ model = AutoModelForMultipleChoice.from_pretrained(model_id)
11
  tokenizer = AutoTokenizer.from_pretrained(model_id)
12
 
13
  # Define the preprocessing function
14
- def preprocess(sample):
15
- first_sentences = [sample["prompt"]] * 5
16
- second_sentences = [sample[option] for option in "ABCDE"]
17
- tokenized_sentences = tokenizer(first_sentences, second_sentences, truncation=True, padding=True, return_tensors="pt")
18
- sample["input_ids"] = tokenized_sentences["input_ids"]
19
- sample["attention_mask"] = tokenized_sentences["attention_mask"]
20
- return sample
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  # Define the prediction function
23
  def predict(data):
24
- inputs = torch.stack(data["input_ids"])
25
- masks = torch.stack(data["attention_mask"])
26
- with torch.no_grad():
27
- logits = model(inputs, attention_mask=masks).logits
28
- predictions_as_ids = torch.argsort(-logits, dim=1)
29
- answers = np.array(list("ABCDE"))[predictions_as_ids.tolist()]
30
- return ["".join(i) for i in answers[:, :3]]
31
- text=gr.Textbox(placeholder="paste multiple choice questions.....")
32
- label=gr.Label(num_top_classes=3)
 
 
 
 
 
33
  # Create the Gradio interface
34
  iface = gr.Interface(
35
  fn=predict,
36
- inputs=text , # Use the correct class with type="json"
37
- outputs=label,
38
  live=True,
39
- examples=[
40
- {"prompt": "This is the prompt", "A": "Option A text", "B": "Option B text", "C": "Option C text", "D": "Option D text", "E": "Option E text"}
41
- ],
42
  title="LLM Science Exam Demo",
43
- description="Enter the prompt and options (A to E) below and get predictions.",
44
  )
45
 
46
  # Run the interface
 
 
 
1
  import gradio as gr
2
  import torch
3
+ import numpy as np
4
  from transformers import AutoModelForMultipleChoice, AutoTokenizer
5
 
6
  model_id = "deepset/deberta-v3-large-squad2"
 
10
  tokenizer = AutoTokenizer.from_pretrained(model_id)
11
 
12
  # Define the preprocessing function
13
+ def preprocess(text):
14
+ # Split the input text into lines
15
+ lines = text.strip().split("\n")
16
+ samples = []
17
+
18
+ # Loop through each line and create a sample
19
+ for line in lines:
20
+ parts = line.split("\t")
21
+ if len(parts) >= 6:
22
+ sample = {
23
+ "prompt": parts[0],
24
+ "A": parts[1],
25
+ "B": parts[2],
26
+ "C": parts[3],
27
+ "D": parts[4],
28
+ "E": parts[5]
29
+ }
30
+ samples.append(sample)
31
+
32
+ return samples
33
 
34
  # Define the prediction function
35
  def predict(data):
36
+ results = []
37
+ for sample in data:
38
+ first_sentences = [sample["prompt"]] * 5
39
+ second_sentences = [sample[option] for option in "ABCDE"]
40
+ tokenized_sentences = tokenizer(first_sentences, second_sentences, truncation=True, padding=True, return_tensors="pt")
41
+ inputs = tokenized_sentences["input_ids"]
42
+ masks = tokenized_sentences["attention_mask"]
43
+ with torch.no_grad():
44
+ logits = model(inputs, attention_mask=masks).logits
45
+ predictions_as_ids = torch.argsort(-logits, dim=1)
46
+ answers = np.array(list("ABCDE"))[predictions_as_ids.tolist()]
47
+ results.append(["".join(i) for i in answers[:, :3]])
48
+ return results
49
+
50
  # Create the Gradio interface
51
  iface = gr.Interface(
52
  fn=predict,
53
+ inputs=gr.inputs.Textbox(placeholder="Paste multiple-choice questions (prompt and options separated by tabs, one question per line) ..."),
54
+ outputs=gr.outputs.Label(num_top_classes=3),
55
  live=True,
 
 
 
56
  title="LLM Science Exam Demo",
57
+ description="Enter multiple-choice questions (prompt and options) below and get predictions.",
58
  )
59
 
60
  # Run the interface