Update app.py
Browse files
app.py
CHANGED
|
@@ -29,170 +29,6 @@ async def process(constraints: InputConstraints):
|
|
| 29 |
result = process_input(constraints, global_tech, global_tech_embeddings)
|
| 30 |
return {"technologies": result}
|
| 31 |
|
| 32 |
-
import gradio as gr
|
| 33 |
-
import pandas as pd
|
| 34 |
-
import numpy as np
|
| 35 |
-
import random
|
| 36 |
-
import json
|
| 37 |
-
|
| 38 |
-
# --- Dummy Implementations for src.services.utils and src.services.processor ---
|
| 39 |
-
# These functions simulate the behavior of your actual services for the Gradio interface.
|
| 40 |
-
|
| 41 |
-
def load_technologies():
|
| 42 |
-
"""
|
| 43 |
-
Dummy function to simulate loading technologies and their embeddings.
|
| 44 |
-
Returns a sample DataFrame and a dummy numpy array for embeddings.
|
| 45 |
-
"""
|
| 46 |
-
tech_data = {
|
| 47 |
-
'id': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
|
| 48 |
-
'name': [
|
| 49 |
-
'Machine Learning', 'Cloud Computing', 'Blockchain', 'Cybersecurity',
|
| 50 |
-
'Data Analytics', 'Artificial Intelligence', 'DevOps', 'Quantum Computing',
|
| 51 |
-
'Edge Computing', 'Robotics'
|
| 52 |
-
],
|
| 53 |
-
'description': [
|
| 54 |
-
'Algorithms for learning from data.', 'On-demand computing resources.',
|
| 55 |
-
'Decentralized ledger technology.', 'Protecting systems from threats.',
|
| 56 |
-
'Analyzing large datasets.', 'Simulating human intelligence.',
|
| 57 |
-
'Software development and operations.', 'Utilizing quantum mechanics.',
|
| 58 |
-
'Processing data near the source.', 'Automated machines.'
|
| 59 |
-
]
|
| 60 |
-
}
|
| 61 |
-
global_tech_df = pd.DataFrame(tech_data)
|
| 62 |
-
# Simulate embeddings as random vectors
|
| 63 |
-
global_tech_embeddings_array = np.random.rand(len(global_tech_df), 128)
|
| 64 |
-
return global_tech_df, global_tech_embeddings_array
|
| 65 |
-
|
| 66 |
-
def set_prompt(problem_description: str) -> str:
|
| 67 |
-
"""
|
| 68 |
-
Dummy function to simulate prompt generation.
|
| 69 |
-
"""
|
| 70 |
-
return f"Based on the problem: '{problem_description}', what are the key technical challenges and requirements?"
|
| 71 |
-
|
| 72 |
-
def retrieve_constraints(prompt: str) -> list[str]:
|
| 73 |
-
"""
|
| 74 |
-
Dummy function to simulate constraint retrieval.
|
| 75 |
-
Returns a few sample constraints based on the prompt.
|
| 76 |
-
"""
|
| 77 |
-
if "security" in prompt.lower() or "secure" in prompt.lower():
|
| 78 |
-
return ["high security", "data privacy", "authentication"]
|
| 79 |
-
elif "performance" in prompt.lower() or "speed" in prompt.lower():
|
| 80 |
-
return ["low latency", "high throughput", "scalability"]
|
| 81 |
-
elif "data" in prompt.lower() or "analyze" in prompt.lower():
|
| 82 |
-
return ["data integration", "real-time analytics", "data storage"]
|
| 83 |
-
return ["cost-efficiency", "ease of integration", "maintainability", "scalability"]
|
| 84 |
-
|
| 85 |
-
def stem(text_list: list[str], type_of_text: str) -> list[str]:
|
| 86 |
-
"""
|
| 87 |
-
Dummy function to simulate stemming.
|
| 88 |
-
Simplistically removes 'ing', 's', 'es' from words.
|
| 89 |
-
"""
|
| 90 |
-
stemmed_list = []
|
| 91 |
-
for text in text_list:
|
| 92 |
-
words = text.split()
|
| 93 |
-
stemmed_words = []
|
| 94 |
-
for word in words:
|
| 95 |
-
word = word.lower()
|
| 96 |
-
if word.endswith("ing"):
|
| 97 |
-
word = word[:-3]
|
| 98 |
-
elif word.endswith("es"):
|
| 99 |
-
word = word[:-2]
|
| 100 |
-
elif word.endswith("s"):
|
| 101 |
-
word = word[:-1]
|
| 102 |
-
stemmed_words.append(word)
|
| 103 |
-
stemmed_list.append(" ".join(stemmed_words))
|
| 104 |
-
return stemmed_list
|
| 105 |
-
|
| 106 |
-
def save_dataframe(df: pd.DataFrame, filename: str):
|
| 107 |
-
"""
|
| 108 |
-
Dummy function to simulate saving a DataFrame.
|
| 109 |
-
"""
|
| 110 |
-
print(f"Simulating saving DataFrame to {filename}")
|
| 111 |
-
# In a real scenario, you might save to Excel: df.to_excel(filename, index=False)
|
| 112 |
-
|
| 113 |
-
def save_to_pickle(data):
|
| 114 |
-
"""
|
| 115 |
-
Dummy function to simulate saving data to a pickle file.
|
| 116 |
-
"""
|
| 117 |
-
print(f"Simulating saving data to pickle: {type(data)}")
|
| 118 |
-
|
| 119 |
-
def get_contrastive_similarities(constraints_stemmed: list[str], global_tech_df: pd.DataFrame, global_tech_embeddings: np.ndarray):
|
| 120 |
-
"""
|
| 121 |
-
Dummy function to simulate getting contrastive similarities.
|
| 122 |
-
Returns a dummy similarity matrix and result similarities.
|
| 123 |
-
"""
|
| 124 |
-
num_constraints = len(constraints_stemmed)
|
| 125 |
-
num_tech = len(global_tech_df)
|
| 126 |
-
|
| 127 |
-
# Simulate a similarity matrix
|
| 128 |
-
# Each row corresponds to a constraint, each column to a technology
|
| 129 |
-
matrix = np.random.rand(num_constraints, num_tech)
|
| 130 |
-
matrix = np.round(matrix, 3) # Round for better display
|
| 131 |
-
|
| 132 |
-
# Simulate result_similarities (e.g., top 3 technologies for each constraint)
|
| 133 |
-
result_similarities = {}
|
| 134 |
-
for i, constraint in enumerate(constraints_stemmed):
|
| 135 |
-
# Get top 3 tech indices for this constraint
|
| 136 |
-
top_tech_indices = np.argsort(matrix[i])[::-1][:3]
|
| 137 |
-
top_tech_names = [global_tech_df.iloc[idx]['name'] for idx in top_tech_indices]
|
| 138 |
-
top_tech_scores = [matrix[i, idx] for idx in top_tech_indices]
|
| 139 |
-
result_similarities[constraint] = list(zip(top_tech_names, top_tech_scores))
|
| 140 |
-
|
| 141 |
-
return result_similarities, matrix
|
| 142 |
-
|
| 143 |
-
def find_best_list_combinations(constraints_stemmed: list[str], global_tech_df: pd.DataFrame, matrix: np.ndarray) -> list[dict]:
|
| 144 |
-
"""
|
| 145 |
-
Dummy function to simulate finding best list combinations.
|
| 146 |
-
Returns a few dummy combinations of technologies.
|
| 147 |
-
"""
|
| 148 |
-
best_combinations = []
|
| 149 |
-
# Simulate finding combinations that best cover constraints
|
| 150 |
-
for i in range(min(3, len(constraints_stemmed))): # Create up to 3 dummy combinations
|
| 151 |
-
combination = {
|
| 152 |
-
"technologies": [],
|
| 153 |
-
"score": round(random.uniform(0.7, 0.95), 2),
|
| 154 |
-
"covered_constraints": []
|
| 155 |
-
}
|
| 156 |
-
num_tech_in_combo = random.randint(2, 4)
|
| 157 |
-
selected_tech_ids = random.sample(global_tech_df['id'].tolist(), num_tech_in_combo)
|
| 158 |
-
for tech_id in selected_tech_ids:
|
| 159 |
-
tech_name = global_tech_df[global_tech_df['id'] == tech_id]['name'].iloc[0]
|
| 160 |
-
combination["technologies"].append({"id": tech_id, "name": tech_name})
|
| 161 |
-
|
| 162 |
-
# Assign some random constraints to be covered
|
| 163 |
-
num_covered_constraints = random.randint(1, len(constraints_stemmed))
|
| 164 |
-
combination["covered_constraints"] = random.sample(constraints_stemmed, num_covered_constraints)
|
| 165 |
-
|
| 166 |
-
best_combinations.append(combination)
|
| 167 |
-
return best_combinations
|
| 168 |
-
|
| 169 |
-
def select_technologies(best_combinations: list[dict]) -> list[int]:
|
| 170 |
-
"""
|
| 171 |
-
Dummy function to simulate selecting technologies based on best combinations.
|
| 172 |
-
Returns a list of unique technology IDs.
|
| 173 |
-
"""
|
| 174 |
-
selected_ids = set()
|
| 175 |
-
for combo in best_combinations:
|
| 176 |
-
for tech in combo["technologies"]:
|
| 177 |
-
selected_ids.add(tech["id"])
|
| 178 |
-
return list(selected_ids)
|
| 179 |
-
|
| 180 |
-
def get_technologies_by_id(tech_ids: list[int], global_tech_df: pd.DataFrame) -> list[dict]:
|
| 181 |
-
"""
|
| 182 |
-
Dummy function to simulate retrieving technology details by ID.
|
| 183 |
-
"""
|
| 184 |
-
selected_technologies = []
|
| 185 |
-
for tech_id in tech_ids:
|
| 186 |
-
tech_info = global_tech_df[global_tech_df['id'] == tech_id]
|
| 187 |
-
if not tech_info.empty:
|
| 188 |
-
selected_technologies.append(tech_info.iloc[0].to_dict())
|
| 189 |
-
return selected_technologies
|
| 190 |
-
|
| 191 |
-
# --- Core Logic (Modified for Gradio Interface) ---
|
| 192 |
-
|
| 193 |
-
# Load global technologies and embeddings once when the app starts
|
| 194 |
-
global_tech_df, global_tech_embeddings_array = load_technologies()
|
| 195 |
-
|
| 196 |
def process_input_gradio(problem_description: str):
|
| 197 |
"""
|
| 198 |
Processes the input problem description step-by-step for Gradio.
|
|
@@ -227,10 +63,8 @@ def process_input_gradio(problem_description: str):
|
|
| 227 |
best_technologies = get_technologies_by_id(best_technologies_id, global_tech_df)
|
| 228 |
|
| 229 |
# Format outputs for Gradio
|
| 230 |
-
# Convert numpy array to list of lists for better Gradio display
|
| 231 |
-
matrix_display = matrix.tolist()
|
| 232 |
|
| 233 |
-
# Convert result_similarities to a more readable format for Gradio
|
| 234 |
result_similarities_display = {
|
| 235 |
k: ", ".join([f"{name} ({score:.3f})" for name, score in v])
|
| 236 |
for k, v in result_similarities.items()
|
|
@@ -269,23 +103,36 @@ output_best_combinations = gr.JSON(label="7. Best Technology Combinations Found"
|
|
| 269 |
output_selected_ids = gr.Textbox(label="8. Selected Technology IDs", interactive=False)
|
| 270 |
output_final_technologies = gr.JSON(label="9. Final Best Technologies", interactive=False)
|
| 271 |
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
gr.
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
result = process_input(constraints, global_tech, global_tech_embeddings)
|
| 30 |
return {"technologies": result}
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
def process_input_gradio(problem_description: str):
|
| 33 |
"""
|
| 34 |
Processes the input problem description step-by-step for Gradio.
|
|
|
|
| 63 |
best_technologies = get_technologies_by_id(best_technologies_id, global_tech_df)
|
| 64 |
|
| 65 |
# Format outputs for Gradio
|
| 66 |
+
matrix_display = matrix.tolist() # Convert numpy array to list of lists for better Gradio display
|
|
|
|
| 67 |
|
|
|
|
| 68 |
result_similarities_display = {
|
| 69 |
k: ", ".join([f"{name} ({score:.3f})" for name, score in v])
|
| 70 |
for k, v in result_similarities.items()
|
|
|
|
| 103 |
output_selected_ids = gr.Textbox(label="8. Selected Technology IDs", interactive=False)
|
| 104 |
output_final_technologies = gr.JSON(label="9. Final Best Technologies", interactive=False)
|
| 105 |
|
| 106 |
+
# Create the Gradio Blocks demo
|
| 107 |
+
with gr.Blocks() as gradio_app_blocks:
|
| 108 |
+
gr.Markdown("# Insight Finder: Step-by-Step Technology Selection")
|
| 109 |
+
gr.Markdown("Enter a problem description to see how relevant technologies are identified through various processing steps.")
|
| 110 |
+
input_problem.render()
|
| 111 |
+
process_button = gr.Button("Process Problem")
|
| 112 |
+
|
| 113 |
+
with gr.Column():
|
| 114 |
+
output_prompt.render()
|
| 115 |
+
output_constraints.render()
|
| 116 |
+
output_stemmed_constraints.render()
|
| 117 |
+
output_tech_loaded.render()
|
| 118 |
+
output_similarities.render()
|
| 119 |
+
output_matrix.render()
|
| 120 |
+
output_best_combinations.render()
|
| 121 |
+
output_selected_ids.render()
|
| 122 |
+
output_final_technologies.render()
|
| 123 |
+
|
| 124 |
+
process_button.click(
|
| 125 |
+
fn=process_input_gradio,
|
| 126 |
+
inputs=input_problem,
|
| 127 |
+
outputs=[
|
| 128 |
+
output_prompt,
|
| 129 |
+
output_constraints,
|
| 130 |
+
output_stemmed_constraints,
|
| 131 |
+
output_tech_loaded,
|
| 132 |
+
output_similarities,
|
| 133 |
+
output_matrix,
|
| 134 |
+
output_best_combinations,
|
| 135 |
+
output_selected_ids,
|
| 136 |
+
output_final_technologies
|
| 137 |
+
]
|
| 138 |
+
)
|