Spaces:
Sleeping
Sleeping
Commit
·
82e18e7
1
Parent(s):
5397a06
update
Browse files
app.py
CHANGED
|
@@ -13,6 +13,7 @@ import warnings
|
|
| 13 |
import socket
|
| 14 |
import tempfile
|
| 15 |
import json # New import
|
|
|
|
| 16 |
|
| 17 |
from google.cloud import bigquery
|
| 18 |
|
|
@@ -68,6 +69,18 @@ else:
|
|
| 68 |
def create_field_mode_ui():
|
| 69 |
"""Creates the Gradio UI for the offline Field Mode."""
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
def search_bigquery_for_remedy(search_query: str) -> str:
|
| 72 |
"""
|
| 73 |
Searches the BigQuery table for a remedy using the SEARCH function.
|
|
@@ -86,20 +99,10 @@ def create_field_mode_ui():
|
|
| 86 |
SEARCH(remedy_description, @query)
|
| 87 |
"""
|
| 88 |
|
| 89 |
-
# Escape special characters for BigQuery SEARCH
|
| 90 |
-
# The SEARCH function in BigQuery requires special characters like '*' to be escaped.
|
| 91 |
-
escaped_search_query = search_query
|
| 92 |
-
reserved_chars = r'\\*?:.()[]{}|&^$#@+-\"\'`'
|
| 93 |
-
for char in reserved_chars:
|
| 94 |
-
if char in escaped_search_query:
|
| 95 |
-
escaped_search_query = escaped_search_query.replace(char, f'\\\\{char}')
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
# Set up the query parameters.
|
| 100 |
job_config = bigquery.QueryJobConfig(
|
| 101 |
query_parameters=[
|
| 102 |
-
bigquery.ScalarQueryParameter("query", "STRING",
|
| 103 |
]
|
| 104 |
)
|
| 105 |
|
|
@@ -141,10 +144,11 @@ def create_field_mode_ui():
|
|
| 141 |
print(f"Diagnosis received: {diagnosis}")
|
| 142 |
|
| 143 |
if "Could not parse" in diagnosis:
|
| 144 |
-
return f"Sorry, I couldn
|
| 145 |
|
| 146 |
-
|
| 147 |
-
|
|
|
|
| 148 |
|
| 149 |
results = search_bigquery_for_remedy(search_query)
|
| 150 |
|
|
@@ -192,7 +196,7 @@ def create_field_mode_ui():
|
|
| 192 |
if temp_file_path and os.path.exists(temp_file_path):
|
| 193 |
os.remove(temp_file_path)
|
| 194 |
|
| 195 |
-
# ... (the rest of your Gradio Interface definition remains the same)
|
| 196 |
css = """
|
| 197 |
footer {visibility: hidden !important;}
|
| 198 |
.gradio-container {font-family: 'IBM Plex Sans', sans-serif;}
|
|
|
|
| 13 |
import socket
|
| 14 |
import tempfile
|
| 15 |
import json # New import
|
| 16 |
+
import re
|
| 17 |
|
| 18 |
from google.cloud import bigquery
|
| 19 |
|
|
|
|
| 69 |
def create_field_mode_ui():
|
| 70 |
"""Creates the Gradio UI for the offline Field Mode."""
|
| 71 |
|
| 72 |
+
def clean_diagnosis_text(diagnosis: str) -> str:
|
| 73 |
+
"""
|
| 74 |
+
Cleans the diagnosis text by removing special characters and extra whitespace.
|
| 75 |
+
This function is designed to prepare the text for a BigQuery SEARCH query.
|
| 76 |
+
"""
|
| 77 |
+
# Remove special characters, keeping only letters, numbers, and basic punctuation.
|
| 78 |
+
# This will remove characters like *, #, etc.
|
| 79 |
+
cleaned_text = re.sub(r'[^\w\s.\-,"]', '', diagnosis)
|
| 80 |
+
# Replace multiple whitespace characters with a single space
|
| 81 |
+
cleaned_text = re.sub(r'\s+', ' ', cleaned_text).strip()
|
| 82 |
+
return cleaned_text
|
| 83 |
+
|
| 84 |
def search_bigquery_for_remedy(search_query: str) -> str:
|
| 85 |
"""
|
| 86 |
Searches the BigQuery table for a remedy using the SEARCH function.
|
|
|
|
| 99 |
SEARCH(remedy_description, @query)
|
| 100 |
"""
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
# Set up the query parameters.
|
| 103 |
job_config = bigquery.QueryJobConfig(
|
| 104 |
query_parameters=[
|
| 105 |
+
bigquery.ScalarQueryParameter("query", "STRING", search_query),
|
| 106 |
]
|
| 107 |
)
|
| 108 |
|
|
|
|
| 144 |
print(f"Diagnosis received: {diagnosis}")
|
| 145 |
|
| 146 |
if "Could not parse" in diagnosis:
|
| 147 |
+
return f"Sorry, I couldn't identify the condition from the image. Raw output: {diagnosis}"
|
| 148 |
|
| 149 |
+
# Clean the diagnosis text before using it as a search query
|
| 150 |
+
search_query = clean_diagnosis_text(diagnosis)
|
| 151 |
+
report_title = diagnosis # Keep the original diagnosis for the report
|
| 152 |
|
| 153 |
results = search_bigquery_for_remedy(search_query)
|
| 154 |
|
|
|
|
| 196 |
if temp_file_path and os.path.exists(temp_file_path):
|
| 197 |
os.remove(temp_file_path)
|
| 198 |
|
| 199 |
+
# ... (the rest of your Gradio Interface definition remains the same)
|
| 200 |
css = """
|
| 201 |
footer {visibility: hidden !important;}
|
| 202 |
.gradio-container {font-family: 'IBM Plex Sans', sans-serif;}
|