feat: Update Gradio API and client for modern standards
Browse filesThis commit refactors the Gradio application to use the latest API endpoint practices by mounting the app on a FastAPI server.
- Replaced deprecated `demo.api_open` with `gr.mount_gradio_app`.
- Updated `cli_app.py` to use the `requests` library instead of `gradio_client`.
- Added `uvicorn` and `requests` to `requirements.txt`.
- Updated `design.md` to reflect these changes.
- app.py +3 -5
- cli_app.py +15 -18
- design.md +2 -3
- requirements.txt +2 -0
app.py
CHANGED
|
@@ -215,10 +215,8 @@ with gr.Blocks(theme=gr.themes.Soft(), css="#log_content textarea { font-family:
|
|
| 215 |
outputs=gr.File(label="Download Filtered Log")
|
| 216 |
)
|
| 217 |
|
| 218 |
-
|
| 219 |
-
demo.api_open(fn=load_filters, api_name="load_filters")
|
| 220 |
-
demo.api_open(fn=apply_filters, api_name="apply_filters")
|
| 221 |
|
| 222 |
if __name__ == "__main__":
|
| 223 |
-
|
| 224 |
-
|
|
|
|
| 215 |
outputs=gr.File(label="Download Filtered Log")
|
| 216 |
)
|
| 217 |
|
| 218 |
+
app = gr.mount_gradio_app(demo, path="/")
|
|
|
|
|
|
|
| 219 |
|
| 220 |
if __name__ == "__main__":
|
| 221 |
+
import uvicorn
|
| 222 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
cli_app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
-
from gradio_client import Client
|
| 2 |
import os
|
| 3 |
import json
|
| 4 |
import argparse
|
|
|
|
| 5 |
|
| 6 |
def run_log_processing(log_file_path: str, filter_file_path: str, output_file_path: str = None):
|
| 7 |
"""
|
|
@@ -13,28 +13,26 @@ def run_log_processing(log_file_path: str, filter_file_path: str, output_file_pa
|
|
| 13 |
output_file_path (str, optional): Path to the output file.
|
| 14 |
Defaults to a generated name if not provided.
|
| 15 |
"""
|
| 16 |
-
|
| 17 |
-
# You might need to change this URL if your app is hosted elsewhere.
|
| 18 |
-
client = Client("http://localhost:7860/")
|
| 19 |
|
| 20 |
print(f"Loading filters from: {filter_file_path}")
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
loaded_filters = client.predict(
|
| 24 |
-
fn_name="/load_filters",
|
| 25 |
-
inputs=[filter_file_path]
|
| 26 |
-
)
|
| 27 |
-
print(f"Loaded {len(loaded_filters)} filters.")
|
| 28 |
|
| 29 |
print(f"Applying filters to log file: {log_file_path}")
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
print("Filters applied.")
|
| 36 |
|
| 37 |
-
# Determine output file name if not provided
|
| 38 |
if output_file_path is None:
|
| 39 |
log_file_name = os.path.basename(log_file_path)
|
| 40 |
filter_file_name = os.path.basename(filter_file_path)
|
|
@@ -44,7 +42,6 @@ def run_log_processing(log_file_path: str, filter_file_path: str, output_file_pa
|
|
| 44 |
|
| 45 |
output_file_path = f"{log_base}_{filter_base}_filtered.txt"
|
| 46 |
|
| 47 |
-
# Save the result to the output file
|
| 48 |
with open(output_file_path, "w") as f:
|
| 49 |
f.write(filtered_log_content)
|
| 50 |
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
import argparse
|
| 4 |
+
import requests
|
| 5 |
|
| 6 |
def run_log_processing(log_file_path: str, filter_file_path: str, output_file_path: str = None):
|
| 7 |
"""
|
|
|
|
| 13 |
output_file_path (str, optional): Path to the output file.
|
| 14 |
Defaults to a generated name if not provided.
|
| 15 |
"""
|
| 16 |
+
base_url = "http://localhost:7860"
|
|
|
|
|
|
|
| 17 |
|
| 18 |
print(f"Loading filters from: {filter_file_path}")
|
| 19 |
+
with open(filter_file_path, 'r') as f:
|
| 20 |
+
filters = json.load(f)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
print(f"Applying filters to log file: {log_file_path}")
|
| 23 |
+
with open(log_file_path, 'r') as f:
|
| 24 |
+
log_content = f.read()
|
| 25 |
+
|
| 26 |
+
response = requests.post(f"{base_url}/api/apply_filters/", json={
|
| 27 |
+
"data": [
|
| 28 |
+
{"path": log_file_path},
|
| 29 |
+
filters
|
| 30 |
+
]
|
| 31 |
+
})
|
| 32 |
+
response.raise_for_status()
|
| 33 |
+
filtered_log_content = response.json()['data'][0]
|
| 34 |
print("Filters applied.")
|
| 35 |
|
|
|
|
| 36 |
if output_file_path is None:
|
| 37 |
log_file_name = os.path.basename(log_file_path)
|
| 38 |
filter_file_name = os.path.basename(filter_file_path)
|
|
|
|
| 42 |
|
| 43 |
output_file_path = f"{log_base}_{filter_base}_filtered.txt"
|
| 44 |
|
|
|
|
| 45 |
with open(output_file_path, "w") as f:
|
| 46 |
f.write(filtered_log_content)
|
| 47 |
|
design.md
CHANGED
|
@@ -54,8 +54,7 @@ This is the main application file that creates the user interface and handles al
|
|
| 54 |
* Clicking "Save Filtered Log" triggers `save_filtered_log`.
|
| 55 |
|
| 56 |
* **API Endpoints:**
|
| 57 |
-
*
|
| 58 |
-
* `apply_filters`: Exposed via `gr.API` to allow external clients to apply filters to log content.
|
| 59 |
|
| 60 |
### 2.2. `filter_utils.py`
|
| 61 |
|
|
@@ -78,7 +77,7 @@ This is a command-line interface (CLI) application that interacts with the runni
|
|
| 78 |
* **Functionality:**
|
| 79 |
* Takes a log file path and a filter JSON file path as arguments.
|
| 80 |
* Optionally takes an output file path; otherwise, it generates one.
|
| 81 |
-
* Uses `
|
| 82 |
* Saves the filtered log content to the specified output file.
|
| 83 |
|
| 84 |
## 3. User Interaction Flow
|
|
|
|
| 54 |
* Clicking "Save Filtered Log" triggers `save_filtered_log`.
|
| 55 |
|
| 56 |
* **API Endpoints:**
|
| 57 |
+
* The Gradio application is mounted on a FastAPI app, which exposes all public functions as API endpoints. The endpoints are accessible at `http://<host>:<port>/api/<function_name>/`.
|
|
|
|
| 58 |
|
| 59 |
### 2.2. `filter_utils.py`
|
| 60 |
|
|
|
|
| 77 |
* **Functionality:**
|
| 78 |
* Takes a log file path and a filter JSON file path as arguments.
|
| 79 |
* Optionally takes an output file path; otherwise, it generates one.
|
| 80 |
+
* Uses the `requests` library to send POST requests to the `/api/apply_filters/` endpoint of the Gradio app.
|
| 81 |
* Saves the filtered log content to the specified output file.
|
| 82 |
|
| 83 |
## 3. User Interaction Flow
|
requirements.txt
CHANGED
|
@@ -1 +1,3 @@
|
|
| 1 |
gradio
|
|
|
|
|
|
|
|
|
| 1 |
gradio
|
| 2 |
+
uvicorn
|
| 3 |
+
requests
|