Madalina Vatamanelu commited on
Commit
cdfc8fe
1 Parent(s): 91c38dc

Extend the app to call the 3 models

Browse files
Files changed (1) hide show
  1. app.py +37 -0
app.py CHANGED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import os
4
+
5
+
6
+
7
+ API_URL1 = "https://api-inference.huggingface.co/models/cardiffnlp/twitter-roberta-base-sentiment"
8
+ API_URL2 = "https://api-inference.huggingface.co/models/facebook/convnext-xlarge-384-22k-1k"
9
+ API_URL3 = "https://api-inference.huggingface.co/models/microsoft/trocr-base-handwritten"
10
+
11
+ bt = os.environ['HACKAITHONBEARERTOKEN']
12
+ headers = {"Authorization": bt }
13
+
14
+ def query(mood, select_model, filepath):
15
+
16
+ print (select_model);
17
+ print (filepath);
18
+
19
+
20
+ if (select_model=="Sentiment"):
21
+ response = requests.post(API_URL1, headers=headers, json=mood)
22
+ elif (select_model=="WhatIsThat"):
23
+ data = open(filepath, 'rb' ).read()
24
+ response = requests.post(API_URL2, headers=headers, data=data)
25
+ else:
26
+ data = open(filepath, 'rb' ).read()
27
+ response = requests.post(API_URL3, headers=headers, data=data)
28
+ return str(response.json())
29
+
30
+ def greet(mood,select_model,image):
31
+ output = query({"inputs":mood}, select_model, image)
32
+ print (str(output))
33
+ return str(output)
34
+
35
+ iface = gr.Interface(
36
+ fn=greet, inputs=["text", gr.Radio(choices=["Sentiment", "WhatIsThat", "HandWriting"],value="Sentiment"),gr.Image(type="filepath")], outputs="text")
37
+ iface.launch()