nkasmanoff commited on
Commit
a40cc28
·
1 Parent(s): 26177cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -78
app.py CHANGED
@@ -3,13 +3,13 @@ import gradio as gr
3
  from PIL import Image
4
  from transformers import AutoProcessor, AutoModelForCausalLM
5
  import spacy
6
- from spacy.matcher import Matcher
7
  device='cpu'
8
 
9
  processor = AutoProcessor.from_pretrained("microsoft/git-base")
10
  model = AutoModelForCausalLM.from_pretrained("nkasmanoff/sky-scribe").to(device)
11
 
12
- nlp = spacy.load('en_core_web_sm')
13
 
14
  def predict(image,max_length=50,device='cpu'):
15
  pixel_values = processor(images=image, return_tensors="pt").to(device).pixel_values
@@ -19,82 +19,6 @@ def predict(image,max_length=50,device='cpu'):
19
  return generated_caption
20
 
21
 
22
- def get_entities(sent):
23
- ## chunk 1
24
- ent1 = ""
25
- ent2 = ""
26
-
27
- prv_tok_dep = "" # dependency tag of previous token in the sentence
28
- prv_tok_text = "" # previous token in the sentence
29
-
30
- prefix = ""
31
- modifier = ""
32
-
33
- #############################################################
34
-
35
- for tok in nlp(sent):
36
- ## chunk 2
37
- # if token is a punctuation mark then move on to the next token
38
- if tok.dep_ != "punct":
39
- # check: token is a compound word or not
40
- if tok.dep_ == "compound":
41
- prefix = tok.text
42
- # if the previous word was also a 'compound' then add the current word to it
43
- if prv_tok_dep == "compound":
44
- prefix = prv_tok_text + " " + tok.text
45
-
46
- # check: token is a modifier or not
47
- if tok.dep_.endswith("mod") == True:
48
- modifier = tok.text
49
- # if the previous word was also a 'compound' then add the current word to it
50
- if prv_tok_dep == "compound":
51
- modifier = prv_tok_text + " " + tok.text
52
-
53
- ## chunk 3
54
- if tok.dep_.find("subj") == True:
55
- ent1 = modifier + " " + prefix + " " + tok.text
56
- prefix = ""
57
- modifier = ""
58
- prv_tok_dep = ""
59
- prv_tok_text = ""
60
-
61
- ## chunk 4
62
- if tok.dep_.find("obj") == True:
63
- ent2 = modifier + " " + prefix + " " + tok.text
64
-
65
- ## chunk 5
66
- # update variables
67
- prv_tok_dep = tok.dep_
68
- prv_tok_text = tok.text
69
- #############################################################
70
-
71
- return [ent1.strip(), ent2.strip()]
72
-
73
-
74
-
75
-
76
- def get_relation(sent):
77
-
78
- doc = nlp(sent)
79
-
80
- # Matcher class object
81
- matcher = Matcher(nlp.vocab)
82
-
83
- #define the pattern
84
- pattern = [{'DEP':'ROOT'},
85
- {'DEP':'prep','OP':"?"},
86
- {'DEP':'agent','OP':"?"},
87
- {'POS':'ADJ','OP':"?"}]
88
-
89
- matcher.add('matching_pattern', patterns=[pattern])
90
- matches = matcher(doc)
91
- k = len(matches) - 1
92
-
93
- span = doc[matches[k][1]:matches[k][2]]
94
-
95
- return(span.text)
96
-
97
-
98
 
99
  input = gr.inputs.Image(label="Please upload an image", type = 'pil', optional=True)
100
  output = gr.outputs.Textbox(type="text",label="Captions")
 
3
  from PIL import Image
4
  from transformers import AutoProcessor, AutoModelForCausalLM
5
  import spacy
6
+
7
  device='cpu'
8
 
9
  processor = AutoProcessor.from_pretrained("microsoft/git-base")
10
  model = AutoModelForCausalLM.from_pretrained("nkasmanoff/sky-scribe").to(device)
11
 
12
+
13
 
14
  def predict(image,max_length=50,device='cpu'):
15
  pixel_values = processor(images=image, return_tensors="pt").to(device).pixel_values
 
19
  return generated_caption
20
 
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
  input = gr.inputs.Image(label="Please upload an image", type = 'pil', optional=True)
24
  output = gr.outputs.Textbox(type="text",label="Captions")