Spaces:
Sleeping
Sleeping
init
Browse files- app.py +48 -15
- requirements.txt +2 -0
app.py
CHANGED
@@ -1,8 +1,12 @@
|
|
1 |
from huggingface_hub import InferenceClient
|
|
|
2 |
import gradio as gr
|
3 |
|
4 |
-
|
5 |
-
|
|
|
|
|
|
|
6 |
|
7 |
|
8 |
def format_prompt(message, history):
|
@@ -14,12 +18,14 @@ def format_prompt(message, history):
|
|
14 |
return prompt
|
15 |
|
16 |
|
17 |
-
def generate(prompt, history, system_prompt, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0,):
|
18 |
temperature = float(temperature)
|
19 |
if temperature < 1e-2:
|
20 |
temperature = 1e-2
|
21 |
top_p = float(top_p)
|
22 |
|
|
|
|
|
23 |
generate_kwargs = dict(
|
24 |
temperature=temperature,
|
25 |
max_new_tokens=max_new_tokens,
|
@@ -29,6 +35,13 @@ def generate(prompt, history, system_prompt, temperature=0.9, max_new_tokens=256
|
|
29 |
seed=42,
|
30 |
)
|
31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
|
33 |
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True,
|
34 |
return_full_text=False)
|
@@ -37,9 +50,33 @@ def generate(prompt, history, system_prompt, temperature=0.9, max_new_tokens=256
|
|
37 |
for response in stream:
|
38 |
output += response.token.text
|
39 |
yield output
|
|
|
|
|
|
|
|
|
|
|
40 |
return output
|
41 |
|
42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
additional_inputs = [
|
44 |
gr.Textbox(
|
45 |
label="System Prompt",
|
@@ -84,19 +121,15 @@ additional_inputs = [
|
|
84 |
)
|
85 |
]
|
86 |
|
87 |
-
|
88 |
-
["Can you write a short story about a time-traveling detective who solves historical mysteries?", None, None, None, None, None,],
|
89 |
-
["I'm trying to learn French. Can you provide some common phrases that would be useful for a beginner, along with their pronunciations?", None, None, None, None, None,],
|
90 |
-
["I have chicken, rice, and bell peppers in my kitchen. Can you suggest an easy recipe I can make with these ingredients?", None, None, None, None, None,],
|
91 |
-
["Can you explain how the QuickSort algorithm works and provide a Python implementation?", None, None, None, None, None,],
|
92 |
-
["What are some unique features of Rust that make it stand out compared to other systems programming languages like C++?", None, None, None, None, None,],
|
93 |
-
]
|
94 |
-
|
95 |
-
gr.ChatInterface(
|
96 |
fn=generate,
|
97 |
-
chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True,
|
|
|
98 |
additional_inputs=additional_inputs,
|
99 |
-
title="Mixtral 46.7B
|
|
|
100 |
examples=examples,
|
101 |
concurrency_limit=20,
|
102 |
-
)
|
|
|
|
|
|
1 |
from huggingface_hub import InferenceClient
|
2 |
+
from typing import List
|
3 |
import gradio as gr
|
4 |
|
5 |
+
model_llm_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
|
6 |
+
# model_toxicity_id = "unitary/toxic-bert"
|
7 |
+
model_toxicity_id = "unitary/unbiased-toxic-roberta"
|
8 |
+
client = InferenceClient(model_llm_id)
|
9 |
+
client_toxicity = InferenceClient(model_toxicity_id)
|
10 |
|
11 |
|
12 |
def format_prompt(message, history):
|
|
|
18 |
return prompt
|
19 |
|
20 |
|
21 |
+
def generate(prompt, history, system_prompt, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0, ):
|
22 |
temperature = float(temperature)
|
23 |
if temperature < 1e-2:
|
24 |
temperature = 1e-2
|
25 |
top_p = float(top_p)
|
26 |
|
27 |
+
start_toxic = "[Toxic Classification] "
|
28 |
+
|
29 |
generate_kwargs = dict(
|
30 |
temperature=temperature,
|
31 |
max_new_tokens=max_new_tokens,
|
|
|
35 |
seed=42,
|
36 |
)
|
37 |
|
38 |
+
response: List = client_toxicity.text_classification(prompt)
|
39 |
+
toxicity_level = [f'{d["label"]}:{round(d["score"], 4)}' for d in response]
|
40 |
+
|
41 |
+
if start_toxic in history:
|
42 |
+
s_idx = history.find(start_toxic)
|
43 |
+
history = history[:s_idx]
|
44 |
+
|
45 |
formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
|
46 |
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True,
|
47 |
return_full_text=False)
|
|
|
50 |
for response in stream:
|
51 |
output += response.token.text
|
52 |
yield output
|
53 |
+
|
54 |
+
output += ' \n \n'
|
55 |
+
output += start_toxic + " | ".join(toxicity_level)
|
56 |
+
yield output
|
57 |
+
|
58 |
return output
|
59 |
|
60 |
|
61 |
+
examples = [
|
62 |
+
["Are you using Detoxify to measure the toxicity?", None,
|
63 |
+
None, None, None, None, ],
|
64 |
+
[
|
65 |
+
"I'm planning a vacation to Japan. Can you suggest a one-week itinerary including must-visit places and local cuisines to try?",
|
66 |
+
None, None, None, None, None, ],
|
67 |
+
[
|
68 |
+
"I'm trying to learn French. Can you provide some common phrases that would be useful for a beginner, along with their pronunciations?",
|
69 |
+
None, None, None, None, None, ],
|
70 |
+
[
|
71 |
+
"I have chicken, rice, and bell peppers in my kitchen. Can you suggest an easy recipe I can make with these ingredients?",
|
72 |
+
None, None, None, None, None, ],
|
73 |
+
["Can you explain how the QuickSort algorithm works and provide a Python implementation?", None, None, None,
|
74 |
+
None, None, ],
|
75 |
+
[
|
76 |
+
"What are some unique features of Rust that make it stand out compared to other systems programming languages like C++?",
|
77 |
+
None, None, None, None, None, ],
|
78 |
+
]
|
79 |
+
|
80 |
additional_inputs = [
|
81 |
gr.Textbox(
|
82 |
label="System Prompt",
|
|
|
121 |
)
|
122 |
]
|
123 |
|
124 |
+
demo = gr.ChatInterface(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
fn=generate,
|
126 |
+
chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True,
|
127 |
+
layout="panel"),
|
128 |
additional_inputs=additional_inputs,
|
129 |
+
title="Mixtral 46.7B",
|
130 |
+
description="Mixtral with Toxic comment classification",
|
131 |
examples=examples,
|
132 |
concurrency_limit=20,
|
133 |
+
)
|
134 |
+
|
135 |
+
demo.launch(show_api=False)
|
requirements.txt
CHANGED
@@ -1,3 +1,5 @@
|
|
|
|
|
|
1 |
torch
|
2 |
huggingface_hub
|
3 |
transformers
|
|
|
1 |
+
setuptools
|
2 |
+
wheel
|
3 |
torch
|
4 |
huggingface_hub
|
5 |
transformers
|