Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import speech_recognition as sr
|
| 3 |
+
from huggingface_hub import InferenceClient
|
| 4 |
+
import random
|
| 5 |
+
import textwrap
|
| 6 |
+
import pyttsx3
|
| 7 |
+
|
| 8 |
+
# Initialize the speech recognition and TTS engine
|
| 9 |
+
recognizer = sr.Recognizer()
|
| 10 |
+
tts_engine = pyttsx3.init()
|
| 11 |
+
|
| 12 |
+
# Define the model to be used
|
| 13 |
+
model = "mistralai/Mixtral-8x7B-Instruct-v0.1"
|
| 14 |
+
client = InferenceClient(model)
|
| 15 |
+
|
| 16 |
+
# Embedded system prompt
|
| 17 |
+
system_prompt_text = (
|
| 18 |
+
"You are a smart and helpful co-worker of Thailand based multi-national company PTT, "
|
| 19 |
+
"and PTTEP. You help with any kind of request and provide a detailed answer to the question. "
|
| 20 |
+
"But if you are asked about something unethical or dangerous, you must refuse and provide a safe and respectful way to handle that."
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
# Read the content of the info.md file with UTF-8 encoding
|
| 24 |
+
with open("info.md", "r", encoding="utf-8") as file:
|
| 25 |
+
info_md_content = file.read()
|
| 26 |
+
|
| 27 |
+
# Chunk the info.md content into smaller sections
|
| 28 |
+
chunk_size = 2500 # Adjust this size as needed
|
| 29 |
+
info_md_chunks = textwrap.wrap(info_md_content, chunk_size)
|
| 30 |
+
|
| 31 |
+
def get_all_chunks(chunks):
|
| 32 |
+
return "\n\n".join(chunks)
|
| 33 |
+
|
| 34 |
+
def format_prompt_mixtral(message, history, info_md_chunks):
|
| 35 |
+
prompt = "<s>"
|
| 36 |
+
all_chunks = get_all_chunks(info_md_chunks)
|
| 37 |
+
prompt += f"{all_chunks}\n\n" # Add all chunks of info.md at the beginning
|
| 38 |
+
prompt += f"{system_prompt_text}\n\n" # Add the system prompt
|
| 39 |
+
|
| 40 |
+
if history:
|
| 41 |
+
for user_prompt, bot_response in history:
|
| 42 |
+
prompt += f"[INST] {user_prompt} [/INST]"
|
| 43 |
+
prompt += f" {bot_response}</s> "
|
| 44 |
+
prompt += f"[INST] {message} [/INST]"
|
| 45 |
+
return prompt
|
| 46 |
+
|
| 47 |
+
def chat_inf(prompt, history, seed, temp, tokens, top_p, rep_p):
|
| 48 |
+
generate_kwargs = dict(
|
| 49 |
+
temperature=temp,
|
| 50 |
+
max_new_tokens=tokens,
|
| 51 |
+
top_p=top_p,
|
| 52 |
+
repetition_penalty=rep_p,
|
| 53 |
+
do_sample=True,
|
| 54 |
+
seed=seed,
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
formatted_prompt = format_prompt_mixtral(prompt, history, info_md_chunks)
|
| 58 |
+
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
| 59 |
+
output = ""
|
| 60 |
+
for response in stream:
|
| 61 |
+
output += response.token.text
|
| 62 |
+
yield [(prompt, output)]
|
| 63 |
+
history.append((prompt, output))
|
| 64 |
+
yield history
|
| 65 |
+
|
| 66 |
+
def clear_fn():
|
| 67 |
+
return None, None
|
| 68 |
+
|
| 69 |
+
rand_val = random.randint(1, 1111111111111111)
|
| 70 |
+
|
| 71 |
+
def check_rand(inp, val):
|
| 72 |
+
if inp:
|
| 73 |
+
return gr.Slider(label="Seed", minimum=1, maximum=1111111111111111, value=random.randint(1, 1111111111111111))
|
| 74 |
+
else:
|
| 75 |
+
return gr.Slider(label="Seed", minimum=1, maximum=1111111111111111, value=int(val))
|
| 76 |
+
|
| 77 |
+
def recognize_speech(audio):
|
| 78 |
+
with sr.AudioFile(audio) as source:
|
| 79 |
+
audio_data = recognizer.record(source) # Record the audio
|
| 80 |
+
try:
|
| 81 |
+
# Recognize the speech using Google's API
|
| 82 |
+
text = recognizer.recognize_google(audio_data)
|
| 83 |
+
return text
|
| 84 |
+
except sr.UnknownValueError:
|
| 85 |
+
return "Sorry, I could not understand the audio."
|
| 86 |
+
except sr.RequestError:
|
| 87 |
+
return "Error: Could not request results from the speech recognition service."
|
| 88 |
+
|
| 89 |
+
def speak_text(text):
|
| 90 |
+
# Convert text to speech using pyttsx3
|
| 91 |
+
tts_engine.save_to_file(text, 'output.mp3') # Save the TTS audio
|
| 92 |
+
tts_engine.runAndWait() # Wait until TTS is done
|
| 93 |
+
|
| 94 |
+
with gr.Blocks() as app:
|
| 95 |
+
gr.HTML("""<center><h1 style='font-size:xx-large;'>PTT Chatbot</h1><br><h3>running on Huggingface Inference</h3><br><h7>EXPERIMENTAL</center>""")
|
| 96 |
+
|
| 97 |
+
with gr.Row():
|
| 98 |
+
chat = gr.Chatbot(height=500)
|
| 99 |
+
|
| 100 |
+
with gr.Group():
|
| 101 |
+
with gr.Row():
|
| 102 |
+
with gr.Column(scale=3):
|
| 103 |
+
inp = gr.Audio(type="filepath") # Audio input
|
| 104 |
+
with gr.Row():
|
| 105 |
+
with gr.Column(scale=2):
|
| 106 |
+
btn = gr.Button("Chat")
|
| 107 |
+
with gr.Column(scale=1):
|
| 108 |
+
with gr.Group():
|
| 109 |
+
stop_btn = gr.Button("Stop")
|
| 110 |
+
clear_btn = gr.Button("Clear")
|
| 111 |
+
with gr.Column(scale=1):
|
| 112 |
+
with gr.Group():
|
| 113 |
+
rand = gr.Checkbox(label="Random Seed", value=True)
|
| 114 |
+
seed = gr.Slider(label="Seed", minimum=1, maximum=1111111111111111, step=1, value=rand_val)
|
| 115 |
+
tokens = gr.Slider(label="Max new tokens", value=3840, minimum=0, maximum=8000, step=64, interactive=True, visible=True, info="The maximum number of tokens")
|
| 116 |
+
temp = gr.Slider(label="Temperature", step=0.01, minimum=0.01, maximum=1.0, value=0.9)
|
| 117 |
+
top_p = gr.Slider(label="Top-P", step=0.01, minimum=0.01, maximum=1.0, value=0.9)
|
| 118 |
+
rep_p = gr.Slider(label="Repetition Penalty", step=0.1, minimum=0.1, maximum=2.0, value=1.0)
|
| 119 |
+
|
| 120 |
+
hid1 = gr.Number(value=1, visible=False)
|
| 121 |
+
|
| 122 |
+
output_audio = gr.Audio(label="Output Audio", type="filepath", interactive=False) # Create an output audio component
|
| 123 |
+
|
| 124 |
+
def handle_chat(audio_input, chat_history, seed, temp, tokens, top_p, rep_p):
|
| 125 |
+
user_message = recognize_speech(audio_input) # Recognize speech input
|
| 126 |
+
if "Sorry" in user_message: # Check for error in recognition
|
| 127 |
+
return chat_history, user_message, None
|
| 128 |
+
response_gen = chat_inf(user_message, chat_history, seed, temp, tokens, top_p, rep_p)
|
| 129 |
+
response = next(response_gen)[0][-1][1] # Get the response text
|
| 130 |
+
speak_text(response) # Speak the response text
|
| 131 |
+
return chat_history + [(user_message, response)], response, 'output.mp3' # Return the filename for audio output
|
| 132 |
+
|
| 133 |
+
go = btn.click(handle_chat, [inp, chat, seed, temp, tokens, top_p, rep_p], [chat, inp, output_audio]) # Use output_audio instead of "output.mp3"
|
| 134 |
+
|
| 135 |
+
stop_btn.click(None, None, None, cancels=[go])
|
| 136 |
+
clear_btn.click(clear_fn, None, [inp, chat])
|
| 137 |
+
|
| 138 |
+
app.queue(default_concurrency_limit=10).launch(share=True, auth=("admin", "0112358"))
|
| 139 |
+
|