Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
from PIL import Image
|
4 |
+
# import pickle
|
5 |
+
import json
|
6 |
+
import numpy as np
|
7 |
+
# from fastapi import FastAPI,Response
|
8 |
+
# from sklearn.metrics import accuracy_score, f1_score
|
9 |
+
# import prometheus_client as prom
|
10 |
+
import pandas as pd
|
11 |
+
# import uvicorn
|
12 |
+
import os
|
13 |
+
from transformers import VisionEncoderDecoderModel,pipeline, ViTImageProcessor, AutoTokenizer
|
14 |
+
import torch
|
15 |
+
|
16 |
+
|
17 |
+
#model
|
18 |
+
|
19 |
+
# loaded_model = pickle.load(open(save_file_name, 'rb'))
|
20 |
+
|
21 |
+
# app=FastAPI()
|
22 |
+
|
23 |
+
|
24 |
+
# test_data=pd.read_csv("test.csv")
|
25 |
+
|
26 |
+
|
27 |
+
# f1_metric = prom.Gauge('death_f1_score', 'F1 score for test samples')
|
28 |
+
|
29 |
+
# Function for updating metrics
|
30 |
+
# def update_metrics():
|
31 |
+
# test = test_data.sample(20)
|
32 |
+
# X = test.iloc[:, :-1].values
|
33 |
+
# y = test['DEATH_EVENT'].values
|
34 |
+
|
35 |
+
# # test_text = test['Text'].values
|
36 |
+
# test_pred = loaded_model.predict(X)
|
37 |
+
# #pred_labels = [int(pred['label'].split("_")[1]) for pred in test_pred]
|
38 |
+
|
39 |
+
# f1 = f1_score( y , test_pred).round(3)
|
40 |
+
|
41 |
+
# #f1 = f1_score(test['labels'], pred_labels).round(3)
|
42 |
+
|
43 |
+
# f1_metric.set(f1)
|
44 |
+
|
45 |
+
|
46 |
+
|
47 |
+
with open("model/config.json") as f:
|
48 |
+
n=json.load(f)
|
49 |
+
encoder_name_or_path=n["encoder"]["_name_or_path"]
|
50 |
+
decoder_name_or_path=n["decoder"]["_name_or_path"]
|
51 |
+
|
52 |
+
|
53 |
+
print(encoder_name_or_path,decoder_name_or_path,)
|
54 |
+
model = VisionEncoderDecoderModel.from_encoder_decoder_pretrained(encoder_name_or_path,decoder_name_or_path)
|
55 |
+
|
56 |
+
|
57 |
+
tokenizer = AutoTokenizer.from_pretrained(decoder_name_or_path)
|
58 |
+
tokenizer.pad_token = tokenizer.unk_token
|
59 |
+
|
60 |
+
|
61 |
+
|
62 |
+
feature_extractor = ViTImageProcessor.from_pretrained(encoder_name_or_path)
|
63 |
+
|
64 |
+
|
65 |
+
|
66 |
+
|
67 |
+
|
68 |
+
|
69 |
+
|
70 |
+
|
71 |
+
|
72 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
73 |
+
|
74 |
+
# cap_model.to(device)
|
75 |
+
|
76 |
+
# def generate_caption(model, image, tokenizer=None):
|
77 |
+
|
78 |
+
|
79 |
+
# generated_ids = model.generate(pixel_values=inputs.pixel_values)
|
80 |
+
# print("generated_ids",generated_ids)
|
81 |
+
|
82 |
+
# if tokenizer is not None:
|
83 |
+
# print("tokenizer not null--",tokenizer)
|
84 |
+
# generated_caption = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
85 |
+
# else:
|
86 |
+
# print("tokenizer null--",tokenizer)
|
87 |
+
# generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
88 |
+
|
89 |
+
# return generated_caption
|
90 |
+
|
91 |
+
|
92 |
+
|
93 |
+
|
94 |
+
|
95 |
+
def predict_event(image):
|
96 |
+
|
97 |
+
generated_caption = tokenizer.decode(model.generate(feature_extractor(image, return_tensors="pt").pixel_values.to(device))[0])
|
98 |
+
|
99 |
+
return '\033[96m' +generated_caption+ '\033[0m'
|
100 |
+
|
101 |
+
|
102 |
+
|
103 |
+
|
104 |
+
# @app.get("/metrics")
|
105 |
+
# async def get_metrics():
|
106 |
+
# update_metrics()
|
107 |
+
# return Response(media_type="text/plain", content= prom.generate_latest())
|
108 |
+
|
109 |
+
|
110 |
+
|
111 |
+
title = "capstone"
|
112 |
+
description = "final capstone"
|
113 |
+
|
114 |
+
|
115 |
+
# inputs=gr.inputs.Image(type="pil")
|
116 |
+
|
117 |
+
iface = gr.Interface(predict_event,
|
118 |
+
inputs=["image"],
|
119 |
+
# gr.Image(type="pil"),
|
120 |
+
outputs=["text"] )
|
121 |
+
|
122 |
+
|
123 |
+
iface.launch()
|
124 |
+
|
125 |
+
|
126 |
+
# app = gr.mount_gradio_app(app, iface, path="/")
|
127 |
+
|
128 |
+
# iface.launch(server_name = "0.0.0.0", server_port = 8001,share=True)
|
129 |
+
|
130 |
+
# if __name__ == "__main__":
|
131 |
+
# Use this for debugging purposes only
|
132 |
+
|
133 |
+
# uvicorn.run(app, host="0.0.0.0", port=8001)
|