Alimubariz124 commited on
Commit
7cd4bfc
·
verified ·
1 Parent(s): 9eec855

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -22
app.py CHANGED
@@ -3,36 +3,61 @@ from model_loader import load_embedding_model, load_llm
3
  from transcript_handler import chunk_text, embed_chunks, create_faiss_index
4
  from qa_engine import query_faiss, build_prompt
5
 
 
6
  embedder = load_embedding_model()
7
  llm = load_llm()
8
 
9
- index = None
10
- chunks = []
11
-
12
- def upload_transcript(file):
13
- global index, chunks
14
- text = file.read().decode("utf-8")
15
- chunks = chunk_text(text)
16
- embeddings, chunks = embed_chunks(chunks, embedder)
17
- index = create_faiss_index(embeddings)
18
- return "Transcript uploaded and indexed successfully!"
19
-
20
- def chat_with_transcript(query):
21
- if not index:
22
- return "Please upload a transcript first."
23
- context = query_faiss(query, index, embedder, chunks)
24
- prompt = build_prompt(context, query)
25
- response = llm(prompt)[0]['generated_text'].split("Answer:")[-1].strip()
26
- return response
27
-
28
  with gr.Blocks() as demo:
29
  gr.Markdown("# 📄 Chat with a Transcript (Open Source + Free!)")
 
 
 
 
 
30
  transcript_input = gr.File(label="Upload Transcript (.txt)")
31
  upload_button = gr.Button("Upload and Process")
32
  query_input = gr.Textbox(label="Ask a question about the transcript")
33
  answer_output = gr.Textbox(label="Answer")
34
 
35
- upload_button.click(upload_transcript, inputs=[transcript_input], outputs=[])
36
- query_input.submit(chat_with_transcript, inputs=[query_input], outputs=[answer_output])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
- demo.launch()
 
3
  from transcript_handler import chunk_text, embed_chunks, create_faiss_index
4
  from qa_engine import query_faiss, build_prompt
5
 
6
+ # Load models
7
  embedder = load_embedding_model()
8
  llm = load_llm()
9
 
10
+ # Main Gradio app
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  with gr.Blocks() as demo:
12
  gr.Markdown("# 📄 Chat with a Transcript (Open Source + Free!)")
13
+
14
+ # State variables for storing index and chunks per session
15
+ index_state = gr.State(None)
16
+ chunks_state = gr.State([])
17
+
18
  transcript_input = gr.File(label="Upload Transcript (.txt)")
19
  upload_button = gr.Button("Upload and Process")
20
  query_input = gr.Textbox(label="Ask a question about the transcript")
21
  answer_output = gr.Textbox(label="Answer")
22
 
23
+ def upload_transcript(file, chunks_state):
24
+ try:
25
+ text = file.read().decode("utf-8")
26
+ if not text.strip():
27
+ return "Error: Uploaded file is empty.", None, []
28
+
29
+ chunks = chunk_text(text)
30
+ if not chunks:
31
+ return "Error: No chunks generated from the transcript.", None, []
32
+
33
+ embeddings, chunks = embed_chunks(chunks, embedder)
34
+ if embeddings.size == 0:
35
+ return "Error: Failed to generate embeddings.", None, []
36
+
37
+ index = create_faiss_index(embeddings)
38
+ return "Transcript uploaded and indexed successfully!", index, chunks
39
+ except Exception as e:
40
+ return f"Error processing transcript: {str(e)}", None, []
41
+
42
+ def chat_with_transcript(query, index_state, chunks_state):
43
+ if index_state is None:
44
+ return "Please upload a transcript first."
45
+ context = query_faiss(query, index_state, embedder, chunks_state)
46
+ prompt = build_prompt(context, query)
47
+ response = llm(prompt)[0]['generated_text']
48
+ if "Answer:" not in response:
49
+ return "Error: Unable to parse the model's response."
50
+ return response.split("Answer:")[-1].strip()
51
+
52
+ upload_button.click(
53
+ upload_transcript,
54
+ inputs=[transcript_input, chunks_state],
55
+ outputs=[answer_output, index_state, chunks_state]
56
+ )
57
+ query_input.submit(
58
+ chat_with_transcript,
59
+ inputs=[query_input, index_state, chunks_state],
60
+ outputs=[answer_output]
61
+ )
62
 
63
+ demo.launch()