File size: 9,907 Bytes
b03d3b6
69d06c3
bb03459
cf6ba24
 
 
75b2af7
b03d3b6
 
 
 
 
 
 
 
 
8118ddb
12445b5
8118ddb
db78288
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b03d3b6
f08b9d8
9435ff3
8118ddb
 
db78288
12445b5
6f25720
8118ddb
882d620
df9068d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
882d620
 
 
 
 
 
 
 
 
 
 
 
 
 
b87140c
882d620
 
 
 
 
 
fdde55e
882d620
 
 
 
feaad6f
882d620
 
 
 
 
feaad6f
882d620
 
39db0a8
d4e41da
882d620
813580a
 
882d620
 
df9068d
 
 
 
 
 
 
 
882d620
c6fae51
 
 
 
7a97701
882d620
 
c6fae51
882d620
 
 
 
 
 
 
 
 
 
 
 
 
 
b2a9019
882d620
b2a9019
882d620
a912654
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7aaac7c
a912654
 
 
 
 
2215e5c
 
 
7aaac7c
a912654
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7aaac7c
 
 
 
 
 
 
 
 
 
aad8f67
a912654
aad8f67
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
from fastapi import FastAPI
from pydantic import BaseModel
from typing import Dict, List
import gradio as gr
import pandas as pd
import json
from src.core import *

app = FastAPI(
    title="Insight Finder",
    description="Find relevant technologies from a problem",
)

class InputData(BaseModel):
    problem: str

class InputConstraints(BaseModel):
    constraints: Dict[str, str]
    
# This schema defines the structure for a single technology object
class Technology(BaseModel):
    """Represents a single technology entry with its details."""
    title: str
    purpose: str
    key_components: str
    advantages: str
    limitations: str
    id: int

# This schema defines the root structure of the JSON
class TechnologyData(BaseModel):
    """Represents the top-level object containing a list of technologies."""
    technologies: List[Technology]

@app.post("/process", response_model=TechnologyData)
async def process(data: InputData):
    result = process_input(data, global_tech, global_tech_embeddings)
    return {"technologies": result}


@app.post("/process-constraints", response_model=TechnologyData)
async def process_constraints(constraints: InputConstraints):
    result = process_input_from_constraints(constraints.constraints, global_tech, global_tech_embeddings)
    return {"technologies": result}


def make_json_serializable(data):
    """
    Recursively convert tensors to floats in a data structure
    so it can be passed to json.dumps.
    """
    if isinstance(data, dict):
        return {k: make_json_serializable(v) for k, v in data.items()}
    elif isinstance(data, list):
        return [make_json_serializable(item) for item in data]
    elif isinstance(data, tuple):
        return tuple(make_json_serializable(item) for item in data)
    elif hasattr(data, 'item'):  # torch.Tensor with single value
        return float(data.item())
    else:
        return data
        
def process_input_gradio(problem_description: str):
    """
    Processes the input problem description step-by-step for Gradio.
    Returns all intermediate results.
    """
    # Step 1: Set Prompt
    prompt = set_prompt(problem_description)

    # Step 2: Retrieve Constraints
    constraints = retrieve_constraints(prompt)

    # Step 3: Stem Constraints
    constraints_stemmed = stem(constraints, "constraints")
    save_dataframe(pd.DataFrame({"stemmed_constraints": constraints_stemmed}), "constraints_stemmed.xlsx")
    print(constraints_stemmed)

    # Step 4: Global Tech (already loaded, just acknowledge)
    # save_dataframe(global_tech_df, "global_tech.xlsx") # This is already done implicitly by loading

    # Step 5: Get Contrastive Similarities
    result_similarities, matrix = get_contrastive_similarities(
        constraints_stemmed, global_tech, global_tech_embeddings
    )
    save_to_pickle(result_similarities)

    # Step 6: Find Best List Combinations
    best_combinations = find_best_list_combinations(constraints_stemmed, global_tech, matrix)

    # Step 7: Select Technologies
    best_technologies_id = select_technologies(best_combinations)

    # Step 8: Get Technologies by ID
    best_technologies = get_technologies_by_id(best_technologies_id, global_tech)

    # Format outputs for Gradio
    matrix_display = matrix #.tolist() # Convert numpy array to list of lists for better Gradio display

    result_similarities_display = {
        item['id2']: f"{item['constraint']['title']} ({item['similarity'].item():.3f})"
        for item in result_similarities
    }
    
    
    # Convert to JSON-safe format
    safe_best_combinations = make_json_serializable(best_combinations)
    safe_best_technologies = make_json_serializable(best_technologies)
    
    # Now this will work safely:
    best_combinations_display = json.dumps(safe_best_combinations, indent=2)
    best_technologies_display = json.dumps(safe_best_technologies, indent=2)

    print("best combinations")
    print(best_combinations_display)
    print("\nbest technologies")
    print(best_technologies_display)
    
    return (
        prompt,
        "\n".join(f"'{k}': {v}" for k, v in d.items()),
        best_combinations_display,
        ", ".join(map(str, best_technologies_id)),
        best_technologies_display
    )

# --- Gradio Interface Setup ---
# Define the input and output components
input_problem = gr.Textbox(
    label="Enter Problem Description",
    placeholder="e.g., Develop a secure and scalable e-commerce platform with real-time analytics."
)

