File size: 5,679 Bytes
2063825
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from flask import Flask, render_template, request, jsonify
from transformers import pipeline
from meta_ai_api import MetaAI
import os
import re  # For text processing

# Initialize Meta AI client
client = MetaAI()

# Category list (unchanged)
categories = [
    "Banking", "Books", "Clothes", "College Admissions", "Cooking",
    "Elementary Education", "Middle School Education", "High School Education", "University Education",
    "Employment", "Finance", "Food", "Gardening", "Homelessness", "Housing", "Jobs", "Investing",
    "Matrimonial", "Brain Medical", "Depression Medical", "Eye Medical", "Hand Medical",
    "Head Medical", "Leg Medical", "Rental", "School", "Shopping",
    "Baseball Sports", "Basketball Sports", "Cricket Sports", "Handball Sports",
    "Jogging Sports", "Hockey Sports", "Running Sports", "Tennis Sports",
    "Stocks", "Travel", "Tourism"
]

# Zero-shot classification pipeline (unchanged)
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")

app = Flask(__name__)

@app.route('/')
def home():
    return render_template('index.html', categories=categories)

@app.route('/predict_categories', methods=['POST'])
def predict_categories():
    data = request.get_json()
    subject = data.get("subject")
    description = data.get("description")
    if not subject or not description:
        return jsonify({"error": "Subject and description required"}), 400
    prompt = f"{subject}. {description}"
    try:
        result = classifier(prompt, categories)
        return jsonify({"predicted_categories": result['labels'][:3]})
    except Exception as e:
        return jsonify({"error": str(e)}), 500

@app.route('/generate_answer', methods=['POST'])
def generate_answer():
    data = request.get_json()
    category = data.get("category")
    question = data.get("question")
    if not category or not question:
        return jsonify({"error": "Category and question required"}), 400
    
    # Create a more specific prompt with formatting instructions
    prompt = (
        f"Category: {category}\n"
        f"Question: {question}\n\n"
        "Please provide a detailed and well-structured answer. Format your response as follows:\n"
        "- For any list of items (e.g., websites, tips, steps, examples, or recommendations), use bullet points starting with '-' (e.g., - Item).\n"
        "- Use bold text (e.g., **text**) for headings or key terms, such as section titles or important concepts.\n"
        "- Use line breaks to separate paragraphs or sections for clarity.\n"
        "- Ensure the response is clear, concise, and easy to read.\n"
        "- If the question asks for recommendations, resources, or a list, always present them as bullet points.\n"
        "For example, if asked for websites, format the response like this:\n"
        "**Websites**\n"
        "- Website 1: Description.\n"
        "- Website 2: Description.\n"
    )
    
    try:
        # Get the response from Meta AI
        response = client.prompt(prompt)
        raw_answer = response['message'].strip()

        # Format the response to ensure consistency
        formatted_answer = format_response(raw_answer)

        return jsonify({"answer": formatted_answer})
    except Exception as e:
        return jsonify({"error": str(e)}), 500

def format_response(text):
    """

    Format the raw AI response to ensure bullet points and structure, even if the AI doesn't follow instructions.

    """
    # Split the text into lines
    lines = text.split('\n')
    formatted_lines = []
    in_list = False

    # Patterns to detect list-like content
    list_indicators = [
        r'^(General|Niche|International|Country-Specific|Additional)\b',  # Section headings
        r'^(Indeed|LinkedIn|Glassdoor|H1BGrader|H1B Visa Jobs|ImmIhelp)\b',  # Website names
        r'^(Network|Check|Stay)\b'  # Tips starting with verbs
    ]

    for line in lines:
        line = line.strip()
        if not line:
            if in_list:
                in_list = False
                formatted_lines.append('')  # Add a line break after a list
            continue

        # Check if the line starts a new section (e.g., "General Job Search Websites")
        if any(re.match(pattern, line, re.IGNORECASE) for pattern in list_indicators):
            if in_list:
                in_list = False
                formatted_lines.append('')  # Add a line break before a new section
            # Add the section as a bold heading
            formatted_lines.append(f"**{line}**")
            in_list = True
            continue

        # If we're in a list, format the line as a bullet point
        if in_list and not line.startswith('-'):
            # Split the line into website/tip and description (e.g., "Indeed (link unavailable): Description")
            if ': ' in line:
                parts = line.split(': ', 1)
                item = parts[0].strip()
                description = parts[1].strip() if len(parts) > 1 else ""
                formatted_lines.append(f"- **{item}**: {description}")
            else:
                formatted_lines.append(f"- {line}")
        else:
            # If not in a list, add the line as a paragraph
            if in_list:
                in_list = False
                formatted_lines.append('')  # Add a line break after a list
            formatted_lines.append(line)

    # Join the lines with proper spacing
    formatted_text = '\n'.join(formatted_lines)
    return formatted_text

if __name__ == '__main__':
    app.run(debug=True)