MCPcopy Index your code
hub / github.com/codingis4noobs2/QuickDigest / chat_with_files

Function chat_with_files

app.py:83–283  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

81
82# Function to display chat with files page
83def chat_with_files():
84 import os
85 import streamlit as st
86 from streamlit_extras.badges import badge
87 from streamlit_extras.colored_header import colored_header
88 from llama_index import (
89 OpenAIEmbedding,
90 ServiceContext,
91 set_global_service_context,
92 )
93 from llama_index.llms import OpenAI
94 from llama_index.chat_engine.types import StreamingAgentChatResponse
95 from llama_index import SimpleDirectoryReader, VectorStoreIndex
96 import assemblyai as aai
97 from PyPDF2 import PdfReader
98 from docx import Document
99
100 # Cache the result to avoid recomputation
101 @st.cache_resource(show_spinner="Indexing documents...Please have patience")
102 def build_index(files):
103 documents = SimpleDirectoryReader(input_files=files).load_data()
104 index = VectorStoreIndex.from_documents(documents)
105 return index
106
107 # Handle streaming responses
108 def handle_stream(root, stream: StreamingAgentChatResponse):
109 text = ""
110 root.markdown("Thinking...")
111 for token in stream.response_gen:
112 text += token
113 root.markdown(text)
114 return text
115
116 # Define constants and settings
117 CACHE_DIR = "./uploads"
118 aai.settings.api_key = st.secrets['assembly_api_key']
119
120 # Render chat messages
121 def render_message(message):
122 with st.chat_message(message["role"]):
123 st.write(message["text"])
124
125 # Transcribe audio and video files
126 def transcribe_audio_video(file_path):
127 transcriber = aai.Transcriber()
128 transcript = transcriber.transcribe(file_path)
129 transcript_path = file_path + ".txt"
130 with open(transcript_path, "w") as f:
131 f.write(transcript.text)
132 return transcript_path
133
134 # Upload files and cache them
135 def upload_files(types=["pdf", "txt", "mp3", "mp4", 'mpeg', 'doc', 'docx'], **kwargs):
136 files = st.file_uploader(
137 label=f"Upload files", type=types, **kwargs
138 )
139 if not files:
140 st.info(f"Please add documents, Note: Scanned documents are not supported yet!")

Callers

nothing calls this directly

Calls 6

get_keyFunction · 0.85
upload_filesFunction · 0.85
process_documentsFunction · 0.85
build_indexFunction · 0.85
render_messageFunction · 0.85
handle_streamFunction · 0.85

Tested by

no test coverage detected