arjunanand13 commited on
Commit
8868bb5
·
verified ·
1 Parent(s): 102910d

Delete app_30_5_24.py

Browse files
Files changed (1) hide show
  1. app_30_5_24.py +0 -218
app_30_5_24.py DELETED
@@ -1,218 +0,0 @@
1
- import torch
2
- from torch import cuda, bfloat16
3
- from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline, BitsAndBytesConfig, StoppingCriteria, StoppingCriteriaList
4
- from langchain.llms import HuggingFacePipeline
5
- from langchain.vectorstores import FAISS
6
- from langchain.chains import ConversationalRetrievalChain
7
- import gradio as gr
8
- from langchain.embeddings import HuggingFaceEmbeddings
9
- import os
10
-
11
- # Load the Hugging Face token from environment
12
- HF_TOKEN = os.environ.get("HF_TOKEN", None)
13
-
14
- # Define stopping criteria
15
- class StopOnTokens(StoppingCriteria):
16
- def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs) -> bool:
17
- for stop_ids in stop_token_ids:
18
- if torch.eq(input_ids[0][-len(stop_ids):], stop_ids).all():
19
- return True
20
- return False
21
-
22
- # Load the LLaMA model and tokenizer
23
- # model_id = 'meta-llama/Meta-Llama-3-8B-Instruct'
24
- # model_id= "meta-llama/Llama-2-7b-chat-hf"
25
- model_id="mistralai/Mistral-7B-Instruct-v0.2"
26
- device = f'cuda:{cuda.current_device()}' if cuda.is_available() else 'cpu'
27
-
28
- # Set quantization configuration
29
- bnb_config = BitsAndBytesConfig(
30
- load_in_4bit=True,
31
- bnb_4bit_quant_type='nf4',
32
- bnb_4bit_use_double_quant=True,
33
- bnb_4bit_compute_dtype=bfloat16
34
- )
35
-
36
- tokenizer = AutoTokenizer.from_pretrained(model_id, token=HF_TOKEN)
37
- model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", token=HF_TOKEN, quantization_config=bnb_config)
38
-
39
- # Define stopping criteria
40
- stop_list = ['\nHuman:', '\n```\n']
41
- stop_token_ids = [tokenizer(x)['input_ids'] for x in stop_list]
42
- stop_token_ids = [torch.LongTensor(x).to(device) for x in stop_token_ids]
43
- stopping_criteria = StoppingCriteriaList([StopOnTokens()])
44
-
45
- # Create text generation pipeline
46
- generate_text = pipeline(
47
- model=model,
48
- tokenizer=tokenizer,
49
- return_full_text=True,
50
- task='text-generation',
51
- # stopping_criteria=stopping_criteria,
52
- temperature=0.1,
53
- max_new_tokens=2048,
54
- # repetition_penalty=1.1
55
- )
56
-
57
- llm = HuggingFacePipeline(pipeline=generate_text)
58
-
59
- # Load the stored FAISS index
60
- try:
61
- vectorstore = FAISS.load_local('faiss_index', HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2", model_kwargs={"device": "cuda"}))
62
- print("Loaded embedding successfully")
63
- except ImportError as e:
64
- print("FAISS could not be imported. Make sure FAISS is installed correctly.")
65
- raise e
66
-
67
- # Set up the Conversational Retrieval Chain
68
- chain = ConversationalRetrievalChain.from_llm(llm, vectorstore.as_retriever(), return_source_documents=True)
69
-
70
- chat_history = []
71
-
72
- def format_prompt(query):
73
- prompt=f"""
74
- You are a knowledgeable assistant with access to a comprehensive database.
75
- I need you to answer my question and provide related information in a specific format.
76
- I have provided four relatable json files , choose the most suitable chunks for answering the query
77
- Here's what I need:
78
- Include a final answer without additional comments, sign-offs, or extra phrases. Be direct and to the point.
79
-
80
- Here's my question:
81
- {query}
82
-
83
- Solution==>
84
-
85
- Example1
86
- Query: "How to use IPU1_0 instead of A15_0 to process NDK in TDA2x-EVM",
87
- Solution: "To use IPU1_0 instead of A15_0 to process NDK in TDA2x-EVM, you need to modify the configuration file of the NDK application. Specifically, change the processor reference from 'A15_0' to 'IPU1_0'.",
88
-
89
- Example2
90
- Query: "Can BQ25896 support I2C interface?",
91
- Solution: "Yes, the BQ25896 charger supports the I2C interface for communication.",
92
- """
93
- # The format I want answer in
94
- # user_query ==> query
95
- # response ==>
96
- # """
97
- # prompt = f"""
98
- # You are a knowledgeable assistant with access to a comprehensive database.
99
- # I need you to answer my question and provide related information in a specific format.
100
- # Here's what I need:
101
- # A brief, general response to my question based on related answers retrieved.
102
- # Include a brief final answer without additional comments, sign-offs, or extra phrases. Be direct and to the point.
103
-
104
- # A JSON-formatted output containing: ALL SOURCE DOCUMENTS
105
- # - "question": The ticketName
106
- # - "answer": The Responses
107
- # Here's my question:
108
- # {query}
109
- # """
110
-
111
- # - "related_questions": A list of related questions and their answers, each as a dictionary with the keys. Consider all source documents:
112
- # - "question": The related question.
113
- # - "answer": The related answer.
114
-
115
-
116
-
117
- # Example 1:
118
- # {{
119
- # "question": "How to use IPU1_0 instead of A15_0 to process NDK in TDA2x-EVM",
120
- # "answer": "To use IPU1_0 instead of A15_0 to process NDK in TDA2x-EVM, you need to modify the configuration file of the NDK application. Specifically, change the processor reference from 'A15_0' to 'IPU1_0'.",
121
- # "related_questions": [
122
- # {{
123
- # "question": "Can you provide MLBP documentation on TDA2?",
124
- # "answer": "MLB is documented for DRA devices in the TRM book, chapter 24.12."
125
- # }},
126
- # {{
127
- # "question": "Hi, could you share me the TDA2x documents about Security(SPRUHS7) and Cryptographic(SPRUHS8) addendums?",
128
- # "answer": "Most of TDA2 documents are on ti.com under the product folder."
129
- # }},
130
- # {{
131
- # "question": "Is any one can provide us a way to access CDDS for nessary docs?",
132
- # "answer": "Which document are you looking for?"
133
- # }},
134
- # {{
135
- # "question": "What can you tell me about the TDA2 and TDA3 processors? Can they / do they run Linux?",
136
- # "answer": "We have moved your post to the appropriate forum."
137
- # }}
138
- # ]
139
- # }}
140
-
141
- # Final Answer: To use IPU1_0 instead of A15_0 to process NDK in TDA2x-EVM, you need to modify the configuration file of the NDK application. Specifically, change the processor reference from 'A15_0' to 'IPU1_0'.
142
-
143
- # Example 2:
144
- # {{
145
- # "question": "Can BQ25896 support I2C interface?",
146
- # "answer": "Yes, the BQ25896 charger supports the I2C interface for communication.",
147
- # "related_questions": [
148
- # {{
149
- # "question": "What are the main features of BQ25896?",
150
- # "answer": "The BQ25896 features include high-efficiency, fast charging capability, and a wide input voltage range."
151
- # }},
152
- # {{
153
- # "question": "How to configure the BQ25896 for USB charging?",
154
- # "answer": "To configure the BQ25896 for USB charging, set the input current limit and the charging current via I2C registers."
155
- # }}
156
- # ]
157
- # }}
158
-
159
- # Final Answer: Yes, the BQ25896 charger supports the I2C interface for communication.
160
-
161
- # """
162
-
163
-
164
- return prompt
165
-
166
-
167
- def qa_infer(query):
168
- content = ""
169
- formatted_prompt = format_prompt(query)
170
- result = chain({"question": formatted_prompt, "chat_history": chat_history})
171
- for doc in result['source_documents']:
172
- content += "-" * 50 + "\n"
173
- content += doc.page_content + "\n"
174
- print(content)
175
- print("#" * 100)
176
- print(result['answer'])
177
- # return content , result['answer']
178
-
179
-
180
- # Save the output to a file
181
- output_file = "output.txt"
182
- with open(output_file, "w") as f:
183
- f.write("Query:\n")
184
- f.write(query + "\n\n")
185
- f.write("Answer:\n")
186
- f.write(result['answer'] + "\n\n")
187
- f.write("Source Documents:\n")
188
- f.write(content + "\n")
189
-
190
- # Return the content and answer along with the download link
191
- download_link = f'<a href="file/{output_file}" download>Download Output File</a>'
192
- return result['answer'],content, download_link
193
-
194
- css_code = """
195
- .gradio-container {
196
- background-color: #daccdb;
197
- }
198
-
199
- /* Button styling for all buttons */
200
- button {
201
- background-color: #927fc7; /* Default color for all other buttons */
202
- color: black;
203
- border: 1px solid black;
204
- padding: 10px;
205
- margin-right: 10px;
206
- font-size: 16px; /* Increase font size */
207
- font-weight: bold; /* Make text bold */
208
- }
209
-
210
-
211
- """
212
- EXAMPLES = ["TDA4 product planning and datasheet release progress? ",
213
- "I'm using Code Composer Studio 5.4.0.00091 and enabled FPv4SPD16 floating point support for CortexM4 in TDA2. However, after building the project, the .asm file shows --float_support=vfplib instead of FPv4SPD16. Why is this happening?",
214
- "Master core in TDA2XX is a15 and in TDA3XX it is m4,so we have to shift all modules that are being used by a15 in TDA2XX to m4 in TDA3xx."]
215
-
216
- demo = gr.Interface(fn=qa_infer, inputs=[gr.Textbox(label="QUERY", placeholder ="Enter your query here")], allow_flagging='never', examples=EXAMPLES, cache_examples=False, outputs=[gr.Textbox(label="SOLUTION"), gr.Textbox(label="RELATED QUERIES"), gr.HTML()], css=css_code)#,outputs="text")
217
- demo.launch()
218
-