Hunzla commited on
Commit
4d4c631
1 Parent(s): d94e09f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+ from langchain.vectorstores import FAISS
3
+ from langchain.embeddings import HuggingFaceEmbeddings
4
+
5
+ pipe = pipeline("automatic-speech-recognition", model="openai/whisper-large-v2")
6
+ import json
7
+ with open("tasks.json", "r",encoding="utf-8") as json_file:
8
+ global data
9
+ data = json.load(json_file)
10
+ def find_index(sentence):
11
+ global data
12
+ for key, value in data.items():
13
+ for i,j in value.items():
14
+ for s in j:
15
+ if sentence == s:
16
+ return i
17
+ for x,item in data.items():
18
+ texts = []
19
+ for key,value in item.items():
20
+ for each in value:
21
+ print(find_index(each))
22
+ texts.append(each)
23
+ globals()[f"faiss_{x}"] = FAISS.from_texts(texts,HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2",model_kwargs={'device':'cpu'}))
24
+
25
+ from fastapi import FastAPI
26
+ app = FastAPI()
27
+ @app.get("/")
28
+ def transcribe_the_command(audio_path,state):
29
+ transcript = pipe(audio_path)["text"]
30
+ similar = globals()[f"faiss_{state}"].similarity_search(transcript)[0].page_content
31
+ print(similar)
32
+ reply = find_index(similar)
33
+ return reply
34
+ import gradio as gr
35
+ iface = gr.Interface(
36
+ fn=transcribe_the_command,
37
+ inputs=[gr.Textbox(),gr.Textbox()],
38
+ outputs="text",
39
+ title="Whisper Small",
40
+ description="Realtime demo for intent recognition using a Whisper small model.",
41
+ )
42
+
43
+ iface.launch(share="true")