Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,45 +4,47 @@ from huggingface_hub import InferenceClient
|
|
| 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("cognitivecomputations/TinyDolphin-2.8.2-1.1b-laser")
|
| 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 |
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
for
|
| 21 |
-
if
|
| 22 |
-
messages.append({"role": "user", "content":
|
| 23 |
-
if
|
| 24 |
-
messages.append({"role": "assistant", "content":
|
| 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,
|
| 33 |
stream=True,
|
| 34 |
temperature=temperature,
|
| 35 |
top_p=top_p,
|
| 36 |
):
|
| 37 |
-
token = message
|
| 38 |
-
|
| 39 |
response += token
|
| 40 |
yield response
|
| 41 |
|
| 42 |
-
|
| 43 |
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the
|
| 45 |
"""
|
|
|
|
| 46 |
demo = gr.ChatInterface(
|
| 47 |
respond,
|
| 48 |
additional_inputs=[
|
|
@@ -59,6 +61,5 @@ demo = gr.ChatInterface(
|
|
| 59 |
],
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
if __name__ == "__main__":
|
| 64 |
demo.launch()
|
|
|
|
| 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 |
+
# Initialize the inference client with the model repo
|
| 8 |
client = InferenceClient("cognitivecomputations/TinyDolphin-2.8.2-1.1b-laser")
|
| 9 |
|
|
|
|
| 10 |
def respond(
|
| 11 |
+
message: str,
|
| 12 |
history: list[tuple[str, str]],
|
| 13 |
+
system_message: str,
|
| 14 |
+
max_tokens: int,
|
| 15 |
+
temperature: float,
|
| 16 |
+
top_p: float,
|
| 17 |
):
|
| 18 |
+
"""Generate a response for the chatbot using the InferenceClient."""
|
| 19 |
+
# Prepare the messages in the correct format for the API
|
| 20 |
messages = [{"role": "system", "content": system_message}]
|
| 21 |
|
| 22 |
+
for user_input, assistant_reply in history:
|
| 23 |
+
if user_input:
|
| 24 |
+
messages.append({"role": "user", "content": user_input})
|
| 25 |
+
if assistant_reply:
|
| 26 |
+
messages.append({"role": "assistant", "content": assistant_reply})
|
| 27 |
|
| 28 |
messages.append({"role": "user", "content": message})
|
| 29 |
|
| 30 |
response = ""
|
| 31 |
|
| 32 |
+
# Stream response tokens from the chat completion API
|
| 33 |
for message in client.chat_completion(
|
| 34 |
+
messages=messages,
|
| 35 |
max_tokens=max_tokens,
|
| 36 |
stream=True,
|
| 37 |
temperature=temperature,
|
| 38 |
top_p=top_p,
|
| 39 |
):
|
| 40 |
+
token = message["choices"][0]["delta"].get("content", "")
|
|
|
|
| 41 |
response += token
|
| 42 |
yield response
|
| 43 |
|
|
|
|
| 44 |
"""
|
| 45 |
+
For information on how to customize the ChatInterface, peruse the Gradio docs: https://www.gradio.app/docs/chatinterface
|
| 46 |
"""
|
| 47 |
+
|
| 48 |
demo = gr.ChatInterface(
|
| 49 |
respond,
|
| 50 |
additional_inputs=[
|
|
|
|
| 61 |
],
|
| 62 |
)
|
| 63 |
|
|
|
|
| 64 |
if __name__ == "__main__":
|
| 65 |
demo.launch()
|