Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,56 +1,49 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
-
from
|
| 4 |
import gradio as gr
|
| 5 |
-
|
| 6 |
-
import
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
|
|
|
| 18 |
|
| 19 |
-
|
| 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 |
-
|
| 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 |
-
|
| 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 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
txtbox = gr.Textbox(
|
| 46 |
-
label="Hindi text output:",
|
| 47 |
-
lines=5
|
| 48 |
-
)
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
|
| 54 |
-
gr.Interface(
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|