Spaces:
Sleeping
Sleeping
Commit
·
2ce6777
1
Parent(s):
603bc54
index
Browse files
app.py
CHANGED
|
@@ -14,6 +14,8 @@ import socket
|
|
| 14 |
import tempfile
|
| 15 |
import json # New import
|
| 16 |
|
|
|
|
|
|
|
| 17 |
# Suppress potential warnings for a cleaner console
|
| 18 |
warnings.filterwarnings("ignore")
|
| 19 |
os.environ["TORCH_COMPILE_DISABLE"] = "1" # Ensure torch compile is off
|
|
@@ -65,6 +67,54 @@ else:
|
|
| 65 |
|
| 66 |
def create_field_mode_ui():
|
| 67 |
"""Creates the Gradio UI for the offline Field Mode."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
def get_diagnosis_and_remedy(uploaded_image: Image.Image) -> str:
|
| 69 |
if uploaded_image is None:
|
| 70 |
return "Please upload an image of a maize plant first."
|
|
@@ -86,12 +136,12 @@ def create_field_mode_ui():
|
|
| 86 |
search_query = diagnosis
|
| 87 |
report_title = diagnosis
|
| 88 |
|
| 89 |
-
results =
|
| 90 |
|
| 91 |
if not results:
|
| 92 |
remedy = "No remedy found in the local knowledge base."
|
| 93 |
else:
|
| 94 |
-
remedy = results
|
| 95 |
|
| 96 |
final_response = f"""
|
| 97 |
## Diagnosis Report
|
|
|
|
| 14 |
import tempfile
|
| 15 |
import json # New import
|
| 16 |
|
| 17 |
+
from google.cloud import bigquery
|
| 18 |
+
|
| 19 |
# Suppress potential warnings for a cleaner console
|
| 20 |
warnings.filterwarnings("ignore")
|
| 21 |
os.environ["TORCH_COMPILE_DISABLE"] = "1" # Ensure torch compile is off
|
|
|
|
| 67 |
|
| 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.
|
| 74 |
+
"""
|
| 75 |
+
try:
|
| 76 |
+
# Initialize the BigQuery client. Your project ID is used here.
|
| 77 |
+
client = bigquery.Client(project="gem-creation")
|
| 78 |
+
|
| 79 |
+
# Define the SQL query with the SEARCH function and a parameter for safety.
|
| 80 |
+
query = """
|
| 81 |
+
SELECT
|
| 82 |
+
remedy_description
|
| 83 |
+
FROM
|
| 84 |
+
`gem-creation.maize_remedies.remedies`
|
| 85 |
+
WHERE
|
| 86 |
+
SEARCH(remedy_description, @query)
|
| 87 |
+
"""
|
| 88 |
+
|
| 89 |
+
# Set up the query parameters.
|
| 90 |
+
job_config = bigquery.QueryJobConfig(
|
| 91 |
+
query_parameters=[
|
| 92 |
+
bigquery.ScalarQueryParameter("query", "STRING", search_query),
|
| 93 |
+
]
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
# Execute the query.
|
| 97 |
+
print(f"Executing BigQuery search for: '{search_query}'")
|
| 98 |
+
query_job = client.query(query, job_config=job_config)
|
| 99 |
+
|
| 100 |
+
# Process the results.
|
| 101 |
+
results = list(query_job) # Get all results
|
| 102 |
+
|
| 103 |
+
if not results:
|
| 104 |
+
print("No remedy found in BigQuery.")
|
| 105 |
+
return "No remedy found in the cloud knowledge base for this condition."
|
| 106 |
+
else:
|
| 107 |
+
# The result is a Row object; access the column by its name.
|
| 108 |
+
remedy = results[0].remedy_description
|
| 109 |
+
print("Remedy found in BigQuery.")
|
| 110 |
+
return remedy
|
| 111 |
+
|
| 112 |
+
except Exception as e:
|
| 113 |
+
error_message = f"An error occurred while querying the BigQuery database: {e}"
|
| 114 |
+
print(f"❌ {error_message}")
|
| 115 |
+
return error_message
|
| 116 |
+
|
| 117 |
+
|
| 118 |
def get_diagnosis_and_remedy(uploaded_image: Image.Image) -> str:
|
| 119 |
if uploaded_image is None:
|
| 120 |
return "Please upload an image of a maize plant first."
|
|
|
|
| 136 |
search_query = diagnosis
|
| 137 |
report_title = diagnosis
|
| 138 |
|
| 139 |
+
results = search_bigquery_for_remedy(search_query)
|
| 140 |
|
| 141 |
if not results:
|
| 142 |
remedy = "No remedy found in the local knowledge base."
|
| 143 |
else:
|
| 144 |
+
remedy = results
|
| 145 |
|
| 146 |
final_response = f"""
|
| 147 |
## Diagnosis Report
|