Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,42 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
import json
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
response =
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import subprocess
|
| 3 |
import json
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
|
| 7 |
+
# Cargar la clave API desde el archivo .env
|
| 8 |
+
load_dotenv()
|
| 9 |
+
API_KEY = os.getenv('API_KEY')
|
| 10 |
|
| 11 |
+
def make_request(query):
|
| 12 |
+
command = [
|
| 13 |
+
'curl',
|
| 14 |
+
f'https://us-east-1.serp.ing/api/v1/google/serp?q={query}&hl=en&gl=us&num=10&snapshot=off&thumbnail=on',
|
| 15 |
+
'--header', f'X-API-KEY: {API_KEY}',
|
| 16 |
+
'--header', 'Content-Type: application/json'
|
| 17 |
+
]
|
| 18 |
|
| 19 |
+
result = subprocess.run(command, capture_output=True, text=True)
|
| 20 |
+
response = result.stdout
|
| 21 |
|
| 22 |
+
try:
|
| 23 |
+
response_json = json.loads(response)
|
| 24 |
+
return response_json
|
| 25 |
+
except json.JSONDecodeError:
|
| 26 |
+
return {"error": "Failed to parse the response as JSON"}
|
| 27 |
+
|
| 28 |
+
# Interfaz de Gradio
|
| 29 |
+
def query_interface(query):
|
| 30 |
+
result = make_request(query)
|
| 31 |
+
return result
|
| 32 |
+
|
| 33 |
+
# Crear la interfaz de Gradio
|
| 34 |
+
iface = gr.Interface(fn=query_interface,
|
| 35 |
+
inputs=gr.Textbox(label="Search Query"),
|
| 36 |
+
outputs="json",
|
| 37 |
+
title="API Query Interface",
|
| 38 |
+
description="Enter a search query to fetch data from the API.")
|
| 39 |
+
|
| 40 |
+
# Ejecutar la interfaz
|
| 41 |
+
if __name__ == "__main__":
|
| 42 |
+
iface.launch()
|