Spaces:
Build error
Build error
init service
Browse files
app.py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import logging
|
| 4 |
+
import typing as t
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
+
from huggingface_hub import InferenceClient
|
| 8 |
+
|
| 9 |
+
logging.basicConfig(
|
| 10 |
+
level=logging.INFO,
|
| 11 |
+
format="%(asctime)s - %(levelname)s - %(message)s",
|
| 12 |
+
)
|
| 13 |
+
logger = logging.getLogger(__name__)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
HF_API_TOKEN = os.getenv("HF_API_TOKEN")
|
| 17 |
+
MODEL_REPO_ID= "google/gemma-2-9b-it"
|
| 18 |
+
|
| 19 |
+
if HF_API_TOKEN is None:
|
| 20 |
+
logger.error("HF_API_TOKEN environment variable is not set.")
|
| 21 |
+
raise ValueError("Error: HF_API_TOKEN environment variable is not set.")
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def build_messages(input_json: str) -> t.Sequence[t.Mapping[str, str]]:
|
| 25 |
+
data_structure = (
|
| 26 |
+
"You are given a restaurant menu in Spanish. You MUST return a single valid JSON in this format:\n"
|
| 27 |
+
"Menu Format:\n"
|
| 28 |
+
"<menu> ::= '{' \"restaurant_name\": <string>, \"categories\": [ <category>* ] '}'\n"
|
| 29 |
+
"<category> ::= '{' \"category\": <string>, \"items\": [ <item>* ] '}'\n"
|
| 30 |
+
"<item> ::= '{' \"name\": <string>, \"price\": <number>, \"description\": <string> }'\n"
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
instructions = (
|
| 34 |
+
"Requirements:\n"
|
| 35 |
+
"1. **Translate ALL Spanish text into English**, including:\n"
|
| 36 |
+
" - restaurant_name (only if it's in Spanish).\n"
|
| 37 |
+
" - All category names.\n"
|
| 38 |
+
" - All item names.\n"
|
| 39 |
+
" - Any Spanish words in the final descriptions.\n\n"
|
| 40 |
+
"2. If an item name is a distinct dish with no direct English equivalent, "
|
| 41 |
+
" **still attempt** an English literal translation, or provide a parenthetical explanation if needed.\n\n"
|
| 42 |
+
"3. For every item, add a new field called description in **concise, appetizing English**.\n\n"
|
| 43 |
+
"4. **Do not** change or remove existing fields. The only added field is description.\n\n"
|
| 44 |
+
"5. **Do not** change meaning or make up information."
|
| 45 |
+
"6. Return **only** the JSON object. **No markdown**, no code fences, no extra text.\n"
|
| 46 |
+
"7. Ensure the output is **valid JSON** with correct brackets, commas, and quotes.\n\n"
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
system_message = data_structure + instructions
|
| 50 |
+
|
| 51 |
+
user_message = (
|
| 52 |
+
"Process the following menu:\n\n"
|
| 53 |
+
f"{input_json}"
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
return [
|
| 57 |
+
{"role": "user", "content": system_message + user_message},
|
| 58 |
+
]
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def process_menu(input_text: str) -> str:
|
| 62 |
+
client = InferenceClient(model=MODEL_REPO_ID, token=HF_API_TOKEN)
|
| 63 |
+
messages = build_messages(input_text)
|
| 64 |
+
|
| 65 |
+
logger.info("Generating response from the model.")
|
| 66 |
+
response = client.chat_completion(
|
| 67 |
+
messages,
|
| 68 |
+
max_tokens=2048,
|
| 69 |
+
temperature=0.1,
|
| 70 |
+
seed=42,
|
| 71 |
+
)
|
| 72 |
+
if response is not None and response.choices is not None:
|
| 73 |
+
content = response.choices[0].message.content
|
| 74 |
+
|
| 75 |
+
logger.info(response)
|
| 76 |
+
logger.info(content)
|
| 77 |
+
|
| 78 |
+
parsed = json.loads(content)
|
| 79 |
+
logger.info("Parsed JSON successfully.")
|
| 80 |
+
return json.dumps(parsed, indent=2, ensure_ascii=False)
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def process_data(data: t.Any) -> str:
|
| 84 |
+
logger.info("Reading input file: %s", data)
|
| 85 |
+
input_name = os.path.basename(data)
|
| 86 |
+
preprocessed_name = "preprocessed_" + input_name
|
| 87 |
+
|
| 88 |
+
with open(data, "r", encoding="utf-8") as raw_data:
|
| 89 |
+
menu = raw_data.read()
|
| 90 |
+
|
| 91 |
+
logger.info("Processing the menu data through the model.")
|
| 92 |
+
preprocessed_data = process_menu(menu)
|
| 93 |
+
|
| 94 |
+
logger.info("Writing preprocessed data to file: %s", preprocessed_name)
|
| 95 |
+
with open(preprocessed_name, "w", encoding="utf-8") as temp_data:
|
| 96 |
+
temp_data.write(preprocessed_data)
|
| 97 |
+
|
| 98 |
+
logger.info("Processing complete. Preprocessed file created: %s", preprocessed_name)
|
| 99 |
+
return preprocessed_name
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
with gr.Blocks() as demo:
|
| 103 |
+
gr.Markdown("# Restaurant Menu Processor")
|
| 104 |
+
gr.Markdown(
|
| 105 |
+
"Upload a JSON file containing a restaurant menu in Spanish. "
|
| 106 |
+
"This tool will translate the menu into English and add descriptions."
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
with gr.Row():
|
| 110 |
+
input_file = gr.File(label="Upload Restaurant Menu JSON (Spanish)")
|
| 111 |
+
output_file = gr.File(label="Download Augmented Menu JSON (English)")
|
| 112 |
+
|
| 113 |
+
process_button = gr.Button("Process Menu")
|
| 114 |
+
process_button.click(process_data, inputs=input_file, outputs=output_file)
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
demo.launch()
|