Spaces:
Paused
Paused
import gradio as gr | |
import pandas as pd | |
import psycopg2 | |
from algo import Algo | |
from psycopg2.extras import DictCursor | |
from db.db_utils import get_connection | |
from multi_food_item_detector import has_delimiters, extract_items | |
db_conn = get_connection() | |
db_cursor = db_conn.cursor(cursor_factory=psycopg2.extras.DictCursor) | |
algo = Algo(db_conn) | |
def process_input(input_text, csv_file): | |
# if csv_file is not None: | |
# # Read the uploaded CSV file | |
# df = pd.read_csv(csv_file.name) | |
# # Check if 'description' column exists | |
# if 'description' in df.columns: | |
# descriptions = df['description'].tolist() | |
# results = algo.match_words(descriptions) | |
# else: | |
# return pd.DataFrame({"Error": ["CSV file must have a 'description' column"]}) | |
# else: | |
# Process the single input text | |
input_items = [[input_text, None]] | |
if has_delimiters(input_text): | |
items = extract_items(input_text) | |
items = [[item, None] for item in items] | |
input_items.extend(items) | |
results = algo.match_words(input_items) | |
print(f" - result -> {results}") | |
df = pd.DataFrame(results, columns=["input_word", "cleaned_word", 'dictionary_word', "specificity", 'sr_legacy_food_category', 'wweia_category', 'dry_matter_content', | |
'water_content', 'similarity_score', 'confidence_score', 'similar_words', 'is_food', 'food_nonfood_score']) | |
# Filter to only required columns | |
df_filtered = df[["input_word", "dictionary_word", "specificity", "is_food", "sr_legacy_food_category", 'wweia_category', 'dry_matter_content', "water_content", "similarity_score", "food_nonfood_score"]] | |
return df_filtered | |
# Gradio interface | |
with gr.Blocks() as demo: | |
with gr.Column(): | |
with gr.Row(): | |
input_text = gr.Textbox(label="Enter a food item", placeholder="e.g. apple") | |
# csv_input = gr.File(label="Upload a CSV file (optional)") | |
submit_button = gr.Button("Submit") | |
output_table = gr.DataFrame(label="Processed Results") | |
input_text.submit(fn=process_input, inputs=[input_text,], outputs=output_table) | |
submit_button.click(fn=process_input, inputs=[input_text,], outputs=output_table) | |
demo.launch() | |
db_cursor.close() | |
db_conn.close() | |