Spaces:
Sleeping
Sleeping
upload chat
Browse files
chat.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
+
|
| 4 |
+
client = InferenceClient(
|
| 5 |
+
provider="featherless-ai",
|
| 6 |
+
api_key=os.environ["HF_TOKEN"]
|
| 7 |
+
)
|
| 8 |
+
|
| 9 |
+
def chat_with_model(message, history, perspective):
|
| 10 |
+
# build messages list with perspective
|
| 11 |
+
messages = [{"role": "system", "content": f"Adopt this perspective: {perspective}"}]
|
| 12 |
+
for user_msg, bot_msg in history:
|
| 13 |
+
messages.append({"role": "user", "content": user_msg})
|
| 14 |
+
if bot_msg:
|
| 15 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 16 |
+
messages.append({"role": "user", "content": message})
|
| 17 |
+
|
| 18 |
+
# Call the model
|
| 19 |
+
reply="" # start empty reply to create the illusion of streaming
|
| 20 |
+
for event in client.chat.completions.create(
|
| 21 |
+
model="mistralai/Mistral-7B-Instruct-v0.2",
|
| 22 |
+
messages=messages,
|
| 23 |
+
max_tokens=512,
|
| 24 |
+
stream=True, # enable streaming
|
| 25 |
+
):
|
| 26 |
+
if token := event.delta.content:
|
| 27 |
+
reply += token
|
| 28 |
+
# yield partial result for Gradio streaming
|
| 29 |
+
yield history + [(message, reply)], history
|