Spaces:
Runtime error
Runtime error
File size: 5,661 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 7a97701 882d620 153d9eb 882d620 b2a9019 882d620 b2a9019 882d620 7aaac7c 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 |
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)
return (
prompt,
"\n ".join(constraints),
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")
# Create the Gradio Blocks demo
with gr.Blocks() 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.")
input_problem.render()
process_button = gr.Button("Process Problem")
with gr.Column():
output_prompt.render()
output_constraints.render()
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") |