Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,35 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
-
# Use a pipeline as a high-level helper
|
| 4 |
from transformers import pipeline
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
"""
|
| 8 |
-
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 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 11 |
|
| 12 |
-
|
| 13 |
def respond(
|
| 14 |
-
message,
|
| 15 |
-
history:
|
| 16 |
-
system_message,
|
| 17 |
-
max_tokens,
|
| 18 |
-
temperature,
|
| 19 |
-
top_p,
|
| 20 |
):
|
| 21 |
messages = [{"role": "system", "content": system_message}]
|
| 22 |
-
|
|
|
|
| 23 |
for val in history:
|
| 24 |
if val[0]:
|
| 25 |
messages.append({"role": "user", "content": val[0]})
|
| 26 |
if val[1]:
|
| 27 |
messages.append({"role": "assistant", "content": val[1]})
|
| 28 |
|
|
|
|
| 29 |
messages.append({"role": "user", "content": message})
|
| 30 |
|
| 31 |
response = ""
|
| 32 |
|
|
|
|
| 33 |
for message in client.chat_completion(
|
| 34 |
messages,
|
| 35 |
max_tokens=max_tokens,
|
|
@@ -38,14 +38,11 @@ def respond(
|
|
| 38 |
top_p=top_p,
|
| 39 |
):
|
| 40 |
token = message.choices[0].delta.content
|
| 41 |
-
|
| 42 |
response += token
|
| 43 |
yield response
|
| 44 |
|
| 45 |
|
| 46 |
-
|
| 47 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 48 |
-
"""
|
| 49 |
demo = gr.ChatInterface(
|
| 50 |
respond,
|
| 51 |
additional_inputs=[
|
|
@@ -62,6 +59,5 @@ demo = gr.ChatInterface(
|
|
| 62 |
],
|
| 63 |
)
|
| 64 |
|
| 65 |
-
|
| 66 |
if __name__ == "__main__":
|
| 67 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
| 3 |
from transformers import pipeline
|
| 4 |
+
from typing import List, Tuple # Importing for type annotations
|
| 5 |
|
| 6 |
+
# Initialize InferenceClient with the correct model
|
|
|
|
|
|
|
|
|
|
| 7 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
| 9 |
+
# Function to handle the response generation using chat completion
|
| 10 |
def respond(
|
| 11 |
+
message: str,
|
| 12 |
+
history: List[Tuple[str, str]], # Using List and Tuple for type annotation
|
| 13 |
+
system_message: str,
|
| 14 |
+
max_tokens: int,
|
| 15 |
+
temperature: float,
|
| 16 |
+
top_p: float,
|
| 17 |
):
|
| 18 |
messages = [{"role": "system", "content": system_message}]
|
| 19 |
+
|
| 20 |
+
# Append history to the messages list
|
| 21 |
for val in history:
|
| 22 |
if val[0]:
|
| 23 |
messages.append({"role": "user", "content": val[0]})
|
| 24 |
if val[1]:
|
| 25 |
messages.append({"role": "assistant", "content": val[1]})
|
| 26 |
|
| 27 |
+
# Append the current user message
|
| 28 |
messages.append({"role": "user", "content": message})
|
| 29 |
|
| 30 |
response = ""
|
| 31 |
|
| 32 |
+
# Use the client to get chat completion and stream the response
|
| 33 |
for message in client.chat_completion(
|
| 34 |
messages,
|
| 35 |
max_tokens=max_tokens,
|
|
|
|
| 38 |
top_p=top_p,
|
| 39 |
):
|
| 40 |
token = message.choices[0].delta.content
|
|
|
|
| 41 |
response += token
|
| 42 |
yield response
|
| 43 |
|
| 44 |
|
| 45 |
+
# Setting up Gradio Interface
|
|
|
|
|
|
|
| 46 |
demo = gr.ChatInterface(
|
| 47 |
respond,
|
| 48 |
additional_inputs=[
|
|
|
|
| 59 |
],
|
| 60 |
)
|
| 61 |
|
|
|
|
| 62 |
if __name__ == "__main__":
|
| 63 |
demo.launch()
|