Spaces:
Paused
Paused
File size: 1,961 Bytes
9189e38 c92bac3 9189e38 c92bac3 9189e38 c92bac3 9189e38 13b3103 9189e38 13b3103 9189e38 37a3f38 b1c94e2 37a3f38 9189e38 b1c94e2 9189e38 f71f31f 9189e38 f71f31f 9189e38 c92bac3 9189e38 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
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
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
results = algo.match_words([[input_text, None]])
print(f" - result -> {results}")
df = pd.DataFrame(results, columns=["input_word", "cleaned_word", 'matching_word', 'dictionary_word', '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", "is_food", '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()
|