llama / app.py
Nexchan's picture
Update app.py
75bef96 verified
raw
history blame
3.49 kB
import gradio as gr
from huggingface_hub import InferenceClient
from pydantic import BaseModel
from typing import List, Dict
# Inisialisasi client model
client = InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct")
# Model untuk data input API
class ChatMessage(BaseModel):
role: str
content: str
def chat_llama(chat_history: List[Dict[str, str]]):
# Mengirim chat_history ke model dan mendapatkan respons
chat_completion = client.chat_completion(
messages=chat_history,
max_tokens=500
)
# Menambahkan respons model ke chat_history
chat_history.append({"role": "assistant", "content": chat_completion.choices[0].message.content})
return chat_history
def chat_mem(message, chat_history):
# Membuat chat_history_role untuk pengolahan model
chat_history_role = [{"role": "system", "content": "You are a helpful assistant."}]
# Menambahkan pesan dari chat_history ke chat_history_role
if chat_history:
for user_message, assistant_response in chat_history:
chat_history_role.append({"role": "user", "content": user_message})
chat_history_role.append({"role": "assistant", "content": assistant_response})
# Menambahkan pesan pengguna terbaru
chat_history_role.append({"role": "user", "content": message})
# Mendapatkan respons dari model
chat_completion = client.chat_completion(
messages=chat_history_role,
max_tokens=500
)
# Menambahkan respons model ke chat_history_role
chat_history_role.append({"role": "assistant", "content": chat_completion.choices[0].message.content})
# Format ulang chat_history
modified = [entry["content"] for entry in chat_history_role]
chat_history = [(modified[i*2], modified[i*2+1]) for i in range(len(modified)//2)]
return "", chat_history # Kembalikan pesan kosong dan chat_history yang diperbarui
def api_chat(chat_history: List[Dict[str, str]]):
# Memanggil chat_llama untuk mendapatkan respons
updated_history = chat_llama(chat_history)
# Mengambil respons terakhir sebagai output
return updated_history[-1] if updated_history else {}
# Mengatur antarmuka Gradio
with gr.Blocks() as demo:
gr.Markdown("## Chat Demo")
with gr.Row():
with gr.Column():
# Bagian Antarmuka Pengguna
chatbot = gr.Chatbot()
msg = gr.Textbox(placeholder="Type your message here...", interactive=True)
with gr.Row():
clear = gr.ClearButton([msg, chatbot], icon="https://img.icons8.com/?size=100&id=Xnx8cxDef16O&format=png&color=000000")
send_btn = gr.Button("Send", variant='primary', icon="https://img.icons8.com/?size=100&id=g8ltXTwIfJ1n&format=png&color=000000")
msg.submit(fn=chat_mem, inputs=[msg, chatbot], outputs=[msg, chatbot])
send_btn.click(fn=chat_mem, inputs=[msg, chatbot], outputs=[msg, chatbot])
gr.Markdown("## API Endpoint for Testing")
gr.Markdown("### Send a POST request to `/api/chat` with the following JSON body:")
gr.Markdown("```json\n[ { \"role\": \"user\", \"content\": \"Hello, how are you?\" }, { \"role\": \"assistant\", \"content\": \"I'm fine, thank you! How can I assist you today?\" }, { \"role\": \"user\", \"content\": \"Can you tell me a joke?\" } ]\n```")
gr.Markdown("### API Response:")
gr.Interface(fn=api_chat, inputs="json", outputs="json").launch()
if __name__ == "__main__":
demo.launch()