Spaces:
Sleeping
Sleeping
Sarah Ciston
commited on
Commit
·
81b0953
1
Parent(s):
5ad7b8c
add chatbot inference interface
Browse files
app.py
CHANGED
@@ -1,7 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
def
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
|
3 |
+
|
4 |
+
# import pipelines
|
5 |
+
# import datasets
|
6 |
+
import os
|
7 |
+
|
8 |
+
if not os.getenv('HF_TOKEN'):
|
9 |
+
raise ValueError('HF_TOKEN must be set')
|
10 |
+
|
11 |
+
from huggingface_hub import InferenceClient
|
12 |
import gradio as gr
|
13 |
+
from gradio import ChatMessage
|
14 |
+
|
15 |
+
MODEL = "meta-llama/Meta-Llama-3-8B-Instruct"
|
16 |
+
# PROMPT = "What is happiness?"
|
17 |
+
HF_TOKEN = os.getenv('HF_TOKEN')
|
18 |
+
|
19 |
+
client = InferenceClient(MODEL, token=HF_TOKEN)
|
20 |
+
# inputs = [{"role": "user", "content": PROMPT}]
|
21 |
+
# output = client.chat_completion(messages, max_tokens=100)
|
22 |
+
|
23 |
+
print(output.choices[0].message.content)
|
24 |
+
print(output.model)
|
25 |
|
26 |
+
def interact_with_agent(prompt, messages):
|
27 |
+
messages.append(ChatMessage(role="user", content=prompt))
|
28 |
+
yield messages
|
29 |
+
# for msg in stream_from_transformers_agent(agent, prompt):
|
30 |
+
for msg in client.chat_completion(messages, max_tokens=100):
|
31 |
+
messages.append(msg)
|
32 |
+
yield messages
|
33 |
+
yield messages
|
34 |
|
35 |
+
with gr.Blocks() as demo:
|
36 |
+
chatbot = gr.ChatMessage(label="Agent",
|
37 |
+
msg_format="messages")
|
38 |
+
text_input = gr.Textbox(lines=1, label="Chat Message")
|
39 |
+
text_input.submit(interact_with_agent, [text_input, chatbot], [chatbot])
|
40 |
+
|
41 |
+
if __name__ == "__main__":
|
42 |
+
demo.launch()
|