brurei commited on
Commit
ef0dcda
1 Parent(s): 1a48b81

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -41
app.py CHANGED
@@ -1,56 +1,49 @@
1
- import soundfile as sf
2
- import torch
3
- from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
4
  import gradio as gr
5
- #import sox
6
- import subprocess
7
 
 
8
 
9
- def read_file_and_process(wav_file):
10
- filename = wav_file.split('.')[0]
11
- filename_16k = filename + "16k.wav"
12
- resampler(wav_file, filename_16k)
13
- speech, _ = sf.read(filename_16k)
14
- inputs = processor(speech, sampling_rate=16_000, return_tensors="pt", padding=True)
15
 
16
- return inputs
 
 
 
 
17
 
 
18
 
19
- def resampler(input_file_path, output_file_path):
20
- command = (
21
- f"ffmpeg -hide_banner -loglevel panic -i {input_file_path} -ar 16000 -ac 1 -bits_per_raw_sample 16 -vn "
22
- f"{output_file_path}"
23
- )
24
- subprocess.call(command, shell=True)
25
 
 
26
 
27
- def parse_transcription(logits):
28
- predicted_ids = torch.argmax(logits, dim=-1)
29
- transcription = processor.decode(predicted_ids[0], skip_special_tokens=True)
30
- return transcription
31
 
 
32
 
33
- def parse(wav_file):
34
- input_values = read_file_and_process(wav_file)
35
- with torch.no_grad():
36
- logits = model(**input_values).logits
37
- return parse_transcription(logits)
38
 
39
 
40
- model_id = "Harveenchadha/vakyansh-wav2vec2-hindi-him-4200"
41
- processor = Wav2Vec2Processor.from_pretrained(model_id)
42
- model = Wav2Vec2ForCTC.from_pretrained(model_id)
 
43
 
44
- input_ = gr.Audio(source="microphone", type="filepath")
45
- txtbox = gr.Textbox(
46
- label="Hindi text output:",
47
- lines=5
48
- )
49
 
50
- title = "Speech-to-Text (Hindi) using Vakyansh"
51
- description = "Upload a hindi audio clip, and let AI do the hard work of transcribing."
52
- article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2104.06678'>Large-Scale Self- and Semi-Supervised Learning for Speech Translation</a></p>"
53
 
54
- gr.Interface(parse, inputs=input_, outputs=txtbox, title=title, description=description, article=article,
55
- streaming=True, interactive=True,
56
- analytics_enabled=False, show_tips=False, enable_queue=True).launch(inline=False,share=True);
 
 
 
 
 
 
 
1
+ # coding=utf8
2
+ from gpt_index import SimpleDirectoryReader, GPTListIndex, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
3
+ from langchain import OpenAI
4
  import gradio as gr
5
+ import sys
6
+ import os
7
 
8
+ os.environ["OPENAI_API_KEY"] = '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'
9
 
 
 
 
 
 
 
10
 
11
+ def construct_index(directory_path):
12
+ max_input_size = 4096
13
+ num_outputs = 512
14
+ max_chunk_overlap = 20
15
+ chunk_size_limit = 600
16
 
17
+ prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
18
 
19
+ llm_predictor = LLMPredictor(llm=OpenAI(temperature=0.7, model_name="text-davinci-003", max_tokens=num_outputs))
 
 
 
 
 
20
 
21
+ documents = SimpleDirectoryReader(directory_path).load_data()
22
 
23
+ index = GPTSimpleVectorIndex(documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper)
 
 
 
24
 
25
+ index.save_to_disk('index.json')
26
 
27
+ return index
 
 
 
 
28
 
29
 
30
+ def chatbot(input_text):
31
+ index = GPTSimpleVectorIndex.load_from_disk('index.json')
32
+ response = index.query(input_text, response_mode="compact")
33
+ return response.response
34
 
35
+ description = """
 
 
 
 
36
 
37
+ <center>Olá sou a Zoh, fui treinada para responder perguntas com base das informações do Hippo Supermercados. Pergunte qualquer coisa. Caso eu não saiba, estarei aprendendo.
38
+ <img src="https://s3.amazonaws.com/enlizt-resources-prod/companies/10958750-6306-11ea-b31c-2b332181af51_256_avatar?nocache=1588599205314" width=200px></center>
39
+ """
40
 
41
+ iface = gr.Interface(fn=chatbot,
42
+ inputs=gr.inputs.Textbox(lines=3, label='O quê gostaria de saber?') ,
43
+ outputs=gr.inputs.Textbox(lines=3, label="Veja o que encontrei"),
44
+ description=description,
45
+ css=".gradio-container-3-23-0 {background-color: #5f0000} .gradio-container-3-23-0 .prose * {color: #ffffff}",
46
+ title="CD2 IA")
47
+
48
+ index = construct_index(".")
49
+ iface.launch(share=True, show_error=True)