CoolFace
Apppublic

rcwell/filter-parser

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
app.py77 linesDownload Raw Back to root
1from flask import Flask, request, jsonify2from transformers import pipeline3 4app = Flask(__name__)5 6# You can use a conversational model or instruct model depending on availability7nlp2json = pipeline(8    "text2text-generation",9    model="google/flan-t5-base"  # You can swap this for a more suitable instruct model if available10)11 12REAL_ESTATE_PROMPT = """13You are a real estate query parser. Your task is to analyze natural language input and convert it into a structured JSON object using only the specified values in the schema below. Follow these rules strictly:14 15- Output **only** a valid JSON object matching the schema below.16- Do **not** provide explanations, comments, or any text outside of the JSON.17- All keys must be in **double quotes**, and string values must be in **double quotes** as well.18- Only use values from the allowed enums. If a value for a field is not found, use:19  - `"Any"` for string fields.20  - An empty array `[]` for list fields.21- Always include **all required fields**, even if their value is "Any" or empty.22 23Schema:24{{25    "type": "object",26    "properties": {{27        "types": {{"type": "array", "items": {{"type": "string", "format": "enum", "enum": ["Long term", "Short term", "Shared", "Transient", "For Sale", "Commercial"]}}}},28        "amenities": {{"type": "array", "items": {{"type": "string", "format": "enum", "enum": ["Wi-Fi", "CCTV", "Parking", "Rooftop", "Gym", "Pool", "Semi furnished", "Fully furnished", "Gated", "Pets Allowed"]}}}},29        "unit": {{"type": "string", "format": "enum", "enum": ["daily", "monthly", "one-time"]}},30        "bedroom": {{"type": "string", "format": "enum", "enum": ["Studio", "Any", "6+", "1", "2", "3", "4", "5"]}},31        "bathroom": {{"type": "string", "format": "enum", "enum": ["Any", "Private", "6+", "1", "2", "3", "4", "5"]}},32        "person": {{"type": "string", "format": "enum", "enum": ["Family", "Any", "6+", "1", "2", "3", "4", "5"]}},33        "price": {{"type": "array", "items": {{"type": "number"}}, "minItems": 2, "maxItems": 2}}34    }},35    "required": ["types", "amenities", "unit", "bedroom", "bathroom", "person", "price"]36}}37 38Example:39Input:40"Looking for a short term apartment with gym and pool, 2 bedrooms, private bathroom, for 3 people, price between 1500 and 2500 monthly."41 42Output:43{{44  "types": ["Short term"],45  "amenities": ["Gym", "Pool"],46  "unit": "monthly",47  "bedroom": "2",48  "bathroom": "Private",49  "person": "3",50  "price": [1500, 2500]51}}52 53Now parse this input:54"{query}"55 56Return only the JSON object as per the schema.57"""58 59@app.route('/filter', methods=['POST'])60def nlp_to_filter():61    data = request.get_json()62    if not data or 'query' not in data:63        return jsonify({'error': 'Missing query in payload'}), 40064    query = data['query']65    prompt = REAL_ESTATE_PROMPT.format(query=query)66    try:67        result = nlp2json(prompt, max_length=256)[0]['generated_text']68        return app.response_class(69            response=result.strip(),70            status=200,71            mimetype="application/json"72        )73    except Exception as e:74        return jsonify({'error': str(e)}), 50075 76if __name__ == '__main__':77    app.run(debug=True)