batlahiya commited on
Commit
500478b
·
verified ·
1 Parent(s): 2fb08d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -171
app.py CHANGED
@@ -1,171 +1,3 @@
1
- # import spaces
2
- # import gradio as gr
3
- # import os
4
- # import re
5
- # from pathlib import Path
6
- # from unidecode import unidecode
7
-
8
- # from langchain_community.document_loaders import PyPDFLoader
9
- # from langchain.text_splitter import RecursiveCharacterTextSplitter
10
- # from langchain_community.vectorstores import Chroma
11
- # from langchain.chains import ConversationalRetrievalChain
12
- # from langchain.memory import ConversationBufferMemory
13
-
14
- # from langchain_huggingface import HuggingFaceEmbeddings, HuggingFacePipeline
15
- # from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
16
-
17
- # import chromadb
18
- # import torch
19
- # from concurrent.futures import ThreadPoolExecutor
20
-
21
- # # Environment configuration
22
- # os.environ["TOKENIZERS_PARALLELISM"] = "false"
23
-
24
- # # Predefined values
25
- # predefined_pdf = "t6.pdf"
26
- # predefined_llm = "TinyLlama/TinyLlama-1.1B-Chat-v1.0" # Use a smaller model for faster responses
27
-
28
- # def load_doc(list_file_path, chunk_size, chunk_overlap):
29
- # loaders = [PyPDFLoader(x) for x in list_file_path]
30
- # pages = []
31
- # for loader in loaders:
32
- # pages.extend(loader.load())
33
- # text_splitter = RecursiveCharacterTextSplitter(
34
- # chunk_size=chunk_size,
35
- # chunk_overlap=chunk_overlap)
36
- # doc_splits = text_splitter.split_documents(pages)
37
- # return doc_splits
38
-
39
- # def create_db(splits, collection_name):
40
- # embedding = HuggingFaceEmbeddings()
41
- # new_client = chromadb.EphemeralClient()
42
- # vectordb = Chroma.from_documents(
43
- # documents=splits,
44
- # embedding=embedding,
45
- # client=new_client,
46
- # collection_name=collection_name,
47
- # )
48
- # return vectordb
49
-
50
- # def load_db():
51
- # embedding = HuggingFaceEmbeddings()
52
- # vectordb = Chroma(
53
- # embedding_function=embedding)
54
- # return vectordb
55
-
56
- # def create_collection_name(filepath):
57
- # collection_name = Path(filepath).stem
58
- # collection_name = collection_name.replace(" ", "-")
59
- # collection_name = unidecode(collection_name)
60
- # collection_name = re.sub('[^A-Za-z0-9]+', '-', collection_name)
61
- # collection_name = collection_name[:50]
62
- # if len(collection_name) < 3:
63
- # collection_name = collection_name + 'xyz'
64
- # if not collection_name[0].isalnum():
65
- # collection_name = 'A' + collection_name[1:]
66
- # if not collection_name[-1].isalnum():
67
- # collection_name = collection_name[:-1] + 'Z'
68
- # print('Filepath: ', filepath)
69
- # print('Collection name: ', collection_name)
70
- # return collection_name
71
-
72
- # def initialize_llmchain(llm_model, temperature, max_tokens, top_k, vector_db):
73
- # if not torch.cuda.is_available():
74
- # print("CUDA is not available. This demo does not work on CPU.")
75
- # return None
76
-
77
- # def init_llm():
78
- # print("Initializing HF model and tokenizer...")
79
- # model = AutoModelForCausalLM.from_pretrained(llm_model, device_map="auto", load_in_4bit=True)
80
- # tokenizer = AutoTokenizer.from_pretrained(llm_model)
81
- # tokenizer.use_default_system_prompt = False
82
-
83
- # print("Initializing HF pipeline...")
84
- # hf_pipeline = pipeline(
85
- # "text-generation",
86
- # model=model,
87
- # tokenizer=tokenizer,
88
- # device_map='auto',
89
- # max_new_tokens=max_tokens,
90
- # do_sample=True,
91
- # top_k=top_k,
92
- # num_return_sequences=1,
93
- # eos_token_id=tokenizer.eos_token_id
94
- # )
95
- # llm = HuggingFacePipeline(pipeline=hf_pipeline, model_kwargs={'temperature': temperature})
96
-
97
- # print("Defining buffer memory...")
98
- # memory = ConversationBufferMemory(
99
- # memory_key="chat_history",
100
- # output_key='answer',
101
- # return_messages=True
102
- # )
103
- # retriever = vector_db.as_retriever()
104
-
105
- # print("Defining retrieval chain...")
106
- # qa_chain = ConversationalRetrievalChain.from_llm(
107
- # llm,
108
- # retriever=retriever,
109
- # chain_type="stuff",
110
- # memory=memory,
111
- # return_source_documents=True,
112
- # verbose=False,
113
- # )
114
- # return qa_chain
115
-
116
- # with ThreadPoolExecutor() as executor:
117
- # future = executor.submit(init_llm)
118
- # qa_chain = future.result()
119
-
120
- # print("Initialization complete!")
121
- # return qa_chain
122
-
123
- # # Define the conversation function
124
- # @spaces.GPU(duration=60)
125
- # def chat(message):
126
- # global qa_chain
127
- # prompt_template = "Instruction: You are an expert landlside assistant. Please provide a well written detailed and helpful answer to the following user query only from the given references.User Query:\n"
128
- # full_input = prompt_template + message
129
- # response = qa_chain({"question": full_input})
130
- # full_answer = response["answer"]
131
- # answer_parts = full_answer.split("Helpful Answer:")
132
- # qa_chain.memory.clear()
133
- # if len(answer_parts) > 1:
134
- # main_answer = answer_parts[-1].strip() # Extracting the main answer
135
- # references = answer_parts[0].strip() # Keeping the references
136
- # answer = f"Helpful Answer: {main_answer}\n\nReferences:\n{references}"
137
- # else:
138
- # answer = full_answer # In case there is no "Helpful Answer" part
139
- # return answer, full_answer
140
-
141
- # interface = gr.Interface(
142
- # fn=chat,
143
- # inputs="textbox", # Use a single input textbox
144
- # outputs=["textbox", "textbox"], # Two output fields: one for the main answer, one for other outputs
145
- # title="LANDSLIDE AWARENESS CHATBOT",
146
- # description="Ask me anything related to landlsides!",
147
- # elem_id="my-interface",
148
- # )
149
-
150
-
151
-
152
- # # Load the PDF document and create the vector database (replace with your logic)
153
- # pdf_filepath = predefined_pdf
154
- # doc_splits = load_doc([pdf_filepath], chunk_size=400, chunk_overlap=40)
155
- # collection_name = create_collection_name(pdf_filepath)
156
- # vector_db = create_db(doc_splits, collection_name)
157
-
158
- # # Initialize the LLM chain with threading
159
- # qa_chain = initialize_llmchain(predefined_llm, temperature=0.7, max_tokens=512, top_k=5, vector_db=vector_db)
160
-
161
- # # Check if qa_chain is properly initialized
162
- # if qa_chain is None:
163
- # print("Failed to initialize the QA chain. Please check the CUDA availability and model paths.")
164
- # else:
165
- # # Launch the Gradio interface with share option
166
- # interface.launch(share=True)
167
-
168
-
169
  import spaces
