ccm commited on
Commit
1202d82
·
verified ·
1 Parent(s): 830480c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -55
app.py CHANGED
@@ -1,37 +1,3 @@
1
- """
2
- This script sets up a Gradio interface for querying an AI assistant about additive manufacturing research.
3
- It uses a vectorstore to retrieve relevant research excerpts and a language model to generate responses.
4
-
5
- Modules:
6
- - gradio: Interface handling
7
- - spaces: For GPU
8
- - transformers: LLM Loading
9
- - langchain_community.vectorstores: Vectorstore for publications
10
- - langchain_huggingface: Embeddings
11
-
12
- Constants:
13
- - PUBLICATIONS_TO_RETRIEVE: The number of publications to retrieve for the prompt
14
- - RAG_TEMPLATE: The template for the RAG prompt
15
-
16
- Functions:
17
- - preprocess(query: str) -> str: Generates a prompt based on the top k documents matching the query.
18
- - reply(message: str, history: list[str]) -> str: Generates a response to the user’s message.
19
-
20
- Example Queries:
21
- - "What is multi-material 3D printing?"
22
- - "How is additive manufacturing being applied in aerospace?"
23
- - "Tell me about innovations in metal 3D printing techniques."
24
- - "What are some sustainable materials for 3D printing?"
25
- - "What are the biggest challenges with support structures in additive manufacturing?"
26
- - "How is 3D printing impacting the medical field?"
27
- - "What are some common applications of additive manufacturing in industry?"
28
- - "What are the benefits and limitations of using polymers in 3D printing?"
29
- - "Tell me about the environmental impacts of additive manufacturing."
30
- - "What are the primary limitations of current 3D printing technologies?"
31
- - "How are researchers improving the speed of 3D printing processes?"
32
- - "What are the best practices for managing post-processing in additive manufacturing?"
33
- """
34
-
35
  import gradio # Interface handling
36
  import spaces # For GPU
37
  import langchain_community.vectorstores # Vectorstore for publications
@@ -65,14 +31,13 @@ publication_vectorstore = langchain_community.vectorstores.FAISS.load_local(
65
  ),
66
  allow_dangerous_deserialization=True,
67
  )
68
-
69
- # Create the callable LLM
70
- llm = transformers.pipeline(
71
- task="text-generation",
72
- model="Qwen/Qwen2.5-7B-Instruct-AWQ",
73
- device="cuda",
74
- streamer=transformers.TextStreamer(transformers.AutoTokenizer.from_pretrained("Qwen/Qwen2.5-7B-Instruct-AWQ"))
75
- )
76
 
77
 
78
  def preprocess(query: str) -> str:
@@ -99,12 +64,12 @@ def preprocess(query: str) -> str:
99
  research_excerpts="\n\n".join(research_excerpts), query=query
100
  )
101
 
102
- # Print the prompt for debugging purposes
103
- print(prompt)
104
-
105
  return prompt
106
 
107
 
 
 
 
108
  @spaces.GPU
109
  def reply(message: str, history: list[str]) -> str:
110
  """
@@ -118,13 +83,30 @@ def reply(message: str, history: list[str]) -> str:
118
  str: The generated response from the language model.
119
  """
120
 
121
- yield llm(
122
- preprocess(message),
123
- max_new_tokens=512,
124
- return_full_text=False,
125
- )[
126
- 0
127
- ]["generated_text"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
 
129
  # Example Queries for Interface
130
  EXAMPLE_QUERIES = [
@@ -132,14 +114,20 @@ EXAMPLE_QUERIES = [
132
  {"text": "How is additive manufacturing being applied in aerospace?"},
133
  {"text": "Tell me about innovations in metal 3D printing techniques."},
134
  {"text": "What are some sustainable materials for 3D printing?"},
135
- {"text": "What are the biggest challenges with support structures in additive manufacturing?"},
 
 
136
  {"text": "How is 3D printing impacting the medical field?"},
137
- {"text": "What are some common applications of additive manufacturing in industry?"},
 
 
138
  {"text": "What are the benefits and limitations of using polymers in 3D printing?"},
139
  {"text": "Tell me about the environmental impacts of additive manufacturing."},
140
  {"text": "What are the primary limitations of current 3D printing technologies?"},
141
  {"text": "How are researchers improving the speed of 3D printing processes?"},
142
- {"text": "What are the best practices for managing post-processing in additive manufacturing?"}
 
 
143
  ]
144
 
145
  # Run the Gradio Interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio # Interface handling
2
  import spaces # For GPU
3
  import langchain_community.vectorstores # Vectorstore for publications
 
31
  ),
32
  allow_dangerous_deserialization=True,
33
  )
34
+ #
35
+ # # Create the callable LLM
36
+ # llm = transformers.pipeline(
37
+ # task="text-generation",
38
+ # model="Qwen/Qwen2.5-7B-Instruct-AWQ",
39
+ # device="cuda",
40
+ # )
 
41
 
42
 
43
  def preprocess(query: str) -> str:
 
64
  research_excerpts="\n\n".join(research_excerpts), query=query
65
  )
66
 
 
 
 
67
  return prompt
68
 
69
 
70
+ import threading
71
+
72
+
73
  @spaces.GPU
74
  def reply(message: str, history: list[str]) -> str:
75
  """
 
83
  str: The generated response from the language model.
84
  """
85
 
86
+ tok = transformers.AutoTokenizer.from_pretrained("Qwen/Qwen2.5-7B-Instruct-AWQ")
87
+ model = transformers.AutoModelForCausalLM.from_pretrained(
88
+ "Qwen/Qwen2.5-7B-Instruct-AWQ"
89
+ )
90
+ inputs = tok([preprocess(message)], return_tensors="pt")
91
+ streamer = transformers.TextIteratorStreamer(tok)
92
+
93
+ generation_kwargs = dict(inputs, streamer=streamer, max_new_tokens=512)
94
+ thread = threading.Thread(target=model.generate, kwargs=generation_kwargs)
95
+ thread.start()
96
+ generated_text = ""
97
+ for new_text in streamer:
98
+ generated_text += new_text
99
+ yield generated_text
100
+
101
+ # yield llm(
102
+ # preprocess(message),
103
+ # max_new_tokens=512,
104
+ # return_full_text=False,
105
+ # streamer=transformers.TextIteratorStreamer(
106
+ # transformers.AutoTokenizer.from_pretrained("Qwen/Qwen2.5-7B-Instruct-AWQ")
107
+ # ),
108
+ # )[0]["generated_text"]
109
+
110
 
111
  # Example Queries for Interface
112
  EXAMPLE_QUERIES = [
 
114
  {"text": "How is additive manufacturing being applied in aerospace?"},
115
  {"text": "Tell me about innovations in metal 3D printing techniques."},
116
  {"text": "What are some sustainable materials for 3D printing?"},
117
+ {
118
+ "text": "What are the biggest challenges with support structures in additive manufacturing?"
119
+ },
120
  {"text": "How is 3D printing impacting the medical field?"},
121
+ {
122
+ "text": "What are some common applications of additive manufacturing in industry?"
123
+ },
124
  {"text": "What are the benefits and limitations of using polymers in 3D printing?"},
125
  {"text": "Tell me about the environmental impacts of additive manufacturing."},
126
  {"text": "What are the primary limitations of current 3D printing technologies?"},
127
  {"text": "How are researchers improving the speed of 3D printing processes?"},
128
+ {
129
+ "text": "What are the best practices for managing post-processing in additive manufacturing?"
130
+ },
131
  ]
132
 
133
  # Run the Gradio Interface