Riswan-Nopiyar commited on
Commit
87a558e
·
verified ·
1 Parent(s): aab6105

fungsi fungsi

Browse files
Files changed (1) hide show
  1. functions.py +50 -0
functions.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import plotly.express as px
2
+ import gradio as gr
3
+ from client import client
4
+
5
+ # Fungsi untuk menghasilkan plot acak
6
+ def random_plot():
7
+ df = px.data.iris()
8
+ fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species",
9
+ size='petal_length', hover_data=['petal_width'])
10
+ return fig
11
+
12
+ # Fungsi untuk mencetak data suka/tidak suka
13
+ def print_like_dislike(x: gr.LikeData):
14
+ print(x.index, x.value, x.liked)
15
+
16
+ # Fungsi untuk menambahkan pesan ke riwayat
17
+ def add_message(history, message):
18
+ if message["files"]:
19
+ for x in message["files"]:
20
+ history.append(((x,), None))
21
+ if message["text"] is not None:
22
+ history.append((message["text"], None))
23
+ return history, gr.Textbox(value="", interactive=True)
24
+
25
+ # Fungsi untuk menghubungkan dengan model Llama
26
+ def chat_mem(message, chat_history):
27
+ chat_history_role = [{"role": "system", "content": "Anda adalah Chat Bot milik Riswan Nopiyar, yang dirancang untuk membantu menjawab pertanyaan dan memberikan informasi dengan sopan dan akurat dalam bahasa Indonesia. Seluruh percakapan akan dilakukan dalam bahasa Indonesia."}]
28
+
29
+ if chat_history:
30
+ for user_msg, assistant_msg in chat_history:
31
+ chat_history_role.append({"role": "user", "content": user_msg})
32
+ chat_history_role.append({"role": "assistant", "content": assistant_msg})
33
+
34
+ chat_history_role.append({"role": "user", "content": message})
35
+
36
+ if "siapa pembuat" in message.lower() or "siapa pencipta" in message.lower():
37
+ assistant_response = "Chatbot ini dibuat oleh Riswan Nopiyar. Jika Anda memiliki pertanyaan lain atau membutuhkan bantuan lebih lanjut, jangan ragu untuk bertanya."
38
+ else:
39
+ chat_completion = client.chat_completion(
40
+ messages=chat_history_role,
41
+ max_tokens=500,
42
+ )
43
+ assistant_response = chat_completion.choices[0].message.content
44
+
45
+ chat_history_role.append({"role": "assistant", "content": assistant_response})
46
+
47
+ modified = [msg["content"] for msg in chat_history_role]
48
+ chat_history = [(modified[i * 2 + 1], modified[i * 2 + 2]) for i in range(len(modified) // 2)]
49
+
50
+ return "", chat_history