Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
from datasets import load_dataset
|
|
|
|
| 4 |
|
| 5 |
"""
|
| 6 |
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
|
|
@@ -8,13 +9,12 @@ For more information on `huggingface_hub` Inference API support, please check th
|
|
| 8 |
|
| 9 |
#Update: Using a new base model
|
| 10 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 11 |
-
#client = InferenceClient("HuggingFaceH4/zephyr-7b-gemma-v0.1")
|
| 12 |
-
#topic_model = BERTopic.load("MaartenGr/BERTopic_Wikipedia")
|
| 13 |
-
# Train model
|
| 14 |
-
#topic_model = BERTopic("english")
|
| 15 |
-
#topics, probs = topic_model.fit_transform(docs)
|
| 16 |
dataset = load_dataset("JustKiddo/KiddosVault")
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
def respond(
|
| 19 |
message,
|
| 20 |
history: list[tuple[str, str]],
|
|
@@ -47,10 +47,24 @@ def respond(
|
|
| 47 |
response += token
|
| 48 |
yield response
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
"""
|
| 51 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 52 |
"""
|
| 53 |
-
|
|
|
|
| 54 |
respond,
|
| 55 |
additional_inputs=[
|
| 56 |
gr.Textbox(value="You are a professional Mental Healthcare Chatbot.", label="System message"),
|
|
@@ -66,6 +80,21 @@ demo = gr.ChatInterface(
|
|
| 66 |
],
|
| 67 |
)
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|
| 71 |
demo.launch(debug=True)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
from datasets import load_dataset
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 5 |
|
| 6 |
"""
|
| 7 |
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
|
|
|
|
| 9 |
|
| 10 |
#Update: Using a new base model
|
| 11 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
dataset = load_dataset("JustKiddo/KiddosVault")
|
| 13 |
|
| 14 |
+
# Load the tokenizer and model for token display
|
| 15 |
+
tokenizer = AutoTokenizer.from_pretrained("t5-small") #Google's T5 Model
|
| 16 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("t5-small")
|
| 17 |
+
|
| 18 |
def respond(
|
| 19 |
message,
|
| 20 |
history: list[tuple[str, str]],
|
|
|
|
| 47 |
response += token
|
| 48 |
yield response
|
| 49 |
|
| 50 |
+
#My custom token generator
|
| 51 |
+
def generate_tokens(text):
|
| 52 |
+
input = tokenizer(text, return_tensors="pt")
|
| 53 |
+
output = model.generate(**input)
|
| 54 |
+
|
| 55 |
+
input_ids = input["input_ids"].tolist()[0]
|
| 56 |
+
output_ids = output.tolist()[0]
|
| 57 |
+
|
| 58 |
+
input_tokens_str = tokenizer.convert_ids_to_tokens(input_ids)
|
| 59 |
+
output_tokens_str = tokenizer.convert_ids_to_tokens(output_ids)
|
| 60 |
+
|
| 61 |
+
return " ".join(input_tokens_str), " ".join(output_tokens_str)
|
| 62 |
+
|
| 63 |
"""
|
| 64 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 65 |
"""
|
| 66 |
+
|
| 67 |
+
chatInterface = gr.ChatInterface(
|
| 68 |
respond,
|
| 69 |
additional_inputs=[
|
| 70 |
gr.Textbox(value="You are a professional Mental Healthcare Chatbot.", label="System message"),
|
|
|
|
| 80 |
],
|
| 81 |
)
|
| 82 |
|
| 83 |
+
with gr.Blocks() as demo:
|
| 84 |
+
with gr.Row():
|
| 85 |
+
chatInterface
|
| 86 |
+
with gr.Column():
|
| 87 |
+
input_text = gr.Textbox(label="Input text")
|
| 88 |
+
input_tokens = gr.Textbox(label="Input tokens")
|
| 89 |
+
output_tokens = gr.Textbox(label="Output tokens")
|
| 90 |
+
|
| 91 |
+
def update_tokens(input_text):
|
| 92 |
+
input_tokens_str, output_tokens_str = generate_tokens(input_text)
|
| 93 |
+
return input_tokens_str, output_tokens_str
|
| 94 |
+
|
| 95 |
+
input_text.change(update_tokens,
|
| 96 |
+
inputs=input_text,
|
| 97 |
+
output_tokens=[input_tokens, output_tokens])
|
| 98 |
|
| 99 |
if __name__ == "__main__":
|
| 100 |
demo.launch(debug=True)
|