Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,8 @@
|
|
| 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]],
|
|
@@ -15,6 +11,7 @@ def respond(
|
|
| 15 |
temperature,
|
| 16 |
top_p,
|
| 17 |
):
|
|
|
|
| 18 |
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
for val in history:
|
|
@@ -23,8 +20,10 @@ def respond(
|
|
| 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(
|
|
@@ -35,18 +34,19 @@ 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(value=
|
| 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(
|
|
@@ -59,6 +59,5 @@ demo = gr.ChatInterface(
|
|
| 59 |
],
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
|
|
|
|
|
|
|
|
|
| 4 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 5 |
|
|
|
|
| 6 |
def respond(
|
| 7 |
message,
|
| 8 |
history: list[tuple[str, str]],
|
|
|
|
| 11 |
temperature,
|
| 12 |
top_p,
|
| 13 |
):
|
| 14 |
+
# Construct the messages with prompt engineering
|
| 15 |
messages = [{"role": "system", "content": system_message}]
|
| 16 |
|
| 17 |
for val in history:
|
|
|
|
| 20 |
if val[1]:
|
| 21 |
messages.append({"role": "assistant", "content": val[1]})
|
| 22 |
|
| 23 |
+
# Add the user message
|
| 24 |
messages.append({"role": "user", "content": message})
|
| 25 |
|
| 26 |
+
# Generate the response
|
| 27 |
response = ""
|
| 28 |
|
| 29 |
for message in client.chat_completion(
|
|
|
|
| 34 |
top_p=top_p,
|
| 35 |
):
|
| 36 |
token = message.choices[0].delta.content
|
|
|
|
| 37 |
response += token
|
| 38 |
yield response
|
| 39 |
|
| 40 |
+
# Example of a more detailed system message for prompt engineering
|
| 41 |
+
default_system_message = (
|
| 42 |
+
"You are an expert GIS person in the urban planning department and are being sent this query to figure out what possible GIS operations need to happen in sequence and on what datasets."
|
| 43 |
+
"If you don't know the answer, say 'I don't know' instead of making something up."
|
| 44 |
+
)
|
| 45 |
|
|
|
|
|
|
|
|
|
|
| 46 |
demo = gr.ChatInterface(
|
| 47 |
respond,
|
| 48 |
additional_inputs=[
|
| 49 |
+
gr.Textbox(value=default_system_message, label="System message"),
|
| 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(
|
|
|
|
| 59 |
],
|
| 60 |
)
|
| 61 |
|
|
|
|
| 62 |
if __name__ == "__main__":
|
| 63 |
+
demo.launch()
|