Chad commited on
Commit ·
5a88082
1
Parent(s): 3f7b468
fr options
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,9 @@ def respond(
|
|
| 15 |
temperature,
|
| 16 |
top_p,
|
| 17 |
):
|
|
|
|
|
|
|
|
|
|
| 18 |
messages = [{"role": "system", "content": "You are a knowledgeable historian. Only answer questions related to history. If asked about other topics, politely refuse."}]
|
| 19 |
|
| 20 |
for val in history:
|
|
@@ -24,7 +23,7 @@ def respond(
|
|
| 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,14 +34,16 @@ 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 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
| 46 |
demo = gr.ChatInterface(
|
| 47 |
respond,
|
| 48 |
additional_inputs=[
|
|
@@ -62,10 +63,16 @@ demo = gr.ChatInterface(
|
|
| 62 |
["Who built the Colosseum?"],
|
| 63 |
["What caused the fall of the Roman Empire?"],
|
| 64 |
["When did World War II begin and end?"],
|
| 65 |
-
["What was the significance of the Magna Carta?"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
],
|
|
|
|
|
|
|
| 67 |
)
|
| 68 |
|
| 69 |
-
|
| 70 |
if __name__ == "__main__":
|
| 71 |
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 |
+
if history is None:
|
| 15 |
+
history = []
|
| 16 |
+
|
| 17 |
messages = [{"role": "system", "content": "You are a knowledgeable historian. Only answer questions related to history. If asked about other topics, politely refuse."}]
|
| 18 |
|
| 19 |
for val in history:
|
|
|
|
| 23 |
messages.append({"role": "assistant", "content": val[1]})
|
| 24 |
|
| 25 |
messages.append({"role": "user", "content": message})
|
| 26 |
+
|
| 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 |
|
| 41 |
+
def example_click(example, history):
|
| 42 |
+
if history is None:
|
| 43 |
+
history = []
|
| 44 |
+
history.append((example, ""))
|
| 45 |
+
return example, history
|
| 46 |
+
|
| 47 |
demo = gr.ChatInterface(
|
| 48 |
respond,
|
| 49 |
additional_inputs=[
|
|
|
|
| 63 |
["Who built the Colosseum?"],
|
| 64 |
["What caused the fall of the Roman Empire?"],
|
| 65 |
["When did World War II begin and end?"],
|
| 66 |
+
["What was the significance of the Magna Carta?"],
|
| 67 |
+
["Comment la tour de Pise a-t-elle commencé à pencher ?"],
|
| 68 |
+
["Qui a construit le Colisée ?"],
|
| 69 |
+
["Quelles sont les causes de la chute de l'Empire romain ?"],
|
| 70 |
+
["Quand la Seconde Guerre mondiale a-t-elle commencé et terminé ?"],
|
| 71 |
+
["Quelle est l'importance de la Magna Carta ?"]
|
| 72 |
],
|
| 73 |
+
cache_examples=False,
|
| 74 |
+
example_click=example_click
|
| 75 |
)
|
| 76 |
|
|
|
|
| 77 |
if __name__ == "__main__":
|
| 78 |
demo.launch()
|