Spaces:
Running
Running
aakashch0179
commited on
Commit
•
fff8388
1
Parent(s):
51ae3cb
Update app.py
Browse files
app.py
CHANGED
@@ -146,130 +146,156 @@
|
|
146 |
# # Process Question and Generate Answer
|
147 |
# process_vqa(image, question)
|
148 |
|
149 |
-
# Chat with pdf
|
150 |
-
import gradio as gr
|
151 |
-
import streamlit as st
|
152 |
-
from langchain.embeddings.openai import OpenAIEmbeddings
|
153 |
-
from langchain.text_splitter import CharacterTextSplitter
|
154 |
-
from langchain.vectorstores import Chroma
|
155 |
-
from langchain.chains import ConversationalRetrievalChain
|
156 |
-
from langchain.chat_models import ChatOpenAI
|
157 |
-
from langchain.document_loaders import PyPDFLoader
|
158 |
-
import os
|
159 |
-
import fitz
|
160 |
-
from PIL import Image
|
161 |
-
|
162 |
-
# Global variables
|
163 |
-
COUNT, N = 0, 0
|
164 |
-
chat_history = []
|
165 |
-
chain = None # Initialize chain as None
|
166 |
-
|
167 |
-
# Function to set the OpenAI API key
|
168 |
-
def set_apikey(api_key):
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
# Function to enable the API key input box
|
173 |
-
def enable_api_box():
|
174 |
-
|
175 |
-
|
176 |
-
# Function to add text to the chat history
|
177 |
-
def add_text(history, text):
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
# Function to process the PDF file and create a conversation chain
|
184 |
-
def process_file(file):
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
# Function to generate a response based on the chat history and query
|
201 |
-
def generate_response(history, query, pdf_upload):
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
# Function to render a specific page of a PDF file as an image
|
220 |
-
def render_file(file):
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
# Function to render initial content from the PDF
|
229 |
-
def render_first(pdf_file):
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
# Streamlit & Gradio Interface
|
235 |
-
|
236 |
-
st.title("PDF-Powered Chatbot")
|
237 |
-
|
238 |
-
with st.container():
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
if __name__ == "__main__":
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
274 |
|
275 |
|
|
|
146 |
# # Process Question and Generate Answer
|
147 |
# process_vqa(image, question)
|
148 |
|
149 |
+
# # Chat with pdf
|
150 |
+
# import gradio as gr
|
151 |
+
# import streamlit as st
|
152 |
+
# from langchain.embeddings.openai import OpenAIEmbeddings
|
153 |
+
# from langchain.text_splitter import CharacterTextSplitter
|
154 |
+
# from langchain.vectorstores import Chroma
|
155 |
+
# from langchain.chains import ConversationalRetrievalChain
|
156 |
+
# from langchain.chat_models import ChatOpenAI
|
157 |
+
# from langchain.document_loaders import PyPDFLoader
|
158 |
+
# import os
|
159 |
+
# import fitz
|
160 |
+
# from PIL import Image
|
161 |
+
|
162 |
+
# # Global variables
|
163 |
+
# COUNT, N = 0, 0
|
164 |
+
# chat_history = []
|
165 |
+
# chain = None # Initialize chain as None
|
166 |
+
|
167 |
+
# # Function to set the OpenAI API key
|
168 |
+
# def set_apikey(api_key):
|
169 |
+
# os.environ['OPENAI_API_KEY'] = api_key
|
170 |
+
# return disable_box
|
171 |
+
|
172 |
+
# # Function to enable the API key input box
|
173 |
+
# def enable_api_box():
|
174 |
+
# return enable_box
|
175 |
+
|
176 |
+
# # Function to add text to the chat history
|
177 |
+
# def add_text(history, text):
|
178 |
+
# if not text:
|
179 |
+
# raise gr.Error('Enter text')
|
180 |
+
# history = history + [(text, '')]
|
181 |
+
# return history
|
182 |
+
|
183 |
+
# # Function to process the PDF file and create a conversation chain
|
184 |
+
# def process_file(file):
|
185 |
+
# global chain
|
186 |
+
# if 'OPENAI_API_KEY' not in os.environ:
|
187 |
+
# raise gr.Error('Upload your OpenAI API key')
|
188 |
+
|
189 |
+
# # Replace with your actual PDF processing logic
|
190 |
+
# loader = PyPDFLoader(file.name)
|
191 |
+
# documents = loader.load()
|
192 |
+
# embeddings = OpenAIEmbeddings()
|
193 |
+
# pdfsearch = Chroma.from_documents(documents, embeddings)
|
194 |
+
|
195 |
+
# chain = ConversationalRetrievalChain.from_llm(ChatOpenAI(temperature=0.3),
|
196 |
+
# retriever=pdfsearch.as_retriever(search_kwargs={"k": 1}),
|
197 |
+
# return_source_documents=True)
|
198 |
+
# return chain
|
199 |
+
|
200 |
+
# # Function to generate a response based on the chat history and query
|
201 |
+
# def generate_response(history, query, pdf_upload):
|
202 |
+
# global COUNT, N, chat_history, chain
|
203 |
+
# if not pdf_upload:
|
204 |
+
# raise gr.Error(message='Upload a PDF')
|
205 |
+
|
206 |
+
# if COUNT == 0:
|
207 |
+
# chain = process_file(pdf_upload)
|
208 |
+
# COUNT += 1
|
209 |
+
|
210 |
+
# # Replace with your LangChain logic to generate a response
|
211 |
+
# result = chain({"question": query, 'chat_history': chat_history}, return_only_outputs=True)
|
212 |
+
# chat_history += [(query, result["answer"])]
|
213 |
+
# N = list(result['source_documents'][0])[1][1]['page'] # Adjust as needed
|
214 |
+
|
215 |
+
# for char in result['answer']:
|
216 |
+
# history[-1][-1] += char
|
217 |
+
# return history, ''
|
218 |
+
|
219 |
+
# # Function to render a specific page of a PDF file as an image
|
220 |
+
# def render_file(file):
|
221 |
+
# global N
|
222 |
+
# doc = fitz.open(file.name)
|
223 |
+
# page = doc[N]
|
224 |
+
# pix = page.get_pixmap(matrix=fitz.Matrix(300/72, 300/72))
|
225 |
+
# image = Image.frombytes('RGB', [pix.width, pix.height], pix.samples)
|
226 |
+
# return image
|
227 |
+
|
228 |
+
# # Function to render initial content from the PDF
|
229 |
+
# def render_first(pdf_file):
|
230 |
+
# # Replace with logic to process the PDF and generate an initial image
|
231 |
+
# image = Image.new('RGB', (600, 400), color = 'white') # Placeholder
|
232 |
+
# return image
|
233 |
+
|
234 |
+
# # Streamlit & Gradio Interface
|
235 |
+
|
236 |
+
# st.title("PDF-Powered Chatbot")
|
237 |
+
|
238 |
+
# with st.container():
|
239 |
+
# gr.Markdown("""
|
240 |
+
# <style>
|
241 |
+
# .image-container { height: 680px; }
|
242 |
+
# </style>
|
243 |
+
# """)
|
244 |
+
|
245 |
+
# with gr.Blocks() as demo:
|
246 |
+
# pdf_upload1 = gr.UploadButton("📁 Upload PDF 1", file_types=[".pdf"]) # Define pdf_upload1
|
247 |
+
|
248 |
+
# # ... (rest of your interface creation)
|
249 |
+
|
250 |
+
# txt = gr.Textbox(label="Enter your query", placeholder="Ask a question...")
|
251 |
+
# submit_btn = gr.Button('Submit')
|
252 |
+
|
253 |
+
# @submit_btn.click()
|
254 |
+
# def on_submit():
|
255 |
+
# add_text(chatbot, txt)
|
256 |
+
# generate_response(chatbot, txt, pdf_upload1) # Use pdf_upload1 here
|
257 |
+
# render_file(pdf_upload1) # Use pdf_upload1 here
|
258 |
+
|
259 |
+
# if __name__ == "__main__":
|
260 |
+
# gr.Interface(
|
261 |
+
# fn=generate_response,
|
262 |
+
# inputs=[
|
263 |
+
# "file", # Define pdf_upload1
|
264 |
+
# "text", # Define chatbot output
|
265 |
+
# "text" # Define txt
|
266 |
+
# ],
|
267 |
+
# outputs=[
|
268 |
+
# "image", # Define show_img
|
269 |
+
# "text", # Define chatbot output
|
270 |
+
# "text" # Define txt
|
271 |
+
# ],
|
272 |
+
# title="PDF-Powered Chatbot"
|
273 |
+
# ).launch(server_port=8888)
|
274 |
+
|
275 |
+
# Text to audio
|
276 |
+
from transformers import AutoProcessor , BarkModel
|
277 |
+
import scipy
|
278 |
+
|
279 |
+
processor = AutoProcessor.from_pretrained("suno/bark")
|
280 |
+
model = BarkModel.from_pretrained("suno/bark")
|
281 |
+
model.to("cuda")
|
282 |
+
|
283 |
+
def generate_audio(text,preset, output):
|
284 |
+
inputs = processor(text, voice_preset=preset)
|
285 |
+
for k , v in inputs.items:
|
286 |
+
inputs[k] = v.to("cuda")
|
287 |
+
audio_array = model.generate(**inputs)
|
288 |
+
audio_array = audio_array.cpu().numpy().squeeze()
|
289 |
+
scipy.io.wavfile.write(output,rate= sample_rate,data= audio_array)
|
290 |
+
|
291 |
+
|
292 |
+
generate_audio(
|
293 |
+
|
294 |
+
text= "HI, welcome to our app hope you enjoy our app ,Thankyou for using our app YOURS Sincerely, Cosmo",
|
295 |
+
preset= "v2/en_speaker_3",
|
296 |
+
output= "output.wav"
|
297 |
+
|
298 |
+
|
299 |
+
)
|
300 |
|
301 |
|