output_prompt = gr.Textbox(label="1. Generated Prompt", interactive=False)
output_constraints = gr.Textbox(label="2. Retrieved Constraints", interactive=False)
output_best_combinations = gr.JSON(label="7. Best Technology Combinations Found")
output_selected_ids = gr.Textbox(label="8. Selected Technology IDs", interactive=False)
output_final_technologies = gr.JSON(label="9. Final Best Technologies")

# Custom CSS for a professional look
custom_css = """
/* General Body and Font Styling */
body {
    font-family: 'Segoe UI', 'Roboto', 'Helvetica Neue', Arial, sans-serif;
    color: #333;
    background-color: #f0f2f5;
}

/* Header Styling */
.gradio-container h1 {
    color: #0056b3; /* A deep blue for the main title */
    text-align: center;
    margin-bottom: 10px;
    font-weight: 600;
    font-size: 2.5em;
    text-shadow: 1px 1px 2px rgba(0,0,0,0.1);
}

.gradio-container h2 {
    color: #007bff; /* A slightly lighter blue for subtitles */
    text-align: center;
    margin-top: 0;
    margin-bottom: 30px;
    font-weight: 400;
    font-size: 1.2em;
}

/* Card-like styling for individual components */
.gradio-container .gr-box {
    background-color: #ffffff;
    border-radius: 12px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
    padding: 20px;
    margin-bottom: 20px;
    border: 1px solid #e0e0e0;
}

/* Input Textbox Styling */
.gradio-container input[type="text"],
.gradio-container textarea {
    border: 1px solid #ced4da;
    border-radius: 8px;
    padding: 12px 15px;
    font-size: 1em;
    color: #495057;
    transition: border-color 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
}

.gradio-container input[type="text"]:focus,
.gradio-container textarea:focus {
    border-color: #007bff;
    box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25);
    outline: none;
}

/* Button Styling */
.gradio-container button {
    background-color: #28a745; /* A vibrant green for action */
    color: white;
    border: none;
    border-radius: 8px;
    padding: 12px 25px;
    font-size: 1.1em;
    font-weight: 500;
    cursor: pointer;
    transition: background-color 0.2s ease-in-out, transform 0.1s ease-in-out;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.gradio-container button:hover {
    background-color: #218838; /* Darker green on hover */
    transform: translateY(-2px);
}

.gradio-container button:active {
    transform: translateY(0);
}

/* Labels for outputs */
.gradio-container label {
    font-weight: 600;
    color: #495057;
    margin-bottom: 8px;
    display: block; /* Ensure labels are on their own line */
    font-size: 1.1em;
}

/* JSON Output Specific Styling */
.gradio-container .json-display {
    background-color: #f8f9fa;
    border: 1px solid #e9ecef;
    border-radius: 8px;
    padding: 15px;
    font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, Courier, monospace;
    color: #212529;
    white-space: pre-wrap; /* Preserve whitespace and wrap long lines */
    overflow-x: auto; /* Allow horizontal scrolling if content is too wide */
    max-height: 400px; /* Limit height and add scroll */
}

/* Responsive adjustments (Gradio handles a lot, but for specific tweaks) */
@media (max-width: 768px) {
    .gradio-container {
        padding: 15px;
    }
    .gradio-container h1 {
        font-size: 2em;
    }
    .gradio-container button {
        width: 100%;
        padding: 15px;
    }
}

/* Optional: Logo and Branding Placeholder */
/* You would typically add an image element in your gr.Blocks() for a logo */
/* Example if you have a logo image: */
/* .logo {
    display: block;
    margin: 0 auto 20px auto;
    max-width: 200px;
    height: auto;
} */
"""

# Create the Gradio Blocks demo with custom theme and CSS
with gr.Blocks(
    theme=gr.themes.Soft(), # A modern, soft theme from Gradio
    css=custom_css
) as gradio_app_blocks:
    # Optional: Add your logo here
    # gr.Image("path/to/your/logo.png", width=150, show_label=False, container=False, elem_classes="logo")

    gr.Markdown("# Insight Finder: Step-by-Step Technology Selection")
    gr.Markdown("## Enter a problem description to see how relevant technologies are identified through various processing steps.")

    with gr.Row(): # Use a row for better layout on wider screens
        with gr.Column(scale=2): # Input takes more space
            input_problem.render()
        with gr.Column(scale=1):  # Button in a smaller column
            gr.Markdown("Click to start the analysis:")
            process_button = gr.Button("Process Problem", elem_id="process_button")


    gr.Markdown("---") # Separator for visual clarity

    gr.Markdown("### Processing Steps & Results:")

    # Group outputs into columns for better organization
    with gr.Row():
        with gr.Column():
            output_prompt.render()
            output_constraints.render()
        with gr.Column():
            output_best_combinations.render()
            output_selected_ids.render()
            output_final_technologies.render()

    # Link the button to the processing function
    process_button.click(
        fn=process_input_gradio,
        inputs=input_problem,
        outputs=[
            output_prompt,
            output_constraints,
            output_best_combinations,
            output_selected_ids,
            output_final_technologies
        ]
    )
    
gr.mount_gradio_app(app, gradio_app_blocks, path="/gradio")