Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,57 +7,69 @@ from typing import List, Dict
|
|
| 7 |
client = InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct")
|
| 8 |
|
| 9 |
def chat_llama(chat_history: List[Dict[str, str]]):
|
|
|
|
| 10 |
chat_completion = client.chat_completion(
|
| 11 |
messages=chat_history,
|
| 12 |
max_tokens=500
|
| 13 |
)
|
|
|
|
| 14 |
chat_history.append({"role": "assistant", "content": chat_completion.choices[0].message.content})
|
|
|
|
| 15 |
return chat_history
|
| 16 |
|
| 17 |
def chat_mem(message, chat_history):
|
|
|
|
| 18 |
chat_history_role = [{"role": "system", "content": "You are a helpful assistant."}]
|
|
|
|
|
|
|
| 19 |
if chat_history:
|
| 20 |
for user_message, assistant_response in chat_history:
|
| 21 |
chat_history_role.append({"role": "user", "content": user_message})
|
| 22 |
chat_history_role.append({"role": "assistant", "content": assistant_response})
|
|
|
|
|
|
|
| 23 |
chat_history_role.append({"role": "user", "content": message})
|
|
|
|
|
|
|
| 24 |
chat_completion = client.chat_completion(
|
| 25 |
messages=chat_history_role,
|
| 26 |
max_tokens=500
|
| 27 |
)
|
|
|
|
|
|
|
| 28 |
chat_history_role.append({"role": "assistant", "content": chat_completion.choices[0].message.content})
|
|
|
|
|
|
|
| 29 |
modified = [entry["content"] for entry in chat_history_role]
|
| 30 |
chat_history = [(modified[i*2], modified[i*2+1]) for i in range(len(modified)//2)]
|
| 31 |
-
|
|
|
|
| 32 |
|
| 33 |
def api_chat(chat_history: List[Dict[str, str]]):
|
|
|
|
| 34 |
updated_history = chat_llama(chat_history)
|
|
|
|
| 35 |
return updated_history[-1] if updated_history else {}
|
| 36 |
|
| 37 |
-
def api_endpoint(chat_history: List[Dict[str, str]]):
|
| 38 |
-
updated_history = api_chat(chat_history)
|
| 39 |
-
return updated_history
|
| 40 |
-
|
| 41 |
# Mengatur antarmuka Gradio
|
| 42 |
with gr.Blocks() as demo:
|
| 43 |
with gr.Row():
|
| 44 |
with gr.Column():
|
| 45 |
chatbot = gr.Chatbot()
|
| 46 |
-
msg =
|
| 47 |
with gr.Row():
|
| 48 |
clear = gr.ClearButton([msg, chatbot], icon="https://img.icons8.com/?size=100&id=Xnx8cxDef16O&format=png&color=000000")
|
| 49 |
send_btn = gr.Button("Send", variant='primary', icon="https://img.icons8.com/?size=100&id=g8ltXTwIfJ1n&format=png&color=000000")
|
| 50 |
msg.submit(fn=chat_mem, inputs=[msg, chatbot], outputs=[msg, chatbot])
|
| 51 |
send_btn.click(fn=chat_mem, inputs=[msg, chatbot], outputs=[msg, chatbot])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
|
| 54 |
-
api_interface = gr.Interface(
|
| 55 |
-
fn=api_endpoint,
|
| 56 |
-
inputs=gr.JSON(),
|
| 57 |
-
outputs=gr.JSON(),
|
| 58 |
-
title="Chat API",
|
| 59 |
-
description="Endpoint API untuk berinteraksi dengan model chat."
|
| 60 |
-
)
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
|
|
|
| 7 |
client = InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct")
|
| 8 |
|
| 9 |
def chat_llama(chat_history: List[Dict[str, str]]):
|
| 10 |
+
# Mengirim chat_history ke model dan mendapatkan respons
|
| 11 |
chat_completion = client.chat_completion(
|
| 12 |
messages=chat_history,
|
| 13 |
max_tokens=500
|
| 14 |
)
|
| 15 |
+
# Menambahkan respons model ke chat_history
|
| 16 |
chat_history.append({"role": "assistant", "content": chat_completion.choices[0].message.content})
|
| 17 |
+
|
| 18 |
return chat_history
|
| 19 |
|
| 20 |
def chat_mem(message, chat_history):
|
| 21 |
+
# Membuat chat_history_role untuk pengolahan model
|
| 22 |
chat_history_role = [{"role": "system", "content": "You are a helpful assistant."}]
|
| 23 |
+
|
| 24 |
+
# Menambahkan pesan dari chat_history ke chat_history_role
|
| 25 |
if chat_history:
|
| 26 |
for user_message, assistant_response in chat_history:
|
| 27 |
chat_history_role.append({"role": "user", "content": user_message})
|
| 28 |
chat_history_role.append({"role": "assistant", "content": assistant_response})
|
| 29 |
+
|
| 30 |
+
# Menambahkan pesan pengguna terbaru
|
| 31 |
chat_history_role.append({"role": "user", "content": message})
|
| 32 |
+
|
| 33 |
+
# Mendapatkan respons dari model
|
| 34 |
chat_completion = client.chat_completion(
|
| 35 |
messages=chat_history_role,
|
| 36 |
max_tokens=500
|
| 37 |
)
|
| 38 |
+
|
| 39 |
+
# Menambahkan respons model ke chat_history_role
|
| 40 |
chat_history_role.append({"role": "assistant", "content": chat_completion.choices[0].message.content})
|
| 41 |
+
|
| 42 |
+
# Format ulang chat_history
|
| 43 |
modified = [entry["content"] for entry in chat_history_role]
|
| 44 |
chat_history = [(modified[i*2], modified[i*2+1]) for i in range(len(modified)//2)]
|
| 45 |
+
|
| 46 |
+
return "", chat_history # Kembalikan pesan kosong dan chat_history yang diperbarui
|
| 47 |
|
| 48 |
def api_chat(chat_history: List[Dict[str, str]]):
|
| 49 |
+
# Memanggil chat_llama untuk mendapatkan respons
|
| 50 |
updated_history = chat_llama(chat_history)
|
| 51 |
+
# Mengambil respons terakhir sebagai output
|
| 52 |
return updated_history[-1] if updated_history else {}
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
# Mengatur antarmuka Gradio
|
| 55 |
with gr.Blocks() as demo:
|
| 56 |
with gr.Row():
|
| 57 |
with gr.Column():
|
| 58 |
chatbot = gr.Chatbot()
|
| 59 |
+
msg = gr.Textbox(interactive=True)
|
| 60 |
with gr.Row():
|
| 61 |
clear = gr.ClearButton([msg, chatbot], icon="https://img.icons8.com/?size=100&id=Xnx8cxDef16O&format=png&color=000000")
|
| 62 |
send_btn = gr.Button("Send", variant='primary', icon="https://img.icons8.com/?size=100&id=g8ltXTwIfJ1n&format=png&color=000000")
|
| 63 |
msg.submit(fn=chat_mem, inputs=[msg, chatbot], outputs=[msg, chatbot])
|
| 64 |
send_btn.click(fn=chat_mem, inputs=[msg, chatbot], outputs=[msg, chatbot])
|
| 65 |
+
|
| 66 |
+
with gr.Column():
|
| 67 |
+
gr.Markdown("### API Testing")
|
| 68 |
+
json_input = gr.Textbox(label="Input JSON", placeholder='Enter JSON here', lines=10)
|
| 69 |
+
json_output = gr.Textbox(label="Output JSON", lines=10, interactive=False)
|
| 70 |
+
test_btn = gr.Button("Test API")
|
| 71 |
|
| 72 |
+
test_btn.click(fn=chat_llama, inputs=json_input, outputs=json_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
if __name__ == "__main__":
|
| 75 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|