Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from langchain.prompts import PromptTemplate
|
3 |
+
from langchain_huggingface import HuggingFaceEndpoint
|
4 |
+
from langchain_core.output_parsers import JsonOutputParser
|
5 |
+
from langdetect import detect
|
6 |
+
import time
|
7 |
+
import torch
|
8 |
+
from transformers import pipeline
|
9 |
+
import re
|
10 |
+
|
11 |
+
# Initialize the LLM and other components
|
12 |
+
llm = HuggingFaceEndpoint(
|
13 |
+
repo_id="mistralai/Mistral-7B-Instruct-v0.3",
|
14 |
+
task="text-generation",
|
15 |
+
max_new_tokens=128,
|
16 |
+
temperature=0.7,
|
17 |
+
do_sample=False,
|
18 |
+
)
|
19 |
+
|
20 |
+
template_classify = '''
|
21 |
+
You are a topic detector bot. Your task is to determine the main topic of given text phrase.
|
22 |
+
|
23 |
+
Answer general main topic not specific words.
|
24 |
+
Your answer does not contain specific information from given text.
|
25 |
+
Answer just one general main topic. Do not answer two or more topic.
|
26 |
+
Answer shortly with two or three word phrase. Do not answer with long sentence.
|
27 |
+
Answer topic with context. Example, if it says "My delivery is late", its topic is late delivery.
|
28 |
+
If you do not know the topic just answer as General.
|
29 |
+
What is the main topic of given text?:
|
30 |
+
|
31 |
+
<text>
|
32 |
+
{TEXT}
|
33 |
+
</text>
|
34 |
+
|
35 |
+
convert it to json format using 'Answer' as key and return it.
|
36 |
+
Your final response MUST contain only the response, no other text.
|
37 |
+
Example:
|
38 |
+
{{"Answer":["General"]}}
|
39 |
+
'''
|
40 |
+
|
41 |
+
json_output_parser = JsonOutputParser()
|
42 |
+
|
43 |
+
# Define the classify_text function
|
44 |
+
def classify_text(text):
|
45 |
+
global llm
|
46 |
+
|
47 |
+
start = time.time()
|
48 |
+
try:
|
49 |
+
lang = detect(text)
|
50 |
+
except:
|
51 |
+
lang = "en"
|
52 |
+
|
53 |
+
prompt_classify = PromptTemplate(
|
54 |
+
template=template_classify,
|
55 |
+
input_variables=["LANG", "TEXT"]
|
56 |
+
)
|
57 |
+
formatted_prompt = prompt_classify.format(TEXT=text, LANG=lang)
|
58 |
+
classify = llm.invoke(formatted_prompt)
|
59 |
+
|
60 |
+
parsed_output = json_output_parser.parse(classify)
|
61 |
+
end = time.time()
|
62 |
+
duration = end - start
|
63 |
+
return lang, parsed_output["Answer"][0], duration
|
64 |
+
|
65 |
+
# Initialize the speech recognition pipeline
|
66 |
+
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
67 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
68 |
+
|
69 |
+
pipe = pipeline(
|
70 |
+
"automatic-speech-recognition",
|
71 |
+
model="openai/whisper-base", # You may want to specify your desired model here
|
72 |
+
torch_dtype=torch_dtype,
|
73 |
+
device=device,
|
74 |
+
)
|
75 |
+
|
76 |
+
def process_audio(audio_path):
|
77 |
+
result = pipe(audio_path)
|
78 |
+
text = result["text"]
|
79 |
+
sentences = re.split(r'[.!?]', text)
|
80 |
+
sentences = [sentence.strip() for sentence in sentences if sentence.strip()]
|
81 |
+
|
82 |
+
classifications = []
|
83 |
+
for sentence in sentences:
|
84 |
+
lang, classification, duration = classify_text(sentence)
|
85 |
+
classifications.append(f"Sentence: {sentence}\nTopic: {classification}\nLanguage: {lang}\nTime: {duration:.2f}s")
|
86 |
+
|
87 |
+
return "\n\n".join(classifications)
|
88 |
+
|
89 |
+
# Create the Gradio interface
|
90 |
+
def create_gradio_interface():
|
91 |
+
with gr.Blocks() as iface:
|
92 |
+
with gr.Tab("Text Input"):
|
93 |
+
text_input = gr.Textbox(label="Text")
|
94 |
+
lang_output = gr.Textbox(label="Detected Language")
|
95 |
+
output_text = gr.Textbox(label="Detected Topics")
|
96 |
+
time_taken = gr.Textbox(label="Time Taken (seconds)")
|
97 |
+
submit_btn = gr.Button("Detect topic")
|
98 |
+
|
99 |
+
def on_text_submit(text):
|
100 |
+
lang, classification, duration = classify_text(text)
|
101 |
+
return lang, classification, f"Time taken: {duration:.2f} seconds"
|
102 |
+
|
103 |
+
submit_btn.click(fn=on_text_submit, inputs=text_input, outputs=[lang_output, output_text, time_taken])
|
104 |
+
|
105 |
+
with gr.Tab("Audio Input"):
|
106 |
+
audio_input = gr.Audio(label="Upload Audio", type="filepath")
|
107 |
+
audio_output = gr.Textbox(label="Detected Topics from Audio")
|
108 |
+
audio_submit_btn = gr.Button("Process Audio")
|
109 |
+
|
110 |
+
audio_submit_btn.click(fn=process_audio, inputs=audio_input, outputs=audio_output)
|
111 |
+
|
112 |
+
iface.launch()
|
113 |
+
|
114 |
+
if __name__ == "__main__":
|
115 |
+
create_gradio_interface()
|