Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,60 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
"""
|
| 7 |
-
client = InferenceClient("Canstralian/RedTeamAI")
|
| 8 |
|
| 9 |
|
| 10 |
def respond(
|
| 11 |
-
message,
|
| 12 |
-
history:
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
):
|
|
|
|
| 18 |
messages = [{"role": "system", "content": system_message}]
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
messages.append({"role": "user", "content": message})
|
| 27 |
|
|
|
|
| 28 |
response = ""
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
messages,
|
| 32 |
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
temperature=temperature,
|
| 35 |
top_p=top_p,
|
|
|
|
| 36 |
):
|
| 37 |
-
|
| 38 |
-
|
| 39 |
response += token
|
| 40 |
-
yield response
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
gr.
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
],
|
|
|
|
|
|
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
from typing import List, Tuple
|
| 4 |
|
| 5 |
+
# Initialize the Inference Client with the Canstralian/redteamai model
|
| 6 |
+
client = InferenceClient("Canstralian/redteamai")
|
|
|
|
|
|
|
| 7 |
|
| 8 |
|
| 9 |
def respond(
|
| 10 |
+
message: str,
|
| 11 |
+
history: List[Tuple[str, str]],
|
| 12 |
+
system_message: str,
|
| 13 |
+
max_tokens: int,
|
| 14 |
+
temperature: float,
|
| 15 |
+
top_p: float,
|
| 16 |
):
|
| 17 |
+
# Start with the system message in the conversation history
|
| 18 |
messages = [{"role": "system", "content": system_message}]
|
| 19 |
+
|
| 20 |
+
# Add the conversation history to the message
|
| 21 |
+
for user_message, assistant_reply in history:
|
| 22 |
+
if user_message:
|
| 23 |
+
messages.append({"role": "user", "content": user_message})
|
| 24 |
+
if assistant_reply:
|
| 25 |
+
messages.append({"role": "assistant", "content": assistant_reply})
|
| 26 |
+
|
| 27 |
+
# Add the current user message
|
| 28 |
messages.append({"role": "user", "content": message})
|
| 29 |
|
| 30 |
+
# Create the API request
|
| 31 |
response = ""
|
| 32 |
+
for result in client.chat_completion(
|
| 33 |
+
messages=messages,
|
|
|
|
| 34 |
max_tokens=max_tokens,
|
|
|
|
| 35 |
temperature=temperature,
|
| 36 |
top_p=top_p,
|
| 37 |
+
stream=True # Enable streaming for real-time responses
|
| 38 |
):
|
| 39 |
+
# Extract and accumulate the response as it streams
|
| 40 |
+
token = result['choices'][0]['delta']['content']
|
| 41 |
response += token
|
| 42 |
+
yield response # Yield response as it's generated
|
| 43 |
+
|
| 44 |
+
# Create the Gradio interface
|
| 45 |
+
demo = gr.Interface(
|
| 46 |
+
fn=respond,
|
| 47 |
+
inputs=[
|
| 48 |
+
gr.Textbox(label="User Message", placeholder="Enter your message here..."),
|
| 49 |
+
gr.State(default=[], label="Chat History"), # State for chat history
|
| 50 |
+
gr.Textbox(value="You are a friendly chatbot.", label="System Message"),
|
| 51 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max Tokens"),
|
|
|
|
| 52 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 53 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (Nucleus Sampling)"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
],
|
| 55 |
+
outputs=gr.Textbox(label="Assistant Response"),
|
| 56 |
+
live=True, # Enable real-time updating of the response
|
| 57 |
)
|
| 58 |
|
|
|
|
| 59 |
if __name__ == "__main__":
|
| 60 |
+
demo.launch()
|