|
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] |
|
|
|
|
|
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 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'): |
|
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. |
|
""" |
|
|
|
prompt = set_prompt(problem_description) |
|
|
|
|
|
constraints = retrieve_constraints(prompt) |
|
|
|
|
|
constraints_stemmed = stem(constraints, "constraints") |
|
save_dataframe(pd.DataFrame({"stemmed_constraints": constraints_stemmed}), "constraints_stemmed.xlsx") |
|
print(constraints_stemmed) |
|
|
|
|
|
|
|
|
|
|
|
result_similarities, matrix = get_contrastive_similarities( |
|
constraints_stemmed, global_tech, global_tech_embeddings |
|
) |
|
save_to_pickle(result_similarities) |
|
|
|
|
|
best_combinations = find_best_list_combinations(constraints_stemmed, global_tech, matrix) |
|
|
|
|
|
best_technologies_id = select_technologies(best_combinations) |
|
|
|
|
|
best_technologies = get_technologies_by_id(best_technologies_id, global_tech) |
|
|
|
|
|
matrix_display = matrix |
|
|
|
result_similarities_display = { |
|
item['id2']: f"{item['constraint']['title']} ({item['similarity'].item():.3f})" |
|
for item in result_similarities |
|
} |
|
|
|
|
|
|
|
safe_best_combinations = make_json_serializable(best_combinations) |
|
safe_best_technologies = make_json_serializable(best_technologies) |
|
|
|
|
|
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 |
|
) |
|
|
|
|
|
|
|
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 = """ |
|
/* 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; |
|
} */ |
|
""" |
|
|
|
|
|
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() |
|
with gr.Column(): |
|
output_best_combinations.render() |
|
output_selected_ids.render() |
|
output_final_technologies.render() |
|
|
|
|
|
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") |