Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,61 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
"""
|
| 5 |
-
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
|
| 6 |
-
"""
|
| 7 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def respond(
|
| 11 |
-
message,
|
| 12 |
-
history: list[tuple[str, str]],
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
top_p,
|
| 17 |
):
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
|
|
|
|
|
|
|
|
|
| 20 |
for val in history:
|
| 21 |
if val[0]:
|
| 22 |
messages.append({"role": "user", "content": val[0]})
|
| 23 |
if val[1]:
|
| 24 |
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
-
|
| 26 |
messages.append({"role": "user", "content": message})
|
| 27 |
-
|
| 28 |
response = ""
|
| 29 |
-
|
| 30 |
for message in client.chat_completion(
|
| 31 |
messages,
|
| 32 |
max_tokens=max_tokens,
|
|
@@ -35,30 +64,39 @@ def respond(
|
|
| 35 |
top_p=top_p,
|
| 36 |
):
|
| 37 |
token = message.choices[0].delta.content
|
| 38 |
-
|
| 39 |
response += token
|
| 40 |
yield response
|
| 41 |
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
demo = gr.ChatInterface(
|
| 47 |
respond,
|
| 48 |
additional_inputs=[
|
| 49 |
-
gr.Textbox(
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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(share=True)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
|
|
|
|
|
|
|
|
|
| 4 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 5 |
|
| 6 |
+
def check_custom_responses(message: str) -> str:
|
| 7 |
+
"""Check for specific patterns and return custom responses."""
|
| 8 |
+
# Convert message to lowercase for case-insensitive matching
|
| 9 |
+
message_lower = message.lower()
|
| 10 |
+
|
| 11 |
+
# Dictionary of custom responses
|
| 12 |
+
custom_responses = {
|
| 13 |
+
"what is ur name?": "xylaria",
|
| 14 |
+
"what is your name?": "xylaria",
|
| 15 |
+
"what's your name?": "xylaria",
|
| 16 |
+
"whats your name": "xylaria",
|
| 17 |
+
"how many 'r' is in strawberry?": "3"
|
| 18 |
+
"who is your developer?": "sk md saad amin"
|
| 19 |
+
"how many r is in strawberry": "3"
|
| 20 |
+
"who is ur dev": "sk md saad amin"
|
| 21 |
+
"who is ur developer": "sk md saad amin"
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
# Check if message matches any custom patterns
|
| 27 |
+
for pattern, response in custom_responses.items():
|
| 28 |
+
if pattern in message_lower:
|
| 29 |
+
return response
|
| 30 |
+
|
| 31 |
+
return None
|
| 32 |
|
| 33 |
def respond(
|
| 34 |
+
message,
|
| 35 |
+
history: list[tuple[str, str]],
|
| 36 |
+
system_message,
|
| 37 |
+
max_tokens,
|
| 38 |
+
temperature,
|
| 39 |
top_p,
|
| 40 |
):
|
| 41 |
+
# First check for custom responses
|
| 42 |
+
custom_response = check_custom_responses(message)
|
| 43 |
+
if custom_response:
|
| 44 |
+
yield custom_response
|
| 45 |
+
return
|
| 46 |
|
| 47 |
+
# If no custom response, proceed with normal chat completion
|
| 48 |
+
messages = [{"role": "system", "content": system_message}]
|
| 49 |
+
|
| 50 |
for val in history:
|
| 51 |
if val[0]:
|
| 52 |
messages.append({"role": "user", "content": val[0]})
|
| 53 |
if val[1]:
|
| 54 |
messages.append({"role": "assistant", "content": val[1]})
|
| 55 |
+
|
| 56 |
messages.append({"role": "user", "content": message})
|
| 57 |
+
|
| 58 |
response = ""
|
|
|
|
| 59 |
for message in client.chat_completion(
|
| 60 |
messages,
|
| 61 |
max_tokens=max_tokens,
|
|
|
|
| 64 |
top_p=top_p,
|
| 65 |
):
|
| 66 |
token = message.choices[0].delta.content
|
|
|
|
| 67 |
response += token
|
| 68 |
yield response
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
demo = gr.ChatInterface(
|
| 71 |
respond,
|
| 72 |
additional_inputs=[
|
| 73 |
+
gr.Textbox(
|
| 74 |
+
value="You are a friendly Chatbot.",
|
| 75 |
+
label="System message"
|
| 76 |
+
),
|
| 77 |
+
gr.Slider(
|
| 78 |
+
minimum=1,
|
| 79 |
+
maximum=2048,
|
| 80 |
+
value=512,
|
| 81 |
+
step=1,
|
| 82 |
+
label="Max new tokens"
|
| 83 |
+
),
|
| 84 |
+
gr.Slider(
|
| 85 |
+
minimum=0.1,
|
| 86 |
+
maximum=4.0,
|
| 87 |
+
value=0.7,
|
| 88 |
+
step=0.1,
|
| 89 |
+
label="Temperature"
|
| 90 |
+
),
|
| 91 |
gr.Slider(
|
| 92 |
minimum=0.1,
|
| 93 |
maximum=1.0,
|
| 94 |
value=0.95,
|
| 95 |
step=0.05,
|
| 96 |
+
label="Top-p (nucleus sampling)"
|
| 97 |
),
|
| 98 |
+
]
|
| 99 |
)
|
| 100 |
|
|
|
|
| 101 |
if __name__ == "__main__":
|
| 102 |
demo.launch(share=True)
|