170
  import gradio as gr
171
  import os
@@ -292,7 +124,7 @@ def initialize_llmchain(llm_model, temperature, max_tokens, top_k, vector_db):
292
  @spaces.GPU(duration=60)
293
  def chat(message):
294
  global qa_chain
295
- prompt_template = "Instruction: You are an expert landslide assistant. Please provide a well written detailed and helpful answer to the following user query only from the given references.User Query:\n"
296
  full_input = prompt_template + message
297
  response = qa_chain({"question": full_input})
298
  full_answer = response["answer"]
@@ -309,12 +141,14 @@ def chat(message):
309
  interface = gr.Interface(
310
  fn=chat,
311
  inputs="textbox", # Use a single input textbox
312
- outputs=["chatbot", "markdown"], # Use chatbot for chat interface and markdown for full response
313
  title="LANDSLIDE AWARENESS CHATBOT",
314
- description="Ask me anything related to landslides!",
315
  elem_id="my-interface",
316
  )
317
 
 
 
318
  # Load the PDF document and create the vector database (replace with your logic)
319
  pdf_filepath = predefined_pdf
320
  doc_splits = load_doc([pdf_filepath], chunk_size=400, chunk_overlap=40)
@@ -330,3 +164,5 @@ if qa_chain is None:
330
  else:
331
  # Launch the Gradio interface with share option
332
  interface.launch(share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import spaces
2
  import gradio as gr
3
  import os
 
124
  @spaces.GPU(duration=60)
125
  def chat(message):
126
  global qa_chain
127
+ prompt_template = "Instruction: You are an expert landlside assistant. Please provide a well written detailed and helpful answer to the following user query only from the given references.User Query:\n"
128
  full_input = prompt_template + message
129
  response = qa_chain({"question": full_input})
130
  full_answer = response["answer"]
 
141
  interface = gr.Interface(
142
  fn=chat,
143
  inputs="textbox", # Use a single input textbox
144
+ outputs=["textbox", "textbox"], # Two output fields: one for the main answer, one for other outputs
145
  title="LANDSLIDE AWARENESS CHATBOT",
146
+ description="Ask me anything related to landlsides!",
147
  elem_id="my-interface",
148
  )
149
 
150
+
151
+
152
  # Load the PDF document and create the vector database (replace with your logic)
153
  pdf_filepath = predefined_pdf
154
  doc_splits = load_doc([pdf_filepath], chunk_size=400, chunk_overlap=40)
 
164
  else:
165
  # Launch the Gradio interface with share option
166
  interface.launch(share=True)
167
+
168
+