Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,161 +1,9 @@
|
|
1 |
-
import os
|
2 |
-
import gradio as gr
|
3 |
-
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
4 |
-
from langchain_community.llms import HuggingFacePipeline
|
5 |
-
from langchain_community.document_loaders import TextLoader, PyPDFLoader
|
6 |
-
from langchain.text_splitter import CharacterTextSplitter
|
7 |
-
from langchain_community.vectorstores import FAISS
|
8 |
-
from langchain_community.embeddings import HuggingFaceEmbeddings
|
9 |
-
from langchain.prompts import PromptTemplate
|
10 |
-
from langchain.chains import RetrievalQA
|
11 |
-
from huggingface_hub import login
|
12 |
-
import diarization
|
13 |
-
import shutil
|
14 |
-
import spaces
|
15 |
-
import time
|
16 |
-
from langdetect import detect
|
17 |
-
|
18 |
-
# Set environment variable to disable tokenizers parallelism warning
|
19 |
-
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
20 |
-
|
21 |
-
# Get Hugging Face token from Space secret
|
22 |
-
hf_token = os.environ.get('hf_secret')
|
23 |
-
if not hf_token:
|
24 |
-
raise ValueError("HF_TOKEN not found in environment variables. Please set it in the Space secrets.")
|
25 |
-
|
26 |
-
# Login to Hugging Face
|
27 |
-
login(token=hf_token)
|
28 |
-
|
29 |
-
# Language detection function
|
30 |
-
def detect_language(text):
|
31 |
-
try:
|
32 |
-
return detect(text)
|
33 |
-
except:
|
34 |
-
return "en" # default to English if detection fails
|
35 |
-
|
36 |
-
# Lazy initialization for the pipeline
|
37 |
-
class LazyPipeline:
|
38 |
-
def __init__(self):
|
39 |
-
self.pipeline = None
|
40 |
-
|
41 |
-
@spaces.GPU(duration=250)
|
42 |
-
def get_pipeline(self):
|
43 |
-
if self.pipeline is None:
|
44 |
-
import torch
|
45 |
-
model_name = "mistralai/Mistral-7B-Instruct-v0.3"
|
46 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
47 |
-
model = AutoModelForCausalLM.from_pretrained(
|
48 |
-
model_name,
|
49 |
-
torch_dtype=torch.float16,
|
50 |
-
device_map="auto",
|
51 |
-
)
|
52 |
-
self.pipeline = pipeline(
|
53 |
-
"text-generation",
|
54 |
-
model=model,
|
55 |
-
tokenizer=tokenizer,
|
56 |
-
max_length = 2000,
|
57 |
-
max_new_tokens=512,
|
58 |
-
temperature=0.8,
|
59 |
-
)
|
60 |
-
return self.pipeline
|
61 |
-
|
62 |
-
lazy_pipe = LazyPipeline()
|
63 |
-
|
64 |
-
# Create a LangChain wrapper around the pipeline
|
65 |
-
class LazyLLM:
|
66 |
-
def __init__(self, lazy_pipeline):
|
67 |
-
self.lazy_pipeline = lazy_pipeline
|
68 |
-
self.llm = None
|
69 |
-
|
70 |
-
@spaces.GPU(duration=150)
|
71 |
-
def get_llm(self):
|
72 |
-
if self.llm is None:
|
73 |
-
pipe = self.lazy_pipeline.get_pipeline()
|
74 |
-
self.llm = HuggingFacePipeline(pipeline=pipe)
|
75 |
-
return self.llm
|
76 |
-
|
77 |
-
lazy_llm = LazyLLM(lazy_pipe)
|
78 |
-
|
79 |
-
# Load instruction files
|
80 |
-
def load_instructions(file_path):
|
81 |
-
with open(file_path, 'r') as file:
|
82 |
-
return file.read().strip()
|
83 |
-
|
84 |
-
attachments_task = load_instructions("tasks/Attachments_task.txt")
|
85 |
-
bigfive_task = load_instructions("tasks/BigFive_task.txt")
|
86 |
-
personalities_task = load_instructions("tasks/Personalities_task.txt")
|
87 |
-
|
88 |
-
# Load knowledge files
|
89 |
-
def load_knowledge(file_path):
|
90 |
-
loader = TextLoader(file_path)
|
91 |
-
documents = loader.load()
|
92 |
-
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
|
93 |
-
texts = text_splitter.split_documents(documents)
|
94 |
-
return texts
|
95 |
-
|
96 |
-
attachments_knowledge = load_knowledge("knowledge/bartholomew_attachments_definitions - no int.txt")
|
97 |
-
bigfive_knowledge = load_knowledge("knowledge/bigfive_definitions.txt")
|
98 |
-
personalities_knowledge = load_knowledge("knowledge/personalities_definitions.txt")
|
99 |
-
|
100 |
-
# Create vector stores
|
101 |
-
embeddings = HuggingFaceEmbeddings()
|
102 |
-
attachments_db = FAISS.from_documents(attachments_knowledge, embeddings)
|
103 |
-
bigfive_db = FAISS.from_documents(bigfive_knowledge, embeddings)
|
104 |
-
personalities_db = FAISS.from_documents(personalities_knowledge, embeddings)
|
105 |
-
|
106 |
-
# Lazy initialization for retrieval chains
|
107 |
-
class LazyChains:
|
108 |
-
def __init__(self, lazy_llm):
|
109 |
-
self.lazy_llm = lazy_llm
|
110 |
-
self.attachments_chain = None
|
111 |
-
self.bigfive_chain = None
|
112 |
-
self.personalities_chain = None
|
113 |
-
|
114 |
-
def create_prompt(self, task):
|
115 |
-
return PromptTemplate(
|
116 |
-
template=task + "\n\nContext: {context}\n\nTask: {question}\n\n-----------\n\nAnswer: ",
|
117 |
-
input_variables=["context", "question"]
|
118 |
-
)
|
119 |
-
|
120 |
-
@spaces.GPU(duration=200)
|
121 |
-
def get_chains(self):
|
122 |
-
if self.attachments_chain is None:
|
123 |
-
llm = self.lazy_llm.get_llm()
|
124 |
-
self.attachments_chain = RetrievalQA.from_chain_type(
|
125 |
-
llm=llm,
|
126 |
-
chain_type="stuff",
|
127 |
-
retriever=attachments_db.as_retriever(),
|
128 |
-
chain_type_kwargs={"prompt": self.create_prompt(attachments_task)}
|
129 |
-
)
|
130 |
-
self.bigfive_chain = RetrievalQA.from_chain_type(
|
131 |
-
llm=llm,
|
132 |
-
chain_type="stuff",
|
133 |
-
retriever=bigfive_db.as_retriever(),
|
134 |
-
chain_type_kwargs={"prompt": self.create_prompt(bigfive_task)}
|
135 |
-
)
|
136 |
-
self.personalities_chain = RetrievalQA.from_chain_type(
|
137 |
-
llm=llm,
|
138 |
-
chain_type="stuff",
|
139 |
-
retriever=personalities_db.as_retriever(),
|
140 |
-
chain_type_kwargs={"prompt": self.create_prompt(personalities_task)}
|
141 |
-
)
|
142 |
-
return self.attachments_chain, self.bigfive_chain, self.personalities_chain
|
143 |
-
|
144 |
-
lazy_chains = LazyChains(lazy_llm)
|
145 |
-
|
146 |
-
@spaces.GPU(duration=150)
|
147 |
-
|
148 |
-
def count_words_and_tokens(text):
|
149 |
-
words = len(text.split())
|
150 |
-
tokens = len(AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.3").tokenize(text))
|
151 |
-
return words, tokens
|
152 |
-
|
153 |
|
154 |
@spaces.GPU(duration=150)
|
155 |
def process_input(input_file):
|
156 |
start_time = time.time()
|
157 |
|
158 |
-
yield 0, "Processing file...", None, None, None, None, None, None, None
|
159 |
|
160 |
file_extension = os.path.splitext(input_file.name)[1].lower()
|
161 |
|
@@ -176,7 +24,7 @@ def process_input(input_file):
|
|
176 |
temp_video_path = "temp_video" + file_extension
|
177 |
shutil.copy2(input_file.name, temp_video_path)
|
178 |
|
179 |
-
yield 0.2, "Transcribing video...", None, None, None, None, None, None,
|
180 |
|
181 |
language = "en" # Default to English for video files
|
182 |
diarization.process_video(temp_video_path, hf_token, language)
|
@@ -188,24 +36,24 @@ def process_input(input_file):
|
|
188 |
input_info = f"Video transcribed. Words: {words}, Tokens: {tokens}"
|
189 |
video_path = temp_video_path
|
190 |
else:
|
191 |
-
yield 1, "Unsupported file format. Please upload a TXT, PDF, or video file.", None, None, None, None, None, None, None
|
192 |
return
|
193 |
|
194 |
detected_language = detect_language(content)
|
195 |
|
196 |
-
yield 0.4, "Analyzing content...", None, detected_language, input_info, None, None, None,
|
197 |
|
198 |
attachments_chain, bigfive_chain, personalities_chain = lazy_chains.get_chains()
|
199 |
|
200 |
-
yield 0.6, "Analyzing attachments...", None, detected_language, input_info, None, None, None,
|
201 |
attachments_result = attachments_chain({"query": content})
|
202 |
attachments_answer = attachments_result['result'].split("-----------\n\nAnswer:")[-1].strip()
|
203 |
|
204 |
-
yield 0.7, "Analyzing Big Five traits...", None, detected_language, input_info, attachments_answer, None, None,
|
205 |
bigfive_result = bigfive_chain({"query": content})
|
206 |
bigfive_answer = bigfive_result['result'].split("-----------\n\nAnswer:")[-1].strip()
|
207 |
|
208 |
-
yield 0.8, "Analyzing personalities...", None, detected_language, input_info, attachments_answer, bigfive_answer, None,
|
209 |
personalities_result = personalities_chain({"query": content})
|
210 |
personalities_answer = personalities_result['result'].split("-----------\n\nAnswer:")[-1].strip()
|
211 |
|
@@ -225,7 +73,7 @@ def create_interface():
|
|
225 |
input_file = gr.File(label="Upload File (TXT, PDF, or Video)")
|
226 |
|
227 |
with gr.Column():
|
228 |
-
progress = gr.
|
229 |
progress_text = gr.Textbox(label="Status")
|
230 |
execution_time = gr.Textbox(label="Execution Time", visible=False)
|
231 |
detected_language = gr.Textbox(label="Detected Language", visible=False)
|
@@ -237,7 +85,6 @@ def create_interface():
|
|
237 |
|
238 |
def update_output(progress_value, progress_text, execution_time, detected_lang, input_info, attachments, bigfive, personalities, video):
|
239 |
return {
|
240 |
-
progress: progress_value,
|
241 |
progress_text: progress_text,
|
242 |
execution_time: gr.update(value=execution_time, visible=execution_time is not None),
|
243 |
detected_language: gr.update(value=detected_lang, visible=detected_lang is not None),
|
@@ -252,11 +99,10 @@ def create_interface():
|
|
252 |
fn=process_input,
|
253 |
inputs=[input_file],
|
254 |
outputs=[progress, progress_text, execution_time, detected_language, input_info, attachments_output, bigfive_output, personalities_output, video_output],
|
255 |
-
show_progress=True
|
256 |
).then(
|
257 |
fn=update_output,
|
258 |
inputs=[progress, progress_text, execution_time, detected_language, input_info, attachments_output, bigfive_output, personalities_output, video_output],
|
259 |
-
outputs=[
|
260 |
)
|
261 |
|
262 |
return iface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
|
2 |
@spaces.GPU(duration=150)
|
3 |
def process_input(input_file):
|
4 |
start_time = time.time()
|
5 |
|
6 |
+
yield 0, "Processing file...", None, None, None, None, None, None, None
|
7 |
|
8 |
file_extension = os.path.splitext(input_file.name)[1].lower()
|
9 |
|
|
|
24 |
temp_video_path = "temp_video" + file_extension
|
25 |
shutil.copy2(input_file.name, temp_video_path)
|
26 |
|
27 |
+
yield 0.2, "Transcribing video...", None, None, None, None, None, None, temp_video_path
|
28 |
|
29 |
language = "en" # Default to English for video files
|
30 |
diarization.process_video(temp_video_path, hf_token, language)
|
|
|
36 |
input_info = f"Video transcribed. Words: {words}, Tokens: {tokens}"
|
37 |
video_path = temp_video_path
|
38 |
else:
|
39 |
+
yield 1, "Unsupported file format. Please upload a TXT, PDF, or video file.", None, None, None, None, None, None, None
|
40 |
return
|
41 |
|
42 |
detected_language = detect_language(content)
|
43 |
|
44 |
+
yield 0.4, "Analyzing content...", None, detected_language, input_info, None, None, None, video_path
|
45 |
|
46 |
attachments_chain, bigfive_chain, personalities_chain = lazy_chains.get_chains()
|
47 |
|
48 |
+
yield 0.6, "Analyzing attachments...", None, detected_language, input_info, None, None, None, video_path
|
49 |
attachments_result = attachments_chain({"query": content})
|
50 |
attachments_answer = attachments_result['result'].split("-----------\n\nAnswer:")[-1].strip()
|
51 |
|
52 |
+
yield 0.7, "Analyzing Big Five traits...", None, detected_language, input_info, attachments_answer, None, None, video_path
|
53 |
bigfive_result = bigfive_chain({"query": content})
|
54 |
bigfive_answer = bigfive_result['result'].split("-----------\n\nAnswer:")[-1].strip()
|
55 |
|
56 |
+
yield 0.8, "Analyzing personalities...", None, detected_language, input_info, attachments_answer, bigfive_answer, None, video_path
|
57 |
personalities_result = personalities_chain({"query": content})
|
58 |
personalities_answer = personalities_result['result'].split("-----------\n\nAnswer:")[-1].strip()
|
59 |
|
|
|
73 |
input_file = gr.File(label="Upload File (TXT, PDF, or Video)")
|
74 |
|
75 |
with gr.Column():
|
76 |
+
progress = gr.Progress()
|
77 |
progress_text = gr.Textbox(label="Status")
|
78 |
execution_time = gr.Textbox(label="Execution Time", visible=False)
|
79 |
detected_language = gr.Textbox(label="Detected Language", visible=False)
|
|
|
85 |
|
86 |
def update_output(progress_value, progress_text, execution_time, detected_lang, input_info, attachments, bigfive, personalities, video):
|
87 |
return {
|
|
|
88 |
progress_text: progress_text,
|
89 |
execution_time: gr.update(value=execution_time, visible=execution_time is not None),
|
90 |
detected_language: gr.update(value=detected_lang, visible=detected_lang is not None),
|
|
|
99 |
fn=process_input,
|
100 |
inputs=[input_file],
|
101 |
outputs=[progress, progress_text, execution_time, detected_language, input_info, attachments_output, bigfive_output, personalities_output, video_output],
|
|
|
102 |
).then(
|
103 |
fn=update_output,
|
104 |
inputs=[progress, progress_text, execution_time, detected_language, input_info, attachments_output, bigfive_output, personalities_output, video_output],
|
105 |
+
outputs=[progress_text, execution_time, detected_language, input_info, attachments_output, bigfive_output, personalities_output, video_output]
|
106 |
)
|
107 |
|
108 |
return iface
|