Spaces:
Runtime error
Runtime error
adding more features to the app
Browse files- app.py +47 -8
- chain_apparatarus_weaviate.py +89 -0
- chain_weaviate.py → chain_experiments_weaviate.py +5 -0
- mesh_utils.py +48 -0
app.py
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
from structured_apparatus_chain import (
|
| 3 |
arxiv_chain as apparatus_arxiv_chain,
|
| 4 |
pub_med_chain as apparatus_pub_med_chain,
|
|
@@ -10,7 +12,6 @@ from structured_experiment_chain import (
|
|
| 10 |
wikipedia_chain as experiment_wikipedia_chain
|
| 11 |
)
|
| 12 |
|
| 13 |
-
from weaviate_utils import init_client
|
| 14 |
|
| 15 |
apparatus_retriever_options = {
|
| 16 |
"Arxiv": apparatus_arxiv_chain,
|
|
@@ -27,6 +28,19 @@ experiment_retriever_options = {
|
|
| 27 |
def generate_apparatus(input_text, retriever_choice):
|
| 28 |
selected_chain = apparatus_retriever_options[retriever_choice]
|
| 29 |
output_text = selected_chain.invoke(input_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
return output_text
|
| 31 |
|
| 32 |
def generate_experiment(input_text, retriever_choice):
|
|
@@ -51,15 +65,31 @@ def generate_experiment(input_text, retriever_choice):
|
|
| 51 |
})
|
| 52 |
return output_text
|
| 53 |
|
| 54 |
-
def
|
| 55 |
# Example processing function
|
| 56 |
weaviate_client = init_client()
|
| 57 |
science_experiment_collection = weaviate_client.collections.get("ScienceEperiment")
|
| 58 |
response = science_experiment_collection.query.bm25(
|
| 59 |
query=input_text,
|
| 60 |
-
limit=
|
| 61 |
)
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
generate_apparatus_interface = gr.Interface(
|
| 65 |
fn=generate_apparatus,
|
|
@@ -77,19 +107,28 @@ generate_experiment_interface = gr.Interface(
|
|
| 77 |
description="I am here to generate and store science experiments for our users",
|
| 78 |
)
|
| 79 |
|
| 80 |
-
|
| 81 |
-
fn=
|
| 82 |
inputs=["text", gr.Slider(minimum=2, maximum=6, step=1, value=2, label="Select a number")],
|
| 83 |
outputs="text",
|
| 84 |
title="Search Existing Experiments",
|
| 85 |
description="If you would like an idea of the experiments in the vectorestore here is the place",
|
| 86 |
)
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
demo = gr.TabbedInterface([
|
| 89 |
generate_apparatus_interface,
|
| 90 |
generate_experiment_interface,
|
| 91 |
-
|
| 92 |
-
|
|
|
|
| 93 |
|
| 94 |
if __name__ == "__main__":
|
| 95 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from weaviate_utils import init_client
|
| 3 |
+
|
| 4 |
from structured_apparatus_chain import (
|
| 5 |
arxiv_chain as apparatus_arxiv_chain,
|
| 6 |
pub_med_chain as apparatus_pub_med_chain,
|
|
|
|
| 12 |
wikipedia_chain as experiment_wikipedia_chain
|
| 13 |
)
|
| 14 |
|
|
|
|
| 15 |
|
| 16 |
apparatus_retriever_options = {
|
| 17 |
"Arxiv": apparatus_arxiv_chain,
|
|
|
|
| 28 |
def generate_apparatus(input_text, retriever_choice):
|
| 29 |
selected_chain = apparatus_retriever_options[retriever_choice]
|
| 30 |
output_text = selected_chain.invoke(input_text)
|
| 31 |
+
weaviate_client = init_client()
|
| 32 |
+
app_components = output_text["Material"]
|
| 33 |
+
component_collection = weaviate_client.collections.get("Component")
|
| 34 |
+
|
| 35 |
+
for i in app_components:
|
| 36 |
+
|
| 37 |
+
app_uuid = component_collection.data.insert({
|
| 38 |
+
"Tags": output_text['Fields_of_study'],
|
| 39 |
+
"FeildsOfStudy" : output_text['Fields_of_study'],
|
| 40 |
+
"ToolName" : i,
|
| 41 |
+
"UsedInComps" : [input_text]
|
| 42 |
+
})
|
| 43 |
+
|
| 44 |
return output_text
|
| 45 |
|
| 46 |
def generate_experiment(input_text, retriever_choice):
|
|
|
|
| 65 |
})
|
| 66 |
return output_text
|
| 67 |
|
| 68 |
+
def search_experiments(input_text, number):
|
| 69 |
# Example processing function
|
| 70 |
weaviate_client = init_client()
|
| 71 |
science_experiment_collection = weaviate_client.collections.get("ScienceEperiment")
|
| 72 |
response = science_experiment_collection.query.bm25(
|
| 73 |
query=input_text,
|
| 74 |
+
limit=number
|
| 75 |
)
|
| 76 |
+
weaviate_client.close()
|
| 77 |
+
response_objects_string = "\n\n".join([str(obj) for obj in response.objects])
|
| 78 |
+
return response_objects_string
|
| 79 |
+
|
| 80 |
+
def search_apparatus(input_text, number):
|
| 81 |
+
# Example processing function
|
| 82 |
+
weaviate_client = init_client()
|
| 83 |
+
component_collection = weaviate_client.collections.get("Component")
|
| 84 |
+
response = component_collection.query.bm25(
|
| 85 |
+
query=input_text,
|
| 86 |
+
limit=number
|
| 87 |
+
)
|
| 88 |
+
# print(response.objects.__str__())
|
| 89 |
+
response_objects_string = "\n\n".join([str(obj) for obj in response.objects])
|
| 90 |
+
weaviate_client.close()
|
| 91 |
+
|
| 92 |
+
return response_objects_string
|
| 93 |
|
| 94 |
generate_apparatus_interface = gr.Interface(
|
| 95 |
fn=generate_apparatus,
|
|
|
|
| 107 |
description="I am here to generate and store science experiments for our users",
|
| 108 |
)
|
| 109 |
|
| 110 |
+
search_experiments_interface = gr.Interface(
|
| 111 |
+
fn=search_experiments,
|
| 112 |
inputs=["text", gr.Slider(minimum=2, maximum=6, step=1, value=2, label="Select a number")],
|
| 113 |
outputs="text",
|
| 114 |
title="Search Existing Experiments",
|
| 115 |
description="If you would like an idea of the experiments in the vectorestore here is the place",
|
| 116 |
)
|
| 117 |
|
| 118 |
+
search_apparatus_interface = gr.Interface(
|
| 119 |
+
fn=search_apparatus,
|
| 120 |
+
inputs=["text", gr.Slider(minimum=2, maximum=6, step=1, value=2, label="Select a number")],
|
| 121 |
+
outputs="text",
|
| 122 |
+
title="Search Existing Apparatuses",
|
| 123 |
+
description="If you would like an idea of the apparatuses in the vectorestore here is the place",
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
demo = gr.TabbedInterface([
|
| 127 |
generate_apparatus_interface,
|
| 128 |
generate_experiment_interface,
|
| 129 |
+
search_experiments_interface,
|
| 130 |
+
search_apparatus_interface,
|
| 131 |
+
], ["Generate Apparatus", "Generate Experiment", "Search Existing Experiments","Search Existing Apparatuses"])
|
| 132 |
|
| 133 |
if __name__ == "__main__":
|
| 134 |
demo.launch()
|
chain_apparatarus_weaviate.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# goal: store results from app.py into vector store
|
| 2 |
+
|
| 3 |
+
from structured_apparatus_chain import (
|
| 4 |
+
arxiv_chain as apparatus_arxiv_chain,
|
| 5 |
+
pub_med_chain as apparatus_pub_med_chain,
|
| 6 |
+
wikipedia_chain as apparatus_wikipedia_chain
|
| 7 |
+
)
|
| 8 |
+
from structured_experiment_chain import (
|
| 9 |
+
arxiv_chain as experiment_arxiv_chain,
|
| 10 |
+
pub_med_chain as experiment_pub_med_chain,
|
| 11 |
+
wikipedia_chain as experiment_wikipedia_chain
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
from weaviate_utils import init_client
|
| 15 |
+
|
| 16 |
+
from datetime import datetime, timezone
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def main():
|
| 22 |
+
# exp_qury = "fabricating cellolouse based electronics"
|
| 23 |
+
# exp_qury = "fabrication of spider silk"
|
| 24 |
+
# app_query = "microscope"
|
| 25 |
+
# app_query = "A gas Condenser"
|
| 26 |
+
app_query = "Electron Microscope"
|
| 27 |
+
app_data = apparatus_arxiv_chain.invoke(app_query)
|
| 28 |
+
# exp_data = experiment_arxiv_chain.invoke(exp_qury)
|
| 29 |
+
|
| 30 |
+
weaviate_client = init_client()
|
| 31 |
+
|
| 32 |
+
component_collection = weaviate_client.collections.get("Component")
|
| 33 |
+
component_image_collection = weaviate_client.collections.get("ComponentImage")
|
| 34 |
+
science_experiment_collection = weaviate_client.collections.get("ScienceEperiment")
|
| 35 |
+
|
| 36 |
+
app_components = app_data["Material"]
|
| 37 |
+
|
| 38 |
+
for i in app_components:
|
| 39 |
+
|
| 40 |
+
app_uuid = component_collection.data.insert({
|
| 41 |
+
"Tags": app_data['Fields_of_study'],
|
| 42 |
+
"FeildsOfStudy" : app_data['Fields_of_study'],
|
| 43 |
+
"ToolName" : i,
|
| 44 |
+
"UsedInComps" : [app_query]
|
| 45 |
+
})
|
| 46 |
+
|
| 47 |
+
response = component_collection.query.bm25(
|
| 48 |
+
query="something that goes in a microscope",
|
| 49 |
+
limit=5
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
# exp_uuid = science_experiment_collection.data.insert({
|
| 53 |
+
# # "DateCreated": datetime.now(timezone.utc),
|
| 54 |
+
# "FieldsOfStudy": exp_data['Fields_of_study'],
|
| 55 |
+
# "Tags": exp_data['Fields_of_study'],
|
| 56 |
+
# "Experiment_Name": exp_data['Experiment_Name'],
|
| 57 |
+
# "Material": exp_data['Material'],
|
| 58 |
+
# "Sources": exp_data['Sources'],
|
| 59 |
+
# "Protocal": exp_data['Protocal'],
|
| 60 |
+
# "Purpose_of_Experiments": exp_data['Purpose_of_Experiments'],
|
| 61 |
+
# "Safety_Precaution": exp_data['Safety_Precuation'], # Corrected spelling mistake
|
| 62 |
+
# "Level_of_Difficulty": exp_data['Level_of_Difficulty'],
|
| 63 |
+
# })
|
| 64 |
+
|
| 65 |
+
response = science_experiment_collection.query.bm25(
|
| 66 |
+
query="silk",
|
| 67 |
+
limit=3
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
jj = science_experiment_collection.query.near_text(
|
| 71 |
+
query="biology",
|
| 72 |
+
limit=2
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
# uuid = component_collection.data.insert({
|
| 78 |
+
# "DateCreated" : datetime.now(timezone.utc),
|
| 79 |
+
# "UsedInComps" : [query],
|
| 80 |
+
# "ToolName" : shap_e_sample,
|
| 81 |
+
# "Tags" : shap_e_list,
|
| 82 |
+
# "feildsOfStudy" : shap_e_list,
|
| 83 |
+
# # "GlbBlob" : base_64_result,
|
| 84 |
+
# })
|
| 85 |
+
|
| 86 |
+
x = 0
|
| 87 |
+
|
| 88 |
+
if __name__ == '__main__':
|
| 89 |
+
main()
|
chain_weaviate.py → chain_experiments_weaviate.py
RENAMED
|
@@ -45,6 +45,11 @@ def main():
|
|
| 45 |
"Level_of_Difficulty": exp_data['Level_of_Difficulty'],
|
| 46 |
})
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
jj = science_experiment_collection.query.near_text(
|
| 49 |
query="biology",
|
| 50 |
limit=2
|
|
|
|
| 45 |
"Level_of_Difficulty": exp_data['Level_of_Difficulty'],
|
| 46 |
})
|
| 47 |
|
| 48 |
+
response = science_experiment_collection.query.bm25(
|
| 49 |
+
query="silk",
|
| 50 |
+
limit=3
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
jj = science_experiment_collection.query.near_text(
|
| 54 |
query="biology",
|
| 55 |
limit=2
|
mesh_utils.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from stl import mesh
|
| 2 |
+
from mpl_toolkits import mplot3d
|
| 3 |
+
from matplotlib import pyplot as plt
|
| 4 |
+
from typing import List, Tuple
|
| 5 |
+
|
| 6 |
+
def generate_mesh_images(file_path: str, viewing_angles: List[Tuple[int, int]], output_prefix: str = 'mesh_') -> None:
|
| 7 |
+
"""
|
| 8 |
+
Generate images of an STL file from different viewing angles.
|
| 9 |
+
|
| 10 |
+
Args:
|
| 11 |
+
file_path (str): Path to the STL file.
|
| 12 |
+
viewing_angles (List[Tuple[int, int]]): List of tuples containing the elevation and azimuth angles for viewing.
|
| 13 |
+
output_prefix (str, optional): Prefix for the output image filenames. Defaults to 'mesh_'.
|
| 14 |
+
"""
|
| 15 |
+
# Load the STL file
|
| 16 |
+
your_mesh = mesh.Mesh.from_file(file_path)
|
| 17 |
+
|
| 18 |
+
# Iterate over each viewing angle and generate an image
|
| 19 |
+
for i, (elev, azim) in enumerate(viewing_angles, start=1):
|
| 20 |
+
# Create a new plot with a larger figure size
|
| 21 |
+
fig = plt.figure(figsize=(10, 10))
|
| 22 |
+
ax = fig.add_subplot(111, projection='3d')
|
| 23 |
+
|
| 24 |
+
# Add the STL file to the plot
|
| 25 |
+
ax.add_collection3d(mplot3d.art3d.Poly3DCollection(your_mesh.vectors))
|
| 26 |
+
|
| 27 |
+
# Calculate the limits of the mesh
|
| 28 |
+
max_dim = max(your_mesh.points.flatten())
|
| 29 |
+
min_dim = min(your_mesh.points.flatten())
|
| 30 |
+
|
| 31 |
+
# Set the limits of the plot
|
| 32 |
+
ax.set_xlim([min_dim, max_dim])
|
| 33 |
+
ax.set_ylim([min_dim, max_dim])
|
| 34 |
+
ax.set_zlim([min_dim, max_dim])
|
| 35 |
+
|
| 36 |
+
# Set the viewing angle
|
| 37 |
+
ax.view_init(elev=elev, azim=azim)
|
| 38 |
+
|
| 39 |
+
# Save the plot as an image
|
| 40 |
+
plt.savefig(f'{output_prefix}{i}.png')
|
| 41 |
+
|
| 42 |
+
# Close the plot to avoid memory leaks
|
| 43 |
+
plt.close()
|
| 44 |
+
|
| 45 |
+
# Example usage:
|
| 46 |
+
file_path = 'sample_data.stl'
|
| 47 |
+
viewing_angles = [(30, 45), (60, 90), (45, 135)]
|
| 48 |
+
generate_mesh_images(file_path, viewing_angles)
|