tx3bas commited on
Commit
910b9c8
·
verified ·
1 Parent(s): 4289b02

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -23
app.py CHANGED
@@ -1,29 +1,23 @@
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):
@@ -31,11 +25,13 @@ def query_interface(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__":
 
1
  import os
2
+ import requests
 
3
  import gradio as gr
 
4
 
5
+ # Recuperar la clave API desde las variables de entorno
 
6
  API_KEY = os.getenv('API_KEY')
7
 
8
  def make_request(query):
9
+ url = f'https://us-east-1.serp.ing/api/v1/google/serp?q={query}&hl=en&gl=us&num=10&snapshot=off&thumbnail=on'
10
+ headers = {
11
+ 'X-API-KEY': API_KEY,
12
+ 'Content-Type': 'application/json'
13
+ }
 
14
 
15
+ response = requests.get(url, headers=headers)
 
16
 
17
+ if response.status_code == 200:
18
+ return response.json()
19
+ else:
20
+ return {"error": f"Failed to fetch data. Status code: {response.status_code}"}
 
21
 
22
  # Interfaz de Gradio
23
  def query_interface(query):
 
25
  return result
26
 
27
  # Crear la interfaz de Gradio
28
+ iface = gr.Interface(
29
+ fn=query_interface,
30
+ inputs=gr.Textbox(label="Search Query"),
31
+ outputs="json",
32
+ title="API Query Interface",
33
+ description="Enter a search query to fetch data from the API."
34
+ )
35
 
36
  # Ejecutar la interfaz
37
  if __name__ == "__main__":