Ceshine Lee commited on
Commit
d38d726
·
1 Parent(s): 46ff5db

Invoke inference API manually

Browse files
Files changed (2) hide show
  1. .gitignores +1 -0
  2. app.py +58 -6
.gitignores ADDED
@@ -0,0 +1 @@
 
 
1
+ secrets*
app.py CHANGED
@@ -1,11 +1,63 @@
 
 
 
 
1
  import gradio as gr
 
 
 
 
 
 
2
 
3
- interface = gr.Interface.load(
4
- "huggingface/ceshine/t5-paraphrase-quora-paws",
5
- description="Paraphrasing model trained on PAWS and Quora datasets",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  examples=[
7
  ["I bought a ticket from London to New York."],
8
- ["Weh Seun spends 14 hours a week doing housework."]
9
- ]
10
  )
11
- interface.launch()
 
 
1
+ import os
2
+ import json
3
+
4
+ import requests
5
  import gradio as gr
6
+ from gradio import inputs, outputs
7
+
8
+ ENDPOINT = (
9
+ "https://api-inference.huggingface.co/models/ceshine/t5-paraphrase-quora-paws"
10
+ )
11
+
12
 
13
+ def paraphrase(source_text):
14
+ res = requests.post(
15
+ ENDPOINT,
16
+ headers={"Authorization": f"Bearer {os.environ['TOKEN']}"},
17
+ data=json.dumps(
18
+ {
19
+ "inputs": source_text,
20
+ "parameters": {
21
+ # "do_sample": True,
22
+ "num_beams": 10,
23
+ "top_k": 5,
24
+ "repetition_penalty": 2.0,
25
+ "temperature": 1.5,
26
+ "num_return_sequences": 10,
27
+ "max_length": 200,
28
+ },
29
+ }
30
+ ),
31
+ )
32
+ if not (res.status_code == 200):
33
+ raise ValueError(
34
+ "Could not complete request to HuggingFace API, Error {}".format(
35
+ res.status_code
36
+ )
37
+ )
38
+ results = res.json()
39
+ print(results)
40
+ outputs = [
41
+ x["generated_text"]
42
+ for x in results
43
+ if x["generated_text"].lower() != source_text.lower().strip()
44
+ ][:3]
45
+ text = ""
46
+ for i, output in enumerate(outputs):
47
+ text += f"{i+1}: {output}\n\n"
48
+ return text
49
+
50
+
51
+ interface = gr.Interface(
52
+ fn=paraphrase,
53
+ inputs=inputs.Textbox(label="Input"),
54
+ outputs=outputs.Textbox(label="Generated text:"),
55
+ title="T5 Sentence Paraphraser",
56
+ description="A paraphrasing model trained on PAWS and Quora datasets",
57
  examples=[
58
  ["I bought a ticket from London to New York."],
59
+ ["Weh Seun spends 14 hours a week doing housework."],
60
+ ],
61
  )
62
+
63
+ interface.launch(enable_queue=True)