import gradio as gr from huggingface_hub import InferenceClient """ For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference """ client = InferenceClient("fadodr/finetuned_mental_health_therapy_original") def respond( message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p, ): print("Message:", message) print("History:", history) print("System Message:", system_message) print("Max Tokens:", max_tokens) print("Temperature:", temperature) print("Top-p:", top_p) print(dir(client)) try: messages = [{"role": "instruction", "content": system_message}] for val in history: if val[0]: messages.append({"role": "input", "content": val[0]}) if val[1]: messages.append({"role": "response", "content": val[1]}) messages.append({"role": "input", "content": message}) response = "" print("sending message") print(messages) for message in client.text_generation( messages, stream=True, temperature=temperature, top_p=top_p, ): print(message) token = message.choices[0].delta.content[len(messages):] response += token yield response except Exception as e: print(e) # from transformers import pipeline, BitsAndBytesConfig # config = BitsAndBytesConfig(load_in_4bit=True) # # Load the pipeline with your custom model # generator = pipeline('text-generation', model='fadodr/finetuned_mental_health_therapy_original', quantization_config=config) # # Generate text based on the input message # def respond(message, history, system_message, max_tokens, temperature, top_p): # prompt = f""" # ### Input: # {message} # ### Instruction: # {system_message} # ### Response: # """ # response = generator(prompt, max_length=max_tokens, temperature=temperature, top_p=top_p) # print(response) # yield response[0]['generated_text'] """ For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface """ demo = gr.ChatInterface( respond, additional_inputs=[ gr.Textbox(value="I need your help as a mental health therapist", label="System message"), gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"), gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"), gr.Slider( minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)", ), ], ) if __name__ == "__main__": demo.launch()