dnzblgn commited on
Commit
49807da
Β·
verified Β·
1 Parent(s): 0ec659f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -22
app.py CHANGED
@@ -19,29 +19,28 @@ hub = {
19
  "HF_API_TOKEN": os.environ.get("HUGGINGFACE_API_TOKEN")
20
  }
21
 
22
- # Global state to keep memory
23
  vector_db = None
24
  qa_chain = None
25
- chat_memory = None # Stores previous conversations
26
 
27
- # Function to transcribe and initialize RAG pipeline
28
  def transcribe_and_setup(audio_file_path):
29
  global vector_db, qa_chain, chat_memory
30
 
31
  if audio_file_path is None:
32
- return "No audio uploaded.", None
33
 
34
- # Transcribe with Whisper
35
  result = model.transcribe(audio_file_path)
36
  transcript = result["text"]
37
 
38
- # Split and embed transcript
39
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=1024, chunk_overlap=64)
40
  splits = text_splitter.create_documents([transcript])
41
  embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
42
  vector_db = FAISS.from_documents(splits, embeddings)
43
 
44
- # Create retriever + LLM QA chain with memory
45
  chat_memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
46
  retriever = vector_db.as_retriever()
47
  llm = HuggingFaceEndpoint(
@@ -53,24 +52,39 @@ def transcribe_and_setup(audio_file_path):
53
  )
54
  qa_chain = ConversationalRetrievalChain.from_llm(llm=llm, retriever=retriever, memory=chat_memory)
55
 
56
- return "Transcription complete! Ready for questions.", None # Don't return transcript
57
 
58
- # Function to handle chat with memory
59
  def answer_question(question, chat_history):
60
  global qa_chain, chat_memory
61
  if qa_chain is None:
62
  return "Please upload and process an audio file first.", chat_history
63
 
64
- response = qa_chain.invoke({"question": question, "chat_history": chat_memory.load_memory_variables({})["chat_history"]})
65
-
66
- # Append new interaction to chat history
67
- chat_history.append(("User", question))
68
- chat_history.append(("Assistant", response["answer"]))
69
-
70
- return "", chat_history # Return empty input box, updated chat history
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
- # Gradio UI
73
- with gr.Blocks(theme=gr.themes.Soft(), css="footer {display:none !important;}") as demo:
74
  gr.Markdown("## πŸŽ™οΈ **Conversational Audio Chatbot**")
75
  gr.Markdown("Upload an audio file, let the AI process it, and ask any questions!")
76
 
@@ -80,20 +94,20 @@ with gr.Blocks(theme=gr.themes.Soft(), css="footer {display:none !important;}")
80
  transcribe_button = gr.Button("πŸš€ Process Audio")
81
  status_output = gr.Textbox(label="πŸ› οΈ Status", interactive=False)
82
  with gr.Column(scale=2):
83
- chatbot = gr.Chatbot(label="πŸ’¬ Chat with your audio", height=400)
84
  question_input = gr.Textbox(label="Type your question", placeholder="Ask about the audio...")
85
  ask_button = gr.Button("πŸ’‘ Ask")
86
 
87
  transcribe_button.click(
88
  fn=transcribe_and_setup,
89
  inputs=audio_input,
90
- outputs=[status_output, chatbot] # No transcript, just chatbot reset
91
  )
92
 
93
  ask_button.click(
94
  fn=answer_question,
95
  inputs=[question_input, chatbot],
96
- outputs=[question_input, chatbot] # Keeps chat history alive
97
  )
98
 
99
- demo.launch()
 
19
  "HF_API_TOKEN": os.environ.get("HUGGINGFACE_API_TOKEN")
20
  }
21
 
22
+ # Global state
23
  vector_db = None
24
  qa_chain = None
25
+ chat_memory = None
26
 
27
+ # Transcribe and set up RAG
28
  def transcribe_and_setup(audio_file_path):
29
  global vector_db, qa_chain, chat_memory
30
 
31
  if audio_file_path is None:
32
+ return "No audio uploaded.", []
33
 
 
34
  result = model.transcribe(audio_file_path)
35
  transcript = result["text"]
36
 
37
+ # Split and embed
38
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=1024, chunk_overlap=64)
39
  splits = text_splitter.create_documents([transcript])
40
  embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
41
  vector_db = FAISS.from_documents(splits, embeddings)
42
 
43
+ # QA setup
44
  chat_memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
45
  retriever = vector_db.as_retriever()
46
  llm = HuggingFaceEndpoint(
 
52
  )
53
  qa_chain = ConversationalRetrievalChain.from_llm(llm=llm, retriever=retriever, memory=chat_memory)
54
 
55
+ return "Transcription complete! Ready for questions.", [] # Empty chat reset
56
 
57
+ # Handle conversation
58
  def answer_question(question, chat_history):
59
  global qa_chain, chat_memory
60
  if qa_chain is None:
61
  return "Please upload and process an audio file first.", chat_history
62
 
63
+ response = qa_chain.invoke({
64
+ "question": question,
65
+ "chat_history": chat_memory.load_memory_variables({})["chat_history"]
66
+ })
67
+
68
+ # Just show back-and-forth messages
69
+ chat_history.append([question, response["answer"]])
70
+ return "", chat_history
71
+
72
+ # Custom CSS
73
+ custom_css = """
74
+ .chatbox .message.user, .chatbox .message.bot {
75
+ background-color: #1e3d2f !important;
76
+ color: #ffffff !important;
77
+ border-radius: 10px !important;
78
+ padding: 10px !important;
79
+ margin: 5px !important;
80
+ }
81
+ .chatbox .message.user::before, .chatbox .message.bot::before {
82
+ content: none !important;
83
+ }
84
+ """
85
 
86
+ # Gradio app
87
+ with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo:
88
  gr.Markdown("## πŸŽ™οΈ **Conversational Audio Chatbot**")
89
  gr.Markdown("Upload an audio file, let the AI process it, and ask any questions!")
90
 
 
94
  transcribe_button = gr.Button("πŸš€ Process Audio")
95
  status_output = gr.Textbox(label="πŸ› οΈ Status", interactive=False)
96
  with gr.Column(scale=2):
97
+ chatbot = gr.Chatbot(label="πŸ’¬ Chat with your audio", elem_classes=["chatbox"])
98
  question_input = gr.Textbox(label="Type your question", placeholder="Ask about the audio...")
99
  ask_button = gr.Button("πŸ’‘ Ask")
100
 
101
  transcribe_button.click(
102
  fn=transcribe_and_setup,
103
  inputs=audio_input,
104
+ outputs=[status_output, chatbot]
105
  )
106
 
107
  ask_button.click(
108
  fn=answer_question,
109
  inputs=[question_input, chatbot],
110
+ outputs=[question_input, chatbot]
111
  )
112
 
113
+ demo.launch()