Pavan178 commited on
Commit
4b3c2a2
·
verified ·
1 Parent(s): 622c776

Update gradio_app.py

Browse files
Files changed (1) hide show
  1. gradio_app.py +55 -68
gradio_app.py CHANGED
@@ -6,29 +6,17 @@ from llama_index.embeddings import HuggingFaceEmbedding
6
  from llama_index import ServiceContext
7
  from dotenv import load_dotenv
8
  import os
 
9
  import tempfile
10
  from pathlib import Path
11
- import logging
12
-
13
- # Set up logging
14
- logging.basicConfig(level=logging.INFO)
15
- logger = logging.getLogger(__name__)
16
 
17
  # Load environment variables
18
  load_dotenv()
19
 
20
- # Define the directory for persistent storage and data
21
- PERSIST_DIR = "./db"
22
- DATA_DIR = "data"
23
-
24
- # Ensure data directory exists
25
- os.makedirs(DATA_DIR, exist_ok=True)
26
- os.makedirs(PERSIST_DIR, exist_ok=True)
27
-
28
  # Configure the Llama index settings
29
  llm = HuggingFaceInferenceAPI(
30
- model_name="google/gemma-1.1-7b-it",
31
- tokenizer_name="google/gemma-1.1-7b-it",
32
  context_window=3000,
33
  token=os.getenv("HF_TOKEN"),
34
  max_new_tokens=512,
@@ -44,57 +32,58 @@ service_context = ServiceContext.from_defaults(
44
  embed_model=embed_model
45
  )
46
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  def data_ingestion():
48
- try:
49
- documents = SimpleDirectoryReader(DATA_DIR).load_data()
50
- storage_context = StorageContext.from_defaults()
51
- index = VectorStoreIndex.from_documents(documents, service_context=service_context)
52
- index.storage_context.persist(persist_dir=PERSIST_DIR)
53
- return True
54
- except Exception as e:
55
- logger.error(f"Error during data ingestion: {str(e)}")
56
- return False
57
 
58
  def handle_query(query):
59
- try:
60
- storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
61
- index = load_index_from_storage(storage_context, service_context=service_context)
62
- chat_text_qa_msgs = [
63
- (
64
- "user",
65
- """You are a Q&A assistant named EazyPeazy, For all other inquiries, your main goal is to provide answers as accurately as possible, based on the instructions and context you have been given. If a question does not match the provided context or is outside the scope of the document, kindly advise the user to ask questions within the context of the document.
66
- Context:
67
- {context_str}
68
- Question:
69
- {query_str}
70
- """
71
- )
72
- ]
73
- text_qa_template = ChatPromptTemplate.from_messages(chat_text_qa_msgs)
74
-
75
- query_engine = index.as_query_engine(text_qa_template=text_qa_template)
76
- answer = query_engine.query(query)
77
-
78
- if hasattr(answer, 'response'):
79
- return answer.response
80
- elif isinstance(answer, dict) and 'response' in answer:
81
- return answer['response']
82
- else:
83
- return "Sorry, I couldn't find an answer."
84
- except Exception as e:
85
- logger.error(f"Error handling query: {str(e)}")
86
- return "An error occurred while processing your query. Please try again."
87
 
88
  def process_file(file):
89
  if file is None:
90
  return "Please upload a PDF file."
91
-
92
  try:
93
- temp_dir = tempfile.mkdtemp()
94
- temp_path = Path(temp_dir) / file.name
95
-
96
- # Write the file content to the temporary file
97
- temp_path.write_bytes(file.read())
98
 
99
  # Copy the file to the DATA_DIR
100
  dest_path = Path(DATA_DIR) / file.name
@@ -102,18 +91,15 @@ def process_file(file):
102
  temp_path.replace(dest_path)
103
 
104
  # Process the uploaded PDF
105
- if data_ingestion():
106
- return f"PDF '{file.name}' processed successfully. You can now ask questions about its content."
107
- else:
108
- return f"Failed to process the PDF '{file.name}'. Please try uploading again."
109
  except Exception as e:
110
- logger.error(f"Error processing file: {str(e)}")
111
- return f"An error occurred while processing the file: {str(e)}"
112
 
113
  def chat_function(message, history):
114
  response = handle_query(message)
115
  history.append((message, response))
116
- return history
117
 
118
  with gr.Blocks() as demo:
119
  gr.Markdown("# (PDF) Information and Inference🗞️")
@@ -122,6 +108,7 @@ with gr.Blocks() as demo:
122
  with gr.Row():
123
  with gr.Column(scale=1):
124
  file_output = gr.Textbox(label="Upload Status")
 
125
  upload_button = gr.UploadButton("Upload PDF", file_types=[".pdf"])
126
 
127
  with gr.Column(scale=2):
@@ -129,9 +116,9 @@ with gr.Blocks() as demo:
129
  msg = gr.Textbox(label="Ask me anything about the content of the PDF:")
130
  clear = gr.Button("Clear")
131
 
132
- upload_button.upload(process_file, upload_button, file_output)
133
- msg.submit(chat_function, [msg, chatbot], chatbot)
134
  clear.click(lambda: None, None, chatbot, queue=False)
135
 
136
  if __name__ == "__main__":
137
- demo.launch()
 
6
  from llama_index import ServiceContext
7
  from dotenv import load_dotenv
8
  import os
9
+ import base64
10
  import tempfile
11
  from pathlib import Path
 
 
 
 
 
12
 
13
  # Load environment variables
14
  load_dotenv()
15
 
 
 
 
 
 
 
 
 
16
  # Configure the Llama index settings
17
  llm = HuggingFaceInferenceAPI(
18
+ model_name="google/gemini-1.1-7b-it",
19
+ tokenizer_name="google/gemini-1.1-7b-it",
20
  context_window=3000,
21
  token=os.getenv("HF_TOKEN"),
22
  max_new_tokens=512,
 
32
  embed_model=embed_model
33
  )
34
 
35
+ # Define the directory for persistent storage and data
36
+ PERSIST_DIR = "./db"
37
+ DATA_DIR = "data"
38
+
39
+ # Ensure data directory exists
40
+ os.makedirs(DATA_DIR, exist_ok=True)
41
+ os.makedirs(PERSIST_DIR, exist_ok=True)
42
+
43
+ def displayPDF(file):
44
+ base64_pdf = base64.b64encode(file.read()).decode('utf-8')
45
+ return f'<iframe src="data:application/pdf;base64,{base64_pdf}" width="100%" height="600" type="application/pdf"></iframe>'
46
+
47
  def data_ingestion():
48
+ documents = SimpleDirectoryReader(DATA_DIR).load_data()
49
+ storage_context = StorageContext.from_defaults()
50
+ index = VectorStoreIndex.from_documents(documents, service_context=service_context)
51
+ index.storage_context.persist(persist_dir=PERSIST_DIR)
 
 
 
 
 
52
 
53
  def handle_query(query):
54
+ storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
55
+ index = load_index_from_storage(storage_context, service_context=service_context)
56
+ chat_text_qa_msgs = [
57
+ (
58
+ "user",
59
+ """You are a Q&A assistant named CHATTO, created by Suriya. You have a specific response programmed for when users specifically ask about your creator, Suriya. The response is: "I was created by Suriya, an enthusiast in Artificial Intelligence. He is dedicated to solving complex problems and delivering innovative solutions. With a strong focus on machine learning, deep learning, Python, generative AI, NLP, and computer vision, Suriya is passionate about pushing the boundaries of AI to explore new possibilities." For all other inquiries, your main goal is to provide answers as accurately as possible, based on the instructions and context you have been given. If a question does not match the provided context or is outside the scope of the document, kindly advise the user to ask questions within the context of the document.
60
+ Context:
61
+ {context_str}
62
+ Question:
63
+ {query_str}
64
+ """
65
+ )
66
+ ]
67
+ text_qa_template = ChatPromptTemplate.from_messages(chat_text_qa_msgs)
68
+
69
+ query_engine = index.as_query_engine(text_qa_template=text_qa_template)
70
+ answer = query_engine.query(query)
71
+
72
+ if hasattr(answer, 'response'):
73
+ return answer.response
74
+ elif isinstance(answer, dict) and 'response' in answer:
75
+ return answer['response']
76
+ else:
77
+ return "Sorry, I couldn't find an answer."
 
 
 
 
78
 
79
  def process_file(file):
80
  if file is None:
81
  return "Please upload a PDF file."
82
+
83
  try:
84
+ with tempfile.NamedTemporaryFile(delete=False) as temp_file:
85
+ temp_file.write(file.read())
86
+ temp_path = Path(temp_file.name)
 
 
87
 
88
  # Copy the file to the DATA_DIR
89
  dest_path = Path(DATA_DIR) / file.name
 
91
  temp_path.replace(dest_path)
92
 
93
  # Process the uploaded PDF
94
+ data_ingestion()
95
+ return f"PDF '{file.name}' processed successfully. You can now ask questions about its content.", displayPDF(file)
 
 
96
  except Exception as e:
97
+ return f"An error occurred while processing the file: {str(e)}", None
 
98
 
99
  def chat_function(message, history):
100
  response = handle_query(message)
101
  history.append((message, response))
102
+ return history, history
103
 
104
  with gr.Blocks() as demo:
105
  gr.Markdown("# (PDF) Information and Inference🗞️")
 
108
  with gr.Row():
109
  with gr.Column(scale=1):
110
  file_output = gr.Textbox(label="Upload Status")
111
+ file_display = gr.HTML()
112
  upload_button = gr.UploadButton("Upload PDF", file_types=[".pdf"])
113
 
114
  with gr.Column(scale=2):
 
116
  msg = gr.Textbox(label="Ask me anything about the content of the PDF:")
117
  clear = gr.Button("Clear")
118
 
119
+ upload_button.upload(process_file, upload_button, [file_output, file_display])
120
+ msg.submit(chat_function, [msg, chatbot], [chatbot, chatbot])
121
  clear.click(lambda: None, None, chatbot, queue=False)
122
 
123
  if __name__ == "__main__":
124
+ demo.launch()