Nexchan commited on
Commit
2c5314f
·
verified ·
1 Parent(s): 5ea96d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -37
app.py CHANGED
@@ -6,56 +6,38 @@ from typing import List, Dict
6
  # Inisialisasi client model
7
  client = InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct")
8
 
9
- # Model untuk data input API
10
- class ChatMessage(BaseModel):
11
- role: str
12
- content: str
13
-
14
  def chat_llama(chat_history: List[Dict[str, str]]):
15
- # Mengirim chat_history ke model dan mendapatkan respons
16
  chat_completion = client.chat_completion(
17
  messages=chat_history,
18
  max_tokens=500
19
  )
20
- # Menambahkan respons model ke chat_history
21
  chat_history.append({"role": "assistant", "content": chat_completion.choices[0].message.content})
22
-
23
  return chat_history
24
 
25
  def chat_mem(message, chat_history):
26
- # Membuat chat_history_role untuk pengolahan model
27
  chat_history_role = [{"role": "system", "content": "You are a helpful assistant."}]
28
-
29
- # Menambahkan pesan dari chat_history ke chat_history_role
30
  if chat_history:
31
  for user_message, assistant_response in chat_history:
32
  chat_history_role.append({"role": "user", "content": user_message})
33
  chat_history_role.append({"role": "assistant", "content": assistant_response})
34
-
35
- # Menambahkan pesan pengguna terbaru
36
  chat_history_role.append({"role": "user", "content": message})
37
-
38
- # Mendapatkan respons dari model
39
  chat_completion = client.chat_completion(
40
  messages=chat_history_role,
41
  max_tokens=500
42
  )
43
-
44
- # Menambahkan respons model ke chat_history_role
45
  chat_history_role.append({"role": "assistant", "content": chat_completion.choices[0].message.content})
46
-
47
- # Format ulang chat_history
48
  modified = [entry["content"] for entry in chat_history_role]
49
  chat_history = [(modified[i*2], modified[i*2+1]) for i in range(len(modified)//2)]
50
-
51
- return "", chat_history # Kembalikan pesan kosong dan chat_history yang diperbarui
52
 
53
  def api_chat(chat_history: List[Dict[str, str]]):
54
- # Memanggil chat_llama untuk mendapatkan respons
55
  updated_history = chat_llama(chat_history)
56
- # Mengambil respons terakhir sebagai output
57
  return updated_history[-1] if updated_history else {}
58
 
 
 
 
 
59
  # Mengatur antarmuka Gradio
60
  with gr.Blocks() as demo:
61
  with gr.Row():
@@ -68,20 +50,14 @@ with gr.Blocks() as demo:
68
  msg.submit(fn=chat_mem, inputs=[msg, chatbot], outputs=[msg, chatbot])
69
  send_btn.click(fn=chat_mem, inputs=[msg, chatbot], outputs=[msg, chatbot])
70
 
71
- # Mengatur endpoint API
72
- def api_endpoint(chat_history: List[Dict[str, str]]):
73
- updated_history = api_chat(chat_history)
74
- return updated_history
75
-
76
- # Membuat interface API
77
- api_interface = gr.Interface(
78
- fn=api_endpoint,
79
- inputs=gr.JSON(),
80
- outputs=gr.JSON(),
81
- title="Chat API",
82
- description="Endpoint API untuk berinteraksi dengan model chat."
83
- )
84
 
85
- if __name__ == "__main__":
86
  demo.launch()
87
  api_interface.launch(server_name="0.0.0.0", server_port=7860)
 
6
  # Inisialisasi client model
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
+ return "", chat_history
 
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():
 
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
+ # Membuat interface API
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
  demo.launch()
63
  api_interface.launch(server_name="0.0.0.0", server_port=7860)