isayahc commited on
Commit
8bbb154
1 Parent(s): 747bb1c
Files changed (4) hide show
  1. langhchain_generate_components.py +48 -38
  2. main.py +61 -7
  3. utils.py +33 -2
  4. weaviate_utils.py +57 -52
langhchain_generate_components.py CHANGED
@@ -38,29 +38,42 @@ os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
38
 
39
 
40
  # The scheme for creating experiments
41
- experiment_schema = [
42
- ResponseSchema(name="Material", description="list of materials need to perfrom the experiments please be specific", type="list"),
43
- ]
44
 
45
 
46
- maker_schema = [
47
  ResponseSchema(name="Material", description="The base components needed to create this items from scratch DIY This item must be exact and not an estimation", type="list"),
 
48
  ]
49
 
50
- experiment_output_parser = StructuredOutputParser.from_response_schemas(experiment_schema)
51
- maker_output_parser = StructuredOutputParser.from_response_schemas(maker_schema)
52
-
53
  memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
 
54
 
55
- format_instructions = experiment_output_parser.get_format_instructions()
56
 
 
 
57
 
58
- experiment_prompt = PromptTemplate(
59
- template="You must generate well detailed science experiments.\n{format_instructions}\n{question}\n{context}",
60
- input_variables=["question"],
61
- partial_variables={"format_instructions": format_instructions},
62
- memory = memory
63
- )
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
  maker_prompt = PromptTemplate(
66
  template="You must generate a well detailed list of items for creating a given item from scratch. \
@@ -87,7 +100,6 @@ def format_docs(docs):
87
  return "\n\n".join([join_strings(d.page_content, d.metadata['Entry ID'],d.metadata['Title'], ) for d in docs])
88
 
89
 
90
- arxiv_retriever = ArxivRetriever(load_max_docs=2)
91
 
92
  # model = ChatOpenAI(temperature=0)
93
  model = ChatOpenAI(temperature=0,model="gpt-4")
@@ -99,37 +111,35 @@ pub_med_retriever = PubMedRetriever()
99
 
100
  wikipedia_retriever = WikipediaRetriever()
101
 
102
- arxiv_chain = (
103
- {"context": arxiv_retriever, "question": RunnablePassthrough()}
104
- | experiment_prompt
105
- | model
106
- | experiment_output_parser
107
- )
108
-
109
- pub_med_chain = (
110
- {"context": pub_med_retriever, "question": RunnablePassthrough()}
111
- | experiment_prompt
112
- | model
113
- | experiment_output_parser
114
- )
115
-
116
- wikipedia_chain = (
117
- {"context": wikipedia_retriever, "question": RunnablePassthrough()}
118
- | experiment_prompt
119
- | model
120
- | experiment_output_parser
121
- )
122
 
123
  maker_wikipedia_chain = (
124
  {"context": wikipedia_retriever, "question": RunnablePassthrough()}
125
  | maker_prompt
126
  | model
127
- | maker_output_parser
128
  )
129
 
130
 
131
-
132
-
133
  if __name__ == "__main__":
134
 
135
 
 
38
 
39
 
40
  # The scheme for creating experiments
41
+ # experiment_schema = [
42
+ # ResponseSchema(name="Material", description="list of materials need to perfrom the experiments please be specific", type="list"),
43
+ # ]
44
 
45
 
46
+ response_schemas = [
47
  ResponseSchema(name="Material", description="The base components needed to create this items from scratch DIY This item must be exact and not an estimation", type="list"),
48
+ ResponseSchema(name="Feild Of Study", description="List the field of study this can be used for", type="list"),
49
  ]
50
 
51
+ output_parser = StructuredOutputParser.from_response_schemas(response_schemas)
 
 
52
  memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
53
+ format_instructions = output_parser.get_format_instructions()
54
 
 
55
 
56
+ # experiment_output_parser = StructuredOutputParser.from_response_schemas(experiment_schema)
57
+ # maker_output_parser = StructuredOutputParser.from_response_schemas(maker_schema)
58
 
59
+ memory = ConversationBufferMemory(
60
+ memory_key="chat_history",
61
+ return_messages=True,
62
+ )
63
+
64
+ # format_instructions = experiment_output_parser.get_format_instructions()
65
+ # maker_format_instructions = maker_output_parser.get_format_instructions()
66
+
67
+ # output_parser = StructuredOutputParser.from_response_schemas(maker_schema)
68
+
69
+ format_instructions = output_parser.get_format_instructions()
70
+
71
+ # experiment_prompt = PromptTemplate(
72
+ # template="You must generate well detailed science experiments.\n{format_instructions}\n{question}\n{context}",
73
+ # input_variables=["question"],
74
+ # partial_variables={"format_instructions": format_instructions},
75
+ # memory = memory
76
+ # )
77
 
78
  maker_prompt = PromptTemplate(
79
  template="You must generate a well detailed list of items for creating a given item from scratch. \
 
100
  return "\n\n".join([join_strings(d.page_content, d.metadata['Entry ID'],d.metadata['Title'], ) for d in docs])
101
 
102
 
 
103
 
104
  # model = ChatOpenAI(temperature=0)
105
  model = ChatOpenAI(temperature=0,model="gpt-4")
 
111
 
112
  wikipedia_retriever = WikipediaRetriever()
113
 
114
+ # arxiv_chain = (
115
+ # {"context": arxiv_retriever, "question": RunnablePassthrough()}
116
+ # | experiment_prompt
117
+ # | model
118
+ # | experiment_output_parser
119
+ # )
120
+
121
+ # pub_med_chain = (
122
+ # {"context": pub_med_retriever, "question": RunnablePassthrough()}
123
+ # | experiment_prompt
124
+ # | model
125
+ # | experiment_output_parser
126
+ # )
127
+
128
+ # wikipedia_chain = (
129
+ # {"context": wikipedia_retriever, "question": RunnablePassthrough()}
130
+ # | experiment_prompt
131
+ # | model
132
+ # | experiment_output_parser
133
+ # )
134
 
135
  maker_wikipedia_chain = (
136
  {"context": wikipedia_retriever, "question": RunnablePassthrough()}
137
  | maker_prompt
138
  | model
139
+ | output_parser
140
  )
141
 
142
 
 
 
143
  if __name__ == "__main__":
144
 
145
 
main.py CHANGED
@@ -1,23 +1,40 @@
1
- from langhchain_generate_components import maker_wikipedia_chain
2
  from utils import (
3
  save_file, convert_obj_to_stl,
4
- change_file_extension,
5
  )
6
  from mesh_utils import generate_mesh_images
7
  from gradio_client import Client
 
 
 
 
 
 
 
 
 
 
8
 
 
 
 
9
 
10
  def main():
11
  # the object to be generated
12
  query = "A Microscope"
13
 
14
  # using a retriever we generat a list of Components
15
- output = maker_wikipedia_chain.invoke(query)
16
 
17
  # the first item
18
  shap_e_sample = output['Material'][0]
 
 
 
19
 
20
  client = Client("hysts/Shap-E")
 
21
  result = client.predict(
22
  shap_e_sample, # str in 'Prompt' Textbox component
23
  1621396601, # float (numeric value between 0 and 2147483647) in 'Seed' Slider component
@@ -26,6 +43,25 @@ def main():
26
  api_name="/text-to-3d"
27
  )
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  saved_file_name = "sample.glb"
30
  # save to local machine
31
  save_file(result,saved_file_name)
@@ -46,10 +82,28 @@ def main():
46
 
47
  viewing_angles = [(30, 45), (60, 90), (45, 135)]
48
 
49
- generate_mesh_images(
50
- stl_file_location,
51
- viewing_angles
52
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
  # These screenshots need to be given to GPT-V
55
  # for feedback
 
1
+ # from langhchain_generate_components import maker_wikipedia_chain
2
  from utils import (
3
  save_file, convert_obj_to_stl,
4
+ change_file_extension, file_to_base64,
5
  )
6
  from mesh_utils import generate_mesh_images
7
  from gradio_client import Client
8
+ from weaviate_utils import init_client
9
+ from datetime import datetime
10
+ from structured__apparatus_chain import (
11
+ wikipedia_chain
12
+ )
13
+ from datetime import datetime, timezone
14
+ from dotenv import load_dotenv
15
+ import os
16
+
17
+ load_dotenv()
18
 
19
+ HF_API_KEY = os.getenv("HUGGINGFACE_API_KEY")
20
+ OPENAI_APIKEY = os.getenv("OPENAI_API_KEY")
21
+ OPENAI_APIKEY = os.getenv("OPENAI_APIKEY")
22
 
23
  def main():
24
  # the object to be generated
25
  query = "A Microscope"
26
 
27
  # using a retriever we generat a list of Components
28
+ output = wikipedia_chain.invoke(query)
29
 
30
  # the first item
31
  shap_e_sample = output['Material'][0]
32
+ shap_e_list = output['Fields_of_study']
33
+
34
+
35
 
36
  client = Client("hysts/Shap-E")
37
+ client.hf_token = os.getenv("HUGGINGFACE_API_KEY")
38
  result = client.predict(
39
  shap_e_sample, # str in 'Prompt' Textbox component
40
  1621396601, # float (numeric value between 0 and 2147483647) in 'Seed' Slider component
 
43
  api_name="/text-to-3d"
44
  )
45
 
46
+ weaviate_client = init_client()
47
+ component_collection = weaviate_client.collections.get("Component")
48
+ component_image_collection = weaviate_client.collections.get("ComponentImage")
49
+
50
+ base_64_result = file_to_base64(result)
51
+
52
+ uuid = component_collection.data.insert({
53
+ "DateCreated" : datetime.now(timezone.utc),
54
+ "UsedInComps" : [query],
55
+ "ToolName" : shap_e_sample,
56
+ "Tags" : shap_e_list,
57
+ "feildsOfStudy" : shap_e_list,
58
+ # "GlbBlob" : base_64_result,
59
+ })
60
+
61
+
62
+
63
+
64
+
65
  saved_file_name = "sample.glb"
66
  # save to local machine
67
  save_file(result,saved_file_name)
 
82
 
83
  viewing_angles = [(30, 45), (60, 90), (45, 135)]
84
 
85
+ # generate_mesh_images(
86
+ # stl_file_location,
87
+ # viewing_angles
88
+ # )
89
+
90
+ data_location = generate_mesh_images(
91
+ stl_file_location,
92
+ viewing_angles,
93
+ "data",
94
+ )
95
+
96
+ for item1, item2 in zip(data_location, viewing_angles):
97
+
98
+ base_64_result = file_to_base64(item1)
99
+
100
+ image_uuid = component_image_collection.data.insert({
101
+ "DateCreated" : datetime.now(timezone.utc),
102
+ "ImageAngle" : [str(i) for i in item2],
103
+ "BelongsToComponent" : uuid,
104
+ })
105
+
106
+
107
 
108
  # These screenshots need to be given to GPT-V
109
  # for feedback
utils.py CHANGED
@@ -1,5 +1,6 @@
1
- import shutil
2
  import trimesh
 
 
3
  import os
4
 
5
  def save_file(input_file, output_file):
@@ -41,4 +42,34 @@ def change_file_extension(file_path: str, new_extension: str) -> str:
41
  """
42
  base_path, _ = os.path.splitext(file_path)
43
  new_file_path = base_path + '.' + new_extension
44
- return new_file_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import trimesh
2
+ import shutil
3
+ import base64
4
  import os
5
 
6
  def save_file(input_file, output_file):
 
42
  """
43
  base_path, _ = os.path.splitext(file_path)
44
  new_file_path = base_path + '.' + new_extension
45
+ return new_file_path
46
+
47
+
48
+ def file_to_base64(file_location: str) -> str:
49
+ """
50
+ This function is to convert files into base64
51
+ ## Input
52
+ file_location (str) : the location of the file on the machine
53
+
54
+ ## Output
55
+ the base64 encoding of the file
56
+ """
57
+ with open(file_location, "rb") as file:
58
+ file_content = file.read()
59
+ base64_encoded = base64.b64encode(file_content)
60
+ return base64_encoded.decode("utf-8")
61
+
62
+ def base64_to_file(base64_string: str, output_file_location: str) -> None:
63
+ """
64
+ Decodes a base64-encoded string and writes the resulting binary data to a file.
65
+
66
+ Args:
67
+ base64_string (str): The base64-encoded string to decode.
68
+ output_file_location (str): The file path where the decoded binary data will be written.
69
+
70
+ Returns:
71
+ None
72
+ """
73
+ binary_data = base64.b64decode(base64_string)
74
+ with open(output_file_location, "wb") as output_file:
75
+ output_file.write(binary_data)
weaviate_utils.py CHANGED
@@ -18,66 +18,71 @@ def init_client():
18
  auth_credentials=weaviate.auth.AuthApiKey(
19
  os.getenv("YOUR_WCS_AUTH_KEY")
20
  ), # Set this environment variable
 
 
 
 
 
21
  )
22
  return client
23
 
24
 
25
 
26
  x = 0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
- try:
29
- client = init_client()
30
- client.collections.create(
31
- name="Component",
32
- description="Component of a given Apparatus",
33
- vectorizer_config=wvc.config.Configure.Vectorizer.text2vec_openai(),
34
- properties=[
35
- # wvc.config.Property(name="id", data_type=wvc.config.DataType.UUID),
36
- wvc.config.Property(name="DateCreated", data_type=wvc.config.DataType.DATE),
37
- wvc.config.Property(name="UsedInComps", data_type=wvc.config.DataType.TEXT_ARRAY),
38
- wvc.config.Property(name="FeildsOfStudy", data_type=wvc.config.DataType.TEXT_ARRAY),
39
- wvc.config.Property(name="ToolName", data_type=wvc.config.DataType.TEXT),
40
- wvc.config.Property(name="Tags", data_type=wvc.config.DataType.TEXT_ARRAY),
41
- wvc.config.Property(name="GlbBlob", data_type=wvc.config.DataType.BLOB),
42
- ]
43
- )
44
- finally:
45
- client.close()
46
-
47
- try:
48
- client = init_client()
49
- client.collections.create(
50
- name="ScienceEperiment",
51
- description="Science Experiment with the goal of making something",
52
- vectorizer_config=wvc.config.Configure.Vectorizer.text2vec_openai(),
53
- properties=[
54
- # wvc.config.Property(name="id", data_type=wvc.config.DataType.UUID),
55
- wvc.config.Property(name="DateCreated", data_type=wvc.config.DataType.DATE),
56
- wvc.config.Property(name="UsedInComps", data_type=wvc.config.DataType.TEXT_ARRAY),
57
- wvc.config.Property(name="FeildsOfStudy", data_type=wvc.config.DataType.TEXT_ARRAY),
58
- wvc.config.Property(name="ToolName", data_type=wvc.config.DataType.TEXT),
59
- wvc.config.Property(name="Tags", data_type=wvc.config.DataType.TEXT_ARRAY),
60
- ]
61
- )
62
- finally:
63
- client.close()
64
 
65
- try:
66
- client = init_client()
67
- client.collections.create(
68
- name="ComponentImage",
69
- description="An image to gain visual context on a component",
70
- vectorizer_config=wvc.config.Configure.Vectorizer.text2vec_openai(),
71
- properties=[
72
- # wvc.config.Property(name="id", data_type=wvc.config.DataType.UUID),
73
- wvc.config.Property(name="DateCreated", data_type=wvc.config.DataType.DATE),
74
- wvc.config.Property(name="ImageContent", data_type=wvc.config.DataType.BLOB),
75
- wvc.config.Property(name="ImageAngle", data_type=wvc.config.DataType.TEXT_ARRAY),
76
- wvc.config.Property(name="BelongsToComponent", data_type=wvc.config.DataType.UUID),
77
- ]
78
- )
79
- finally:
80
- client.close()
81
 
82
 
83
 
 
18
  auth_credentials=weaviate.auth.AuthApiKey(
19
  os.getenv("YOUR_WCS_AUTH_KEY")
20
  ), # Set this environment variable
21
+ headers={
22
+ "X-OpenAI-Api-Key": os.environ[
23
+ "OPENAI_API_KEY"
24
+ ] # Replace with your inference API key
25
+ },
26
  )
27
  return client
28
 
29
 
30
 
31
  x = 0
32
+ def main() :
33
+ try:
34
+ client = init_client()
35
+ client.collections.create(
36
+ name="Component",
37
+ description="Component of a given Apparatus",
38
+ vectorizer_config=wvc.config.Configure.Vectorizer.text2vec_openai(),
39
+ properties=[
40
+ # wvc.config.Property(name="id", data_type=wvc.config.DataType.UUID),
41
+ wvc.config.Property(name="DateCreated", data_type=wvc.config.DataType.DATE),
42
+ wvc.config.Property(name="UsedInComps", data_type=wvc.config.DataType.TEXT_ARRAY),
43
+ wvc.config.Property(name="FeildsOfStudy", data_type=wvc.config.DataType.TEXT_ARRAY),
44
+ wvc.config.Property(name="ToolName", data_type=wvc.config.DataType.TEXT),
45
+ wvc.config.Property(name="Tags", data_type=wvc.config.DataType.TEXT_ARRAY),
46
+ wvc.config.Property(name="GlbBlob", data_type=wvc.config.DataType.BLOB),
47
+ ]
48
+ )
49
+ finally:
50
+ client.close()
51
 
52
+ try:
53
+ client = init_client()
54
+ client.collections.create(
55
+ name="ScienceEperiment",
56
+ description="Science Experiment with the goal of making something",
57
+ vectorizer_config=wvc.config.Configure.Vectorizer.text2vec_openai(),
58
+ properties=[
59
+ # wvc.config.Property(name="id", data_type=wvc.config.DataType.UUID),
60
+ wvc.config.Property(name="DateCreated", data_type=wvc.config.DataType.DATE),
61
+ wvc.config.Property(name="UsedInComps", data_type=wvc.config.DataType.TEXT_ARRAY),
62
+ wvc.config.Property(name="FeildsOfStudy", data_type=wvc.config.DataType.TEXT_ARRAY),
63
+ wvc.config.Property(name="ToolName", data_type=wvc.config.DataType.TEXT),
64
+ wvc.config.Property(name="Tags", data_type=wvc.config.DataType.TEXT_ARRAY),
65
+ ]
66
+ )
67
+ finally:
68
+ client.close()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
+ try:
71
+ client = init_client()
72
+ client.collections.create(
73
+ name="ComponentImage",
74
+ description="An image to gain visual context on a component",
75
+ vectorizer_config=wvc.config.Configure.Vectorizer.text2vec_openai(),
76
+ properties=[
77
+ # wvc.config.Property(name="id", data_type=wvc.config.DataType.UUID),
78
+ wvc.config.Property(name="DateCreated", data_type=wvc.config.DataType.DATE),
79
+ wvc.config.Property(name="ImageContent", data_type=wvc.config.DataType.BLOB),
80
+ wvc.config.Property(name="ImageAngle", data_type=wvc.config.DataType.TEXT_ARRAY),
81
+ wvc.config.Property(name="BelongsToComponent", data_type=wvc.config.DataType.UUID),
82
+ ]
83
+ )
84
+ finally:
85
+ client.close()
86
 
87
 
88