incognitolm commited on
Commit
c436b71
·
verified ·
1 Parent(s): 71e9ac5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -11
app.py CHANGED
@@ -1,6 +1,8 @@
1
  import gradio as gr
2
  import requests
3
  import os
 
 
4
 
5
  api_keys = []
6
  last_api_key_used = 0
@@ -33,9 +35,7 @@ def get_api_key():
33
  global last_api_key_used
34
 
35
  if num_api_keys == 0:
36
- raise Exception(
37
- "No API keys found. Format: OLLAMA_<number>"
38
- )
39
 
40
  last_api_key_used += 1
41
  if last_api_key_used >= num_api_keys:
@@ -63,20 +63,63 @@ def perform_search(query):
63
  response = requests.post(url, headers=headers, json=payload)
64
  response.raise_for_status()
65
 
66
- results = response.json()
67
- return results
68
 
69
- # return "\n".join(
70
- # f"{r['title']}: {r['url']}"
71
- # for r in results
72
- # )
73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
  demo = gr.Interface(
76
  fn=perform_search,
77
  inputs=gr.Textbox(label="Search Query"),
78
- outputs=gr.Textbox(label="Results"),
79
  title="Ollama Web Search API"
80
  )
81
 
82
- demo.launch()
 
 
1
  import gradio as gr
2
  import requests
3
  import os
4
+ from fastapi import FastAPI, HTTPException
5
+ from pydantic import BaseModel
6
 
7
  api_keys = []
8
  last_api_key_used = 0
 
35
  global last_api_key_used
36
 
37
  if num_api_keys == 0:
38
+ raise Exception("No API keys found. Format: OLLAMA_<number>")
 
 
39
 
40
  last_api_key_used += 1
41
  if last_api_key_used >= num_api_keys:
 
63
  response = requests.post(url, headers=headers, json=payload)
64
  response.raise_for_status()
65
 
66
+ return response.json()
 
67
 
 
 
 
 
68
 
69
+ # -----------------------
70
+ # FastAPI
71
+ # -----------------------
72
+
73
+ app = FastAPI(title="Ollama Web Search API")
74
+
75
+
76
+ class SearchRequest(BaseModel):
77
+ query: str
78
+ max_results: int = 1
79
+
80
+
81
+ @app.post("/api/search")
82
+ def api_search(request: SearchRequest):
83
+
84
+ try:
85
+ api_key = get_api_key()
86
+
87
+ url = "https://ollama.com/api/web_search"
88
+
89
+ headers = {
90
+ "Authorization": f"Bearer {api_key}",
91
+ "Content-Type": "application/json"
92
+ }
93
+
94
+ payload = {
95
+ "query": request.query,
96
+ "max_results": request.max_results
97
+ }
98
+
99
+ response = requests.post(url, headers=headers, json=payload)
100
+ response.raise_for_status()
101
+
102
+ return response.json()
103
+
104
+ except Exception as e:
105
+ raise HTTPException(status_code=500, detail=str(e))
106
+
107
+
108
+ @app.get("/health")
109
+ def health():
110
+ return {"status": "ok"}
111
+
112
+
113
+ # -----------------------
114
+ # Gradio UI
115
+ # -----------------------
116
 
117
  demo = gr.Interface(
118
  fn=perform_search,
119
  inputs=gr.Textbox(label="Search Query"),
120
+ outputs=gr.JSON(label="Results"),
121
  title="Ollama Web Search API"
122
  )
123
 
124
+ # Mount Gradio at root
125
+ app = gr.mount_gradio_app(app, demo, path="/")