Spaces:
Sleeping
Sleeping
matiasbonfanti
commited on
Commit
•
bd8a26c
1
Parent(s):
1f3531d
Upload app.py
Browse files
app.py
CHANGED
@@ -1,138 +1,191 @@
|
|
1 |
-
|
2 |
import gradio as gr
|
3 |
-
import
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
import os
|
11 |
-
import shutil
|
12 |
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
model = YOLO("seg-640.pt")
|
23 |
-
results = model.predict(source=imagen_path, save=True, save_txt=True, project="diarios", classes=0, conf=0.5)
|
24 |
|
25 |
-
# cargar imagen tif
|
26 |
-
image = cv2.imread(imagen_path)
|
27 |
|
28 |
-
# crear una carpeta para guardar los recortes
|
29 |
-
filename = os.path.splitext(os.path.basename(results[0].path))[0]
|
30 |
-
foldername = os.path.splitext(filename)[0]
|
31 |
-
if not os.path.exists(f"recorte/{foldername}"):
|
32 |
-
os.makedirs(f"recorte/{foldername}")
|
33 |
|
34 |
-
datos = results[0].masks.xy
|
35 |
|
36 |
-
num_notas = 0
|
37 |
|
38 |
-
for i, coords in enumerate(datos):
|
39 |
-
# coordenadas de segmentación
|
40 |
-
coordenadas = np.array(coords)
|
41 |
|
42 |
-
# crear una máscara vacía
|
43 |
-
mask = np.zeros((image.shape[0], image.shape[1]), dtype=np.uint8)
|
44 |
|
45 |
-
# dibujar un polígono en la máscara
|
46 |
-
cv2.fillPoly(mask, [coordenadas.astype(np.int32)], 255)
|
47 |
|
48 |
-
# aplicar la máscara a la imagen
|
49 |
-
masked = cv2.bitwise_and(image, image, mask=mask)
|
50 |
|
51 |
-
# recortar la imagen utilizando la caja delimitadora de la máscara
|
52 |
-
x, y, w, h = cv2.boundingRect(mask)
|
53 |
-
recorte = masked[y:y+h, x:x+w]
|
54 |
|
55 |
-
# guardar el recorte como una imagen
|
56 |
-
recorte_path = os.path.join(f"recorte/{foldername}", f"nota {i}.jpg")
|
57 |
-
cv2.imwrite(recorte_path, recorte)
|
58 |
|
59 |
-
num_notas += 1
|
60 |
-
|
61 |
-
noticias(foldername)
|
62 |
-
segmentacion = f"diarios/predict/{filename}.png"
|
63 |
-
segmentacion = cv2.imread(segmentacion)
|
64 |
-
recorte1 = f"recorte/{foldername}/nota 0.jpg"
|
65 |
-
prediccion = f"deteccion/{foldername}/predict/nota 0.jpg"
|
66 |
-
|
67 |
-
try:
|
68 |
-
recorte2 = f"recorte/{foldername}/nota 1.jpg"
|
69 |
-
prediccion2 = f"deteccion/{foldername}/predict/nota 1.jpg"
|
70 |
-
if not os.path.exists(recorte2):
|
71 |
-
raise Exception(f"El archivo {recorte2} no existe.")
|
72 |
-
except:
|
73 |
-
recorte2 = "sin_nota.jpg"
|
74 |
-
prediccion2 = "sin_nota.jpg"
|
75 |
-
|
76 |
-
return segmentacion, prediccion, prediccion2, num_notas
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
with gr.Blocks() as demo:
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
gr.Markdown(
|
94 |
"""
|
95 |
<p align="center">
|
96 |
-
<img width = 600 src="https://
|
97 |
-
|
|
|
|
|
|
|
|
|
98 |
</p>
|
99 |
|
100 |
"""
|
101 |
)
|
102 |
-
|
103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
gr.Markdown(
|
105 |
"""
|
106 |
-
- Puede
|
107 |
-
-
|
108 |
"""
|
109 |
)
|
110 |
-
with gr.
|
111 |
-
seed = gr.
|
112 |
-
with gr.Row():
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
gr.Examples(["Popular.jpg"], inputs=[seed])
|
119 |
-
gr.Examples(["Clarin3.jpg"], inputs=[seed])
|
120 |
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
with gr.Column():
|
127 |
-
|
128 |
-
|
129 |
-
|
|
|
130 |
with gr.Column():
|
131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
|
|
|
133 |
with gr.Row():
|
134 |
-
|
135 |
-
|
136 |
-
|
|
|
|
|
|
|
|
|
137 |
if __name__ == "__main__":
|
138 |
demo.launch()
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import openai
|
4 |
+
from deep_translator import GoogleTranslator
|
5 |
+
|
6 |
+
|
7 |
+
|
8 |
+
|
9 |
import os
|
|
|
10 |
|
11 |
|
12 |
+
openkey = os.getenv("openkey")
|
13 |
+
yelpkey = os.getenv("yelpkey")
|
14 |
+
|
15 |
+
|
16 |
+
|
17 |
+
|
18 |
+
def generate_text(text):
|
19 |
+
url = f"https://api.yelp.com/v3/businesses/{text}/reviews?limit=1&sort_by=yelp_sort"
|
20 |
+
|
21 |
+
headers = {
|
22 |
+
"accept": "application/json",
|
23 |
+
"Authorization": yelpkey
|
24 |
+
}
|
25 |
+
|
26 |
+
response = requests.get(url, headers=headers)
|
27 |
+
response = response.json()
|
28 |
+
review = response['reviews'][0]['text']
|
29 |
+
user = response['reviews'][0]['user']['name']
|
30 |
+
image = response['reviews'][0]['user']['image_url']
|
31 |
+
link = response['reviews'][0]['url']
|
32 |
+
calificacion = response['reviews'][0]['rating']
|
33 |
+
creacion = response['reviews'][0]['time_created']
|
34 |
+
openai.api_key = openkey
|
35 |
+
traductor = GoogleTranslator(source='en', target='es')
|
36 |
+
traduccion = traductor.translate(review)
|
37 |
+
response2 = openai.Completion.create(
|
38 |
+
model="text-davinci-003",
|
39 |
+
prompt= f"Responder la siguiente reseña. nombre:{user}. {review}",
|
40 |
+
temperature=0.5,
|
41 |
+
max_tokens=300,
|
42 |
+
top_p=1,
|
43 |
+
frequency_penalty=0,
|
44 |
+
presence_penalty=0
|
45 |
+
)
|
46 |
+
response2 = response2.choices[0].text
|
47 |
|
|
|
|
|
48 |
|
|
|
|
|
49 |
|
|
|
|
|
|
|
|
|
|
|
50 |
|
|
|
51 |
|
|
|
52 |
|
|
|
|
|
|
|
53 |
|
|
|
|
|
54 |
|
|
|
|
|
55 |
|
|
|
|
|
56 |
|
|
|
|
|
|
|
57 |
|
|
|
|
|
|
|
58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
+
|
61 |
+
|
62 |
+
|
63 |
+
|
64 |
+
return review,user,image,response2,traduccion,calificacion,creacion
|
65 |
+
|
66 |
+
|
67 |
+
|
68 |
+
|
69 |
+
|
70 |
+
|
71 |
+
|
72 |
+
|
73 |
+
|
74 |
+
|
75 |
+
|
76 |
+
|
77 |
+
|
78 |
+
|
79 |
+
|
80 |
+
|
81 |
+
|
82 |
+
|
83 |
+
|
84 |
+
|
85 |
+
|
86 |
+
|
87 |
+
|
88 |
+
|
89 |
+
|
90 |
+
|
91 |
+
|
92 |
+
|
93 |
+
|
94 |
+
|
95 |
+
|
96 |
+
|
97 |
+
|
98 |
+
|
99 |
+
|
100 |
+
|
101 |
+
|
102 |
+
|
103 |
+
|
104 |
+
|
105 |
+
|
106 |
+
|
107 |
+
|
108 |
+
|
109 |
|
110 |
with gr.Blocks() as demo:
|
111 |
+
|
112 |
+
|
113 |
+
|
114 |
+
|
115 |
+
|
116 |
+
|
117 |
+
|
118 |
+
|
119 |
gr.Markdown(
|
120 |
"""
|
121 |
<p align="center">
|
122 |
+
<img width = 600 src="https://neurona-ba.com/wp-content/uploads/2021/07/HenryLogo.jpg">
|
123 |
+
<img width = 450 src="https://raw.githubusercontent.com/BonfantiMatias/images/main/proyecto.jpg">
|
124 |
+
<img width = 400 src="https://raw.githubusercontent.com/BonfantiMatias/images/main/proyecto2.jpg">
|
125 |
+
</p>
|
126 |
+
<p align="center">
|
127 |
+
<img width=1024 src="https://raw.githubusercontent.com/BonfantiMatias/images/main/COMOFUNCIONA.png">
|
128 |
</p>
|
129 |
|
130 |
"""
|
131 |
)
|
132 |
+
gr.Markdown(
|
133 |
+
"""
|
134 |
+
<p align="center">
|
135 |
+
</p>
|
136 |
+
<p align="center">
|
137 |
+
</p>
|
138 |
+
"""
|
139 |
+
)
|
140 |
+
gr.Markdown("# Ingresar el Negocio")
|
141 |
gr.Markdown(
|
142 |
"""
|
143 |
+
- Puede ingresar el Negocio por su ID o Alias de [YELP.com](https://www.yelp.com)
|
144 |
+
- Presionar el Boton 'Iniciar Modelo' que se encuentra al final de la pagina
|
145 |
"""
|
146 |
)
|
147 |
+
with gr.Column():
|
148 |
+
seed = gr.Textbox(label="Introducir el nombre de la empresa o su Id")
|
149 |
+
with gr.Row():
|
150 |
+
gr.Examples(["la-fábrica-del-taco-buenos-aires"], inputs=[seed])
|
151 |
+
gr.Examples(["versailles-miami-4"], inputs=[seed])
|
152 |
+
gr.Examples(["la-casa-de-toño-ciudad-de-méxico-4"], inputs=[seed])
|
153 |
+
gr.Examples(["osteria-del-bugiardo-verona"], inputs=[seed])
|
154 |
+
gr.Examples(["arume-barcelona"], inputs=[seed])
|
|
|
|
|
155 |
|
156 |
+
|
157 |
+
|
158 |
+
gr.Markdown("# Mediante la Api Yelp Fusion se obtienen los datos de la ultima reseña del local seleccionado")
|
159 |
+
|
160 |
|
161 |
+
|
162 |
+
with gr.Row():
|
163 |
+
|
164 |
+
|
165 |
+
|
166 |
+
|
167 |
with gr.Column():
|
168 |
+
user = gr.Textbox(label="Id Cliente" )
|
169 |
+
review = gr.Textbox(label="Reseña Cliente")
|
170 |
+
calificacion = gr.Textbox(label="Calificacion de la reseña")
|
171 |
+
creacion = gr.Textbox(label="Fecha de creacion de la reseña")
|
172 |
with gr.Column():
|
173 |
+
image = gr.Image(label="Avatar Cliente")
|
174 |
+
|
175 |
+
|
176 |
+
|
177 |
+
gr.Markdown("# Traduccion de la reseña al idioma español")
|
178 |
+
with gr.Row():
|
179 |
+
traduccion = gr.Textbox(label="Traduccion reseña")
|
180 |
|
181 |
+
gr.Markdown("# Respuesta de la reseña implementando el modelo GPT-3 de OpenAi")
|
182 |
with gr.Row():
|
183 |
+
response2 = gr.Textbox(label="Respuesta GPT-3")
|
184 |
+
|
185 |
+
with gr.Row():
|
186 |
+
btn = gr.Button("Iniciar Modelo")
|
187 |
+
btn.click(generate_text, inputs=[seed], outputs=[review,user,image,response2,traduccion,calificacion,creacion])
|
188 |
+
|
189 |
+
|
190 |
if __name__ == "__main__":
|
191 |
demo.launch()
|