Spaces:
Runtime error
Runtime error
finishinig the MVP and wow factor
Browse files- app.py +94 -1
- chain_apparatarus_weaviate.py +36 -0
- google_buckets.py +61 -38
- multi_angle_stl.py +36 -36
- utils.py +19 -0
app.py
CHANGED
@@ -12,6 +12,22 @@ from structured_experiment_chain import (
|
|
12 |
wikipedia_chain as experiment_wikipedia_chain
|
13 |
)
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
apparatus_retriever_options = {
|
17 |
"Arxiv": apparatus_arxiv_chain,
|
@@ -32,7 +48,20 @@ def generate_apparatus(input_text, retriever_choice):
|
|
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'],
|
@@ -40,6 +69,14 @@ def generate_apparatus(input_text, retriever_choice):
|
|
40 |
"ToolName" : i,
|
41 |
"UsedInComps" : [input_text]
|
42 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
return output_text
|
45 |
|
@@ -91,6 +128,47 @@ def search_apparatus(input_text, number):
|
|
91 |
|
92 |
return response_objects_string
|
93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
generate_apparatus_interface = gr.Interface(
|
95 |
fn=generate_apparatus,
|
96 |
inputs=["text", gr.Radio(choices=list(apparatus_retriever_options.keys()), label="Select a retriever", value="Wikipedia")],
|
@@ -123,12 +201,27 @@ search_apparatus_interface = gr.Interface(
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
|
133 |
if __name__ == "__main__":
|
134 |
demo.launch()
|
|
|
12 |
wikipedia_chain as experiment_wikipedia_chain
|
13 |
)
|
14 |
|
15 |
+
from google_buckets import CloudStorageManager
|
16 |
+
import dotenv
|
17 |
+
import os
|
18 |
+
|
19 |
+
from utils import (
|
20 |
+
change_file_extension, convert_obj_to_stl,
|
21 |
+
remove_files
|
22 |
+
)
|
23 |
+
|
24 |
+
from mesh_utils import generate_mesh_images
|
25 |
+
|
26 |
+
from vision_model import analyze_images
|
27 |
+
|
28 |
+
from gradio_client import Client as ShapEClient
|
29 |
+
|
30 |
+
dotenv.load_dotenv()
|
31 |
|
32 |
apparatus_retriever_options = {
|
33 |
"Arxiv": apparatus_arxiv_chain,
|
|
|
48 |
app_components = output_text["Material"]
|
49 |
component_collection = weaviate_client.collections.get("Component")
|
50 |
|
51 |
+
bucket_name = os.getenv('GOOGLE_BUCKET_NAME')
|
52 |
+
manager = CloudStorageManager(bucket_name)
|
53 |
+
|
54 |
for i in app_components:
|
55 |
+
|
56 |
+
client = ShapEClient("hysts/Shap-E")
|
57 |
+
client.hf_token = os.getenv("HUGGINGFACE_API_KEY")
|
58 |
+
result = client.predict(
|
59 |
+
i, # str in 'Prompt' Textbox component
|
60 |
+
1621396601, # float (numeric value between 0 and 2147483647) in 'Seed' Slider component
|
61 |
+
15, # float (numeric value between 1 and 20) in 'Guidance scale' Slider component
|
62 |
+
64, # float (numeric value between 2 and 100) in 'Number of inference steps' Slider component
|
63 |
+
api_name="/text-to-3d"
|
64 |
+
)
|
65 |
|
66 |
app_uuid = component_collection.data.insert({
|
67 |
"Tags": output_text['Fields_of_study'],
|
|
|
69 |
"ToolName" : i,
|
70 |
"UsedInComps" : [input_text]
|
71 |
})
|
72 |
+
|
73 |
+
|
74 |
+
glb_file_name = app_uuid.hex + ".glb"
|
75 |
+
|
76 |
+
manager.upload_file(
|
77 |
+
result,
|
78 |
+
glb_file_name,
|
79 |
+
)
|
80 |
|
81 |
return output_text
|
82 |
|
|
|
128 |
|
129 |
return response_objects_string
|
130 |
|
131 |
+
def review_3d_model(uuid:str) -> None:
|
132 |
+
"""input the uuid of a 3d model"""
|
133 |
+
uuid = uuid.replace("-","")
|
134 |
+
bucket_name = os.getenv('GOOGLE_BUCKET_NAME')
|
135 |
+
manager = CloudStorageManager(bucket_name)
|
136 |
+
xx = manager.get_file_by_uuid(uuid)
|
137 |
+
manager.download_file(
|
138 |
+
xx,
|
139 |
+
xx
|
140 |
+
)
|
141 |
+
xx_as_stl = change_file_extension(xx,"stl")
|
142 |
+
convert_obj_to_stl(xx,xx_as_stl)
|
143 |
+
viewing_angles = [(30, 45), (60, 90), (45, 135)]
|
144 |
+
|
145 |
+
prompt = "I am creating an 3d model of a Glass lenses for refracting light,\
|
146 |
+
using a text-to-3d model\
|
147 |
+
Do these images look correct?\
|
148 |
+
If not please make a suggesttion on how to improve the text input\
|
149 |
+
As this response will be used in a pipeline please only output a new \
|
150 |
+
potential prompt or output nothing, "
|
151 |
+
# Please keep the prompt to 5 25 words to not confuse the model"
|
152 |
+
|
153 |
+
images = generate_mesh_images(
|
154 |
+
xx_as_stl,
|
155 |
+
viewing_angles,
|
156 |
+
|
157 |
+
)
|
158 |
+
|
159 |
+
response = analyze_images(
|
160 |
+
images,
|
161 |
+
prompt,
|
162 |
+
# api_key,
|
163 |
+
)
|
164 |
+
|
165 |
+
#clean up
|
166 |
+
remove_files(images)
|
167 |
+
remove_files([xx,xx_as_stl])
|
168 |
+
return response
|
169 |
+
|
170 |
+
|
171 |
+
|
172 |
generate_apparatus_interface = gr.Interface(
|
173 |
fn=generate_apparatus,
|
174 |
inputs=["text", gr.Radio(choices=list(apparatus_retriever_options.keys()), label="Select a retriever", value="Wikipedia")],
|
|
|
201 |
description="If you would like an idea of the apparatuses in the vectorestore here is the place",
|
202 |
)
|
203 |
|
204 |
+
review_3d_model_interface = gr.Interface(
|
205 |
+
fn=review_3d_model,
|
206 |
+
inputs=["text"],
|
207 |
+
outputs="text",
|
208 |
+
title="Review 3D Model",
|
209 |
+
description="Input the UUID of a 3D model to review its images and provide feedback.",
|
210 |
+
)
|
211 |
+
|
212 |
demo = gr.TabbedInterface([
|
213 |
generate_apparatus_interface,
|
214 |
generate_experiment_interface,
|
215 |
search_experiments_interface,
|
216 |
search_apparatus_interface,
|
217 |
+
review_3d_model_interface,
|
218 |
+
], [
|
219 |
+
"Generate Apparatus",
|
220 |
+
"Generate Experiment",
|
221 |
+
"Search Existing Experiments",
|
222 |
+
"Search Existing Apparatuses",
|
223 |
+
"review_3d_model_interface"
|
224 |
+
])
|
225 |
|
226 |
if __name__ == "__main__":
|
227 |
demo.launch()
|
chain_apparatarus_weaviate.py
CHANGED
@@ -11,12 +11,19 @@ from structured_experiment_chain import (
|
|
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"
|
@@ -33,9 +40,26 @@ def main():
|
|
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'],
|
@@ -43,6 +67,18 @@ def main():
|
|
43 |
"ToolName" : i,
|
44 |
"UsedInComps" : [app_query]
|
45 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
response = component_collection.query.bm25(
|
48 |
query="something that goes in a microscope",
|
|
|
11 |
wikipedia_chain as experiment_wikipedia_chain
|
12 |
)
|
13 |
|
14 |
+
|
15 |
+
# from google_buckets import upload_file, man
|
16 |
+
|
17 |
from weaviate_utils import init_client
|
18 |
|
19 |
from datetime import datetime, timezone
|
20 |
|
21 |
+
from gradio_client import Client as ShapEClient
|
22 |
+
import os
|
23 |
|
24 |
+
from google_buckets import CloudStorageManager
|
25 |
|
26 |
+
from utils import copy_file_to_location
|
27 |
|
28 |
def main():
|
29 |
# exp_qury = "fabricating cellolouse based electronics"
|
|
|
40 |
component_image_collection = weaviate_client.collections.get("ComponentImage")
|
41 |
science_experiment_collection = weaviate_client.collections.get("ScienceEperiment")
|
42 |
|
43 |
+
|
44 |
+
bucket_name = os.getenv('GOOGLE_BUCKET_NAME')
|
45 |
+
manager = CloudStorageManager(bucket_name)
|
46 |
+
|
47 |
+
|
48 |
+
|
49 |
app_components = app_data["Material"]
|
50 |
|
51 |
for i in app_components:
|
52 |
+
|
53 |
+
client = ShapEClient("hysts/Shap-E")
|
54 |
+
client.hf_token = os.getenv("HUGGINGFACE_API_KEY")
|
55 |
+
result = client.predict(
|
56 |
+
i, # str in 'Prompt' Textbox component
|
57 |
+
1621396601, # float (numeric value between 0 and 2147483647) in 'Seed' Slider component
|
58 |
+
15, # float (numeric value between 1 and 20) in 'Guidance scale' Slider component
|
59 |
+
64, # float (numeric value between 2 and 100) in 'Number of inference steps' Slider component
|
60 |
+
api_name="/text-to-3d"
|
61 |
+
)
|
62 |
+
|
63 |
|
64 |
app_uuid = component_collection.data.insert({
|
65 |
"Tags": app_data['Fields_of_study'],
|
|
|
67 |
"ToolName" : i,
|
68 |
"UsedInComps" : [app_query]
|
69 |
})
|
70 |
+
|
71 |
+
glb_file_name = app_uuid.hex + ".glb"
|
72 |
+
|
73 |
+
manager.upload_file(
|
74 |
+
result,
|
75 |
+
glb_file_name,
|
76 |
+
)
|
77 |
+
# copy_file_to_location(result,glb_file_name)
|
78 |
+
# upload_file(glb_file_name)
|
79 |
+
# os.remove(glb_file_name)
|
80 |
+
|
81 |
+
x = 0
|
82 |
|
83 |
response = component_collection.query.bm25(
|
84 |
query="something that goes in a microscope",
|
google_buckets.py
CHANGED
@@ -2,58 +2,81 @@ from google.cloud import storage
|
|
2 |
import os
|
3 |
import json
|
4 |
|
5 |
-
SERVICE_ACOUNT_STUFF = os.getenv('GOOGLE_APPLICATION_CREDENTIALS_JSON')
|
6 |
|
7 |
-
# https://stackoverflow.com/questions/71878229/initializing-firebase-admin-via-environment-variables-without-storing-serviceacc
|
8 |
-
key_dict = json.loads(
|
9 |
-
os.environ["GOOGLE_APPLICATION_CREDENTIALS_JSON"]
|
10 |
-
)
|
11 |
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
13 |
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
|
|
|
|
|
|
|
|
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
client = storage.Client()
|
21 |
|
22 |
-
#
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
file_name = "tmpcpd7o7v0.glb"
|
27 |
|
|
|
28 |
|
29 |
|
30 |
-
#
|
31 |
-
def upload_file(file_path):
|
32 |
-
bucket = client.get_bucket(bucket_name)
|
33 |
-
blob = bucket.blob(file_name)
|
34 |
-
blob.upload_from_filename(file_path)
|
35 |
-
print(f'File {file_name} uploaded to {bucket_name}.')
|
36 |
|
37 |
-
# Function to download a .glb file from the bucket
|
38 |
-
def download_file(destination_path):
|
39 |
-
bucket = client.get_bucket(bucket_name)
|
40 |
-
blob = bucket.blob(file_name)
|
41 |
-
blob.download_to_filename(destination_path)
|
42 |
-
print(f'File {file_name} downloaded to {destination_path}.')
|
43 |
|
44 |
-
#
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
-
def main():
|
52 |
# Example usage
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
delete_file()
|
57 |
|
58 |
if __name__ == "__main__":
|
59 |
main()
|
|
|
2 |
import os
|
3 |
import json
|
4 |
|
|
|
5 |
|
|
|
|
|
|
|
|
|
6 |
|
7 |
+
from google.cloud import storage
|
8 |
+
|
9 |
+
class CloudStorageManager:
|
10 |
+
def __init__(self, bucket_name):
|
11 |
+
self.client = storage.Client()
|
12 |
+
self.bucket_name = bucket_name
|
13 |
|
14 |
+
def upload_file(self, file_path, destination_file_name):
|
15 |
+
bucket = self.client.bucket(self.bucket_name)
|
16 |
+
blob = bucket.blob(destination_file_name)
|
17 |
+
blob.upload_from_filename(file_path)
|
18 |
+
print(f'File {destination_file_name} uploaded to {self.bucket_name}.')
|
19 |
|
20 |
+
def download_file(self, source_file_name, destination_path):
|
21 |
+
bucket = self.client.bucket(self.bucket_name)
|
22 |
+
blob = bucket.blob(source_file_name)
|
23 |
+
blob.download_to_filename(destination_path)
|
24 |
+
print(f'File {source_file_name} downloaded to {destination_path}.')
|
25 |
|
26 |
+
def delete_file(self, file_name):
|
27 |
+
bucket = self.client.bucket(self.bucket_name)
|
28 |
+
blob = bucket.blob(file_name)
|
29 |
+
blob.delete()
|
30 |
+
print(f'File {file_name} deleted from {self.bucket_name}.')
|
31 |
+
|
32 |
+
def get_file_by_uuid(self, uuid):
|
33 |
+
bucket = self.client.bucket(self.bucket_name)
|
34 |
+
blobs = bucket.list_blobs(prefix=uuid)
|
35 |
+
for blob in blobs:
|
36 |
+
if blob.name.endswith('.glb'):
|
37 |
+
return blob.name
|
38 |
+
return None
|
39 |
|
40 |
+
def main():
|
41 |
+
SERVICE_ACOUNT_STUFF = os.getenv('GOOGLE_APPLICATION_CREDENTIALS_JSON')
|
|
|
42 |
|
43 |
+
# https://stackoverflow.com/questions/71878229/initializing-firebase-admin-via-environment-variables-without-storing-serviceacc
|
44 |
+
key_dict = json.loads(
|
45 |
+
os.environ["GOOGLE_APPLICATION_CREDENTIALS_JSON"]
|
46 |
+
)
|
|
|
47 |
|
48 |
+
SERVICE_ACOUNT_STUFF = os.getenv('GOOGLE_APPLICATION_CREDENTIALS_JSON')
|
49 |
|
50 |
|
51 |
+
# fire_app = firebase_admin.initialize_app(Certificate(key_dict))
|
|
|
|
|
|
|
|
|
|
|
52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
+
# Initialize Google Cloud Storage client
|
55 |
+
client = storage.Client()
|
56 |
+
bucket_name = os.getenv('GOOGLE_BUCKET_NAME')
|
57 |
+
manager = CloudStorageManager(bucket_name)
|
58 |
+
|
59 |
+
|
60 |
+
# uuid = '9ca1555c-e8ca-4111-a084-1a2374b2e6bd'
|
61 |
+
# uuid = '9ca1555c-e8ca-4111-a084-1a2374b2e6bd'.replace("-","")
|
62 |
+
uuid = "506bb34a122a4bea86a64f96933f6bbd"
|
63 |
+
xx = manager.get_file_by_uuid(uuid)
|
64 |
+
|
65 |
+
# manager.upload_file(
|
66 |
+
# "506bb34a122a4bea86a64f96933f6bbd.glb",
|
67 |
+
# "506bb34a122a4bea86a64f96933f6bbd.glb"
|
68 |
+
# )
|
69 |
+
|
70 |
+
manager.download_file(
|
71 |
+
xx,
|
72 |
+
xx
|
73 |
+
)
|
74 |
+
x = 0
|
75 |
|
|
|
76 |
# Example usage
|
77 |
+
# manager.upload_file("/home/isayahc/projects/Hackathon-Projects/Maker-Tech-Tree/7698996e43bf4aa1ba98f5dd0bf77000.glb", "7698996e43bf4aa1ba98f5dd0bf77000.glb")
|
78 |
+
# manager.download_file('your-file.glb', 'path/to/save/your/file.glb')
|
79 |
+
# manager.delete_file('your-file.glb')
|
|
|
80 |
|
81 |
if __name__ == "__main__":
|
82 |
main()
|
multi_angle_stl.py
CHANGED
@@ -1,39 +1,39 @@
|
|
1 |
from stl import mesh
|
|
|
2 |
from mpl_toolkits import mplot3d
|
3 |
-
from matplotlib import pyplot as plt
|
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 |
-
plt.
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
1 |
from stl import mesh
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
from mpl_toolkits import mplot3d
|
|
|
4 |
|
5 |
+
def load_stl(file_path):
|
6 |
+
"""Load the STL file."""
|
7 |
+
return mesh.Mesh.from_file(file_path)
|
8 |
+
|
9 |
+
def generate_images(mesh, viewing_angles, figsize=(10, 10)):
|
10 |
+
"""Generate images from different viewing angles."""
|
11 |
+
for i, (elev, azim) in enumerate(viewing_angles, start=1):
|
12 |
+
fig = plt.figure(figsize=figsize)
|
13 |
+
ax = fig.add_subplot(111, projection='3d')
|
14 |
+
ax.add_collection3d(mplot3d.art3d.Poly3DCollection(mesh.vectors))
|
15 |
+
max_dim = max(mesh.points.flatten())
|
16 |
+
min_dim = min(mesh.points.flatten())
|
17 |
+
ax.set_xlim([min_dim, max_dim])
|
18 |
+
ax.set_ylim([min_dim, max_dim])
|
19 |
+
ax.set_zlim([min_dim, max_dim])
|
20 |
+
ax.view_init(elev=elev, azim=azim)
|
21 |
+
plt.savefig(f'mesh_{i}.png')
|
22 |
+
plt.close()
|
23 |
+
|
24 |
+
def main():
|
25 |
+
# Load the STL file
|
26 |
+
# stl_file_path = 'sample_data.stl'
|
27 |
+
# your_mesh = load_stl(stl_file_path)
|
28 |
+
|
29 |
+
# Define three different viewing angles
|
30 |
+
viewing_angles = [(30, 45), (60, 90), (45, 135)]
|
31 |
+
|
32 |
+
# Generate images from different viewing angles
|
33 |
+
# generate_images(your_mesh, viewing_angles)
|
34 |
+
|
35 |
+
# Optional: Show the last generated plot
|
36 |
+
# plt.show()
|
37 |
+
|
38 |
+
if __name__ == "__main__":
|
39 |
+
main()
|
utils.py
CHANGED
@@ -73,3 +73,22 @@ def base64_to_file(base64_string: str, output_file_location: str) -> None:
|
|
73 |
binary_data = base64.b64decode(base64_string)
|
74 |
with open(output_file_location, "wb") as output_file:
|
75 |
output_file.write(binary_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
binary_data = base64.b64decode(base64_string)
|
74 |
with open(output_file_location, "wb") as output_file:
|
75 |
output_file.write(binary_data)
|
76 |
+
|
77 |
+
def copy_file_to_location(file_path: str, destination_path: str) -> None:
|
78 |
+
"""
|
79 |
+
Copy a file to a new location with the same name.
|
80 |
+
|
81 |
+
Args:
|
82 |
+
file_path: The path of the file to be copied.
|
83 |
+
destination_path: The path of the destination directory.
|
84 |
+
"""
|
85 |
+
shutil.copy(file_path, destination_path)
|
86 |
+
|
87 |
+
def remove_files(file_paths):
|
88 |
+
"""Remove files given a list of file paths."""
|
89 |
+
for file_path in file_paths:
|
90 |
+
try:
|
91 |
+
os.remove(file_path)
|
92 |
+
print(f"File '{file_path}' removed successfully.")
|
93 |
+
except OSError as e:
|
94 |
+
print(f"Error removing file '{file_path}': {e}")
|