Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| from gradio_client import Client | |
| from st_audiorec import st_audiorec | |
| # Constants | |
| TITLE = "70B Parameter Chatbot With Voice" | |
| DESCRIPTION = "" | |
| # Initialize client | |
| with st.sidebar: | |
| system_promptSide = st.text_input("Optional system prompt:") | |
| temperatureSide = st.slider("Temperature", min_value=0.0, max_value=1.0, value=0.9, step=0.05) | |
| max_new_tokensSide = st.slider("Max new tokens", min_value=0.0, max_value=4096.0, value=4096.0, step=64.0) | |
| ToppSide = st.slider("Top-p (nucleus sampling)", min_value=0.0, max_value=1.0, value=0.6, step=0.05) | |
| RepetitionpenaltySide = st.slider("Repetition penalty", min_value=0.0, max_value=2.0, value=1.2, step=0.05) | |
| topk = st.slider("TOP K", min_value=1.0, max_value=1000.0, value=150.0, step=0.05) | |
| whisper_client = Client("https://sanchit-gandhi-whisper-large-v2.hf.space/") | |
| def transcribe(wav_path): | |
| return whisper_client.predict( | |
| wav_path, # str (filepath or URL to file) in 'inputs' Audio component | |
| "transcribe", # str in 'Task' Radio component | |
| api_name="/predict" | |
| ) | |
| # Prediction function | |
| def predict(message, system_prompt='', max_new_tokens=2048, temperature=0.7, Topp=0.5, topk=150, Repetitionpenalty=1.2): | |
| with st.status("Starting client"): | |
| client = Client("https://huggingface-projects-llama-2-7b-chat.hf.space/--replicas/sbx2w/") | |
| st.write("Requesting client") | |
| with st.status("Requesting LLama-2"): | |
| st.write("Requesting API") | |
| response = client.predict( | |
| message, # str in 'Message' Textbox component | |
| system_prompt, # str in 'Optional system prompt' Textbox component | |
| temperature, # int | float (numeric value between 0.0 and 1.0) | |
| max_new_tokens, # int | float (numeric value between 0 and 4096) | |
| Topp, # int | float (numeric value between 0.0 and 1) | |
| topk, # int | float (numeric value between 1 and 1000) in 'Top-k' Slider component | |
| Repetitionpenalty, # int | float (numeric value between 1.0 and 2.0) | |
| api_name="/chat" | |
| ) | |
| st.write("Done") | |
| return response | |
| # Streamlit UI | |
| st.title(TITLE) | |
| st.write(DESCRIPTION) | |
| if "messages" not in st.session_state: | |
| st.session_state.messages = [] | |
| # Display chat messages from history on app rerun | |
| for message in st.session_state.messages: | |
| with st.chat_message(message["role"], avatar=("π§βπ»" if message["role"] == 'human' else 'π¦')): | |
| st.markdown(message["content"]) | |
| textinput = st.chat_input("Ask LLama-2-7b anything...") | |
| wav_audio_data = st_audiorec() | |
| if wav_audio_data != None: | |
| with st.status("Transcribing audio"): | |
| # save audio | |
| with open("audio.wav", "wb") as f: | |
| f.write(wav_audio_data) | |
| prompt = transcribe("audio.wav") | |
| st.write("Transcribed audio") | |
| st.chat_message("human",avatar = "π§βπ»").markdown(prompt) | |
| st.session_state.messages.append({"role": "human", "content": prompt}) | |
| # transcribe audio | |
| response = predict(message= prompt) | |
| with st.chat_message("assistant", avatar='π¦'): | |
| st.markdown(response) | |
| # Add assistant response to chat history | |
| st.session_state.messages.append({"role": "assistant", "content": response}) | |
| # React to user input | |
| if prompt := textinput: | |
| # Display user message in chat message container | |
| st.chat_message("human",avatar = "π§βπ»").markdown(prompt) | |
| # Add user message to chat history | |
| st.session_state.messages.append({"role": "human", "content": prompt}) | |
| response = predict(message=prompt)#, temperature= temperatureSide,max_new_tokens=max_new_tokensSide, Topp=ToppSide,Repetitionpenalty=RepetitionpenaltySide) | |
| # Display assistant response in chat message container | |
| with st.chat_message("assistant", avatar='π¦'): | |
| st.markdown(response) | |
| # Add assistant response to chat history | |
| st.session_state.messages.append({"role": "assistant", "content": response}) | |