Spaces:
Runtime error
Runtime error
File size: 14,880 Bytes
b03d3b6 69d06c3 bb03459 cf6ba24 75b2af7 b03d3b6 e602ae8 b03d3b6 8118ddb 12445b5 8118ddb db78288 e602ae8 02f1abf e602ae8 ebd619d e602ae8 719344c e602ae8 31b55cd e602ae8 db78288 e602ae8 1eebfeb 8118ddb db78288 12445b5 1eebfeb 02f1abf e602ae8 31b55cd a7372e0 02f1abf e602ae8 31b55cd 02f1abf 882d620 df9068d 8d05399 df9068d 8d05399 f1ee15c 8d05399 31b55cd 8d05399 f1ee15c 8d05399 f1ee15c 8d05399 882d620 b87140c 882d620 fdde55e 882d620 feaad6f 882d620 feaad6f 882d620 d5a06db 882d620 8d05399 d5a06db 8d05399 31b55cd d4e41da 882d620 8d05399 882d620 8d05399 882d620 8d05399 882d620 8d05399 882d620 8d05399 a912654 8d05399 31b55cd 8d05399 a912654 8d05399 a912654 8d05399 a912654 8d05399 a912654 8d05399 a912654 8d05399 a912654 8d05399 a912654 7aaac7c a912654 8d05399 a912654 8d05399 3d0bcee a912654 8d05399 a912654 8d05399 a912654 8d05399 a912654 7aaac7c fd04976 31b55cd |
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 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 |
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 InputProblem(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
class OutputPriorArt(BaseModel):
"""Represents the search of prior art using the technology combinations"""
content: str
uris: List
class InputPriorArtConstraints(BaseModel):
technologies: List[Technology]
constraints: Dict[str, str]
class InputPriorArtProblem(BaseModel):
technologies: List[Technology]
problem: str
# 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: InputProblem):
result= process_input(data, global_tech, global_tech_embeddings, "problem")
return {"technologies": result}
@app.post("/process-constraints", response_model=TechnologyData)
async def process_constraints(constraints: InputConstraints):
result= process_input(constraints.constraints, global_tech, global_tech_embeddings, "constraints")
return {"technologies": result}
@app.post("/prior-art-constraints", response_model=OutputPriorArt)
async def prior_art_constraints(data: InputPriorArtConstraints):
prior_art = process_prior_art(data.technologies, data.constraints, "constraints")
print(prior_art)
return prior_art
@app.post("/prior-art-problems", response_model=OutputPriorArt)
async def prior_art_problems(data: InputPriorArtProblem):
prior_art = process_prior_art(data.technologies, data.problems, "problem")
return prior_art
def make_json_serializable(data):
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'):
return float(data.item())
else:
return data
def format_constraints_html(constraints: dict) -> str:
html_content = "<div class='constraints-container'>"
for title, description in constraints.items():
html_content += f"""
<div class='constraint-item'>
<p><span class='constraint-title'>{title}:</span> <span class='constraint-description'>{description}</span></p>
</div>
"""
html_content += "</div>"
return "<h1>Retrieved Constraints</h1>" + html_content
def format_best_combinations_html(combinations_data: list) -> str:
html_content = "<div class='combinations-outer-container'>"
for i, combination in enumerate(combinations_data):
problem_title = combination.get("problem", {}).get("title", f"Problem {i+1}")
technologies = combination.get("technologies", [])
html_content += f"""
<div class='problem-card'>
<h3 class='problem-card-title'>{problem_title}</h3>
<div class='technologies-inner-container'>
"""
for tech_info_score in technologies:
tech_info = tech_info_score[0]
if isinstance(tech_info, dict):
html_content += f"""
<div class='technology-card'>
<h4 class='tech-card-title'>{tech_info.get('title', 'N/A')}</h4>
<p><strong>Purpose:</strong> {tech_info.get('purpose', 'N/A')}</p>
<p><strong>Components:</strong> {tech_info.get('key_components', 'N/A')}</p>
<p><strong>Advantages:</strong> {tech_info.get('advantages', 'N/A')}</p>
<p><strong>Limitations:</strong> {tech_info.get('limitations', 'N/A')}</p>
</div>
"""
html_content += """
</div>
</div>
"""
html_content += "</div>"
return "<h1>The 5 Best Technology Combinations per constraint</h1>" + html_content
def format_final_technologies_html(technologies_list: list) -> str:
html_content = "<div class='final-tech-container'>"
for tech_info in technologies_list:
if isinstance(tech_info, dict):
html_content += f"""
<div class='final-tech-card'>
<h4 class='final-tech-title'>{tech_info.get('title', 'N/A')}</h4>
<p><strong>Purpose:</strong> {tech_info.get('purpose', 'N/A')}</p>
<p><strong>Components:</strong> {tech_info.get('key_components', 'N/A')}</p>
<p><strong>Advantages:</strong> {tech_info.get('advantages', 'N/A')}</p>
<p><strong>Limitations:</strong> {tech_info.get('limitations', 'N/A')}</p>
</div>
"""
html_content += "</div>"
return "<h1>The best technologies combinations </h1>" + html_content
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)
print(constraints)
print(best_combinations)
print(best_technologies)
# Format outputs for Gradio
# For Constraints:
constraints_html = format_constraints_html(constraints)
# For Best Combinations:
best_combinations_html = format_best_combinations_html(best_combinations)
# For Final Technologies:
final_technologies_html = format_final_technologies_html(best_technologies)
prior_art = process_prior_art(best_technologies, constraints, "constraints")
print(prior_art)
return (
prompt,
constraints_html, # Output HTML for constraints
best_combinations_html, # Output HTML for best combinations
", ".join(map(str, best_technologies_id)), # Still a simple text list
final_technologies_html # Output HTML for final technologies
)
# --- Gradio Interface Setup ---
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.HTML(label="2. Retrieved Constraints") # Changed to HTML
output_best_combinations = gr.HTML(label="7. Best Technology Combinations Found") # Changed to HTML
output_selected_ids = gr.Textbox(label="8. Selected Technology IDs", interactive=False)
output_final_technologies = gr.HTML(label="9. Final Best Technologies") # Changed to HTML
# Custom CSS for a professional look and specific output styling
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;
}
/* --- Specific Styling for Outputs --- */
/* 2. Retrieved Constraints Styling */
.constraints-container {
padding: 15px;
background-color: #f8f9fa;
border-radius: 8px;
border: 1px solid #e9ecef;
font-family: 'Trebuchet MS', 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', Tahoma, sans-serif; /* Different font */
line-height: 1.6;
max-height: 300px;
overflow-y: auto;
}
.constraint-item {
margin-bottom: 10px;
padding-bottom: 10px;
border-bottom: 1px dashed #e0e0e0;
}
.constraint-item:last-child {
border-bottom: none;
margin-bottom: 0;
padding-bottom: 0;
}
.constraint-title {
font-weight: bold;
color: #004085; /* Darker blue for constraint titles */
font-size: 1.1em;
}
.constraint-description {
color: #333;
font-size: 1em;
}
/* 7. Best Technology Combinations Found & 9. Final Best Technologies Styling */
.combinations-outer-container, .final-tech-container {
padding: 15px;
background-color: #f8f9fa;
border-radius: 8px;
border: 1px solid #e9ecef;
max-height: 500px; /* Adjust as needed */
overflow-y: auto;
font-family: 'Trebuchet MS', 'Lucida Grande', 'Lucida Sans Unicode', 'Lucida Sans', Tahoma, sans-serif; /* Different font */
}
.problem-card {
background-color: #ffffff;
border: 1px solid #cfe2ff; /* Light blue border for problem card */
border-radius: 10px;
padding: 20px;
margin-bottom: 20px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.05);
}
.problem-card-title {
color: #0056b3; /* Deep blue for problem title */
font-size: 1.4em;
margin-top: 0;
margin-bottom: 15px;
border-bottom: 2px solid #cfe2ff;
padding-bottom: 10px;
}
.technologies-inner-container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); /* Responsive grid for technologies */
gap: 15px;
}
.technology-card, .final-tech-card {
background-color: #f0faff; /* Very light blue for technology cards */
border: 1px solid #b0d9ff; /* Slightly darker blue border */
border-radius: 8px;
padding: 15px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
transition: transform 0.2s ease-in-out;
}
.technology-card:hover, .final-tech-card:hover {
transform: translateY(-3px);
}
.tech-card-title, .final-tech-title {
color: #007bff; /* Gradio's primary blue */
font-size: 1.2em;
margin-top: 0;
margin-bottom: 10px;
font-weight: 600;
}
.technology-card p, .final-tech-card p {
font-size: 0.95em;
line-height: 1.5;
margin-bottom: 5px;
color: #555;
}
.technology-card p strong, .final-tech-card p strong {
color: #004085; /* Darker blue for bold labels */
}
/* Responsive adjustments */
@media (max-width: 768px) {
.gradio-container {
padding: 15px;
}
.gradio-container h1 {
font-size: 2em;
}
.gradio-container button {
width: 100%;
padding: 15px;
}
.technologies-inner-container {
grid-template-columns: 1fr; /* Stack columns on smaller screens */
}
}
"""
# Create the Gradio Blocks demo with custom theme and CSS
with gr.Blocks(
theme=gr.themes.Soft(),
css=custom_css
) as gradio_app_blocks:
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():
with gr.Column(scale=2):
input_problem.render()
with gr.Column(scale=1):
gr.Markdown("Click to start the analysis:"),
process_button = gr.Button("Process Problem", elem_id="process_button")
gr.Markdown("---")
gr.Markdown("### Processing Steps & Results:")
with gr.Row():
with gr.Column():
output_prompt.render()
output_constraints.render() # Renders HTML
with gr.Column():
output_selected_ids.render() # This remains a Textbox
output_best_combinations.render() # Renders HTML
output_final_technologies.render() # Renders HTML
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")
#if __name__ == "__main__":
# gradio_app_blocks.launch() |