File size: 3,766 Bytes
15fafa0
 
 
 
 
 
 
 
 
 
 
 
 
cfd7c15
15fafa0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8362937
 
 
9e88c0d
ae72fd8
 
 
8362937
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae72fd8
 
 
8362937
15fafa0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8362937
15fafa0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from pydantic import BaseModel
import streamlit as st  # Import streamlit with the alias 'st'
import openai
import instructor

class Email(BaseModel):
  subject: str
  message: str 
  
client = openai.OpenAI(api_key='sk-RYiDonFCU85Bfr242X2POvCBhlhfHlz8knZQoVjWVJw180s6',
                                              base_url="https://api.chatanywhere.tech/v1")
client = instructor.from_openai(client)

def generate_email(subject, to, sender, tone, temperature, max_tokens, top_p):
  return client.chat.completions.create(
      model='gpt-4o-mini',
      messages=[
          {
              'role':'user',
              'content': f"""
              Write an email about{subject} to {to} from {sender}.
              The email should be{tone}
              """
          }
      ],
      response_model=Email,
      top_p=top_p,
      temperature=temperature,
  )

subject = st.text_area('Enter a subject')
sender = st.text_area('Enter a sender')
receiver = st.text_area('Enter a receiver')

temperature = st.slider("Select Temperature the level", 0.0, 1.0, step=0.1,key="temerature")
top_p = st.slider("Select p sampling", 0.0, 1.0, step=0.1,key="slider_top_p")
max_tokens = st.slider("Select the number of word", 100, 1000,key='max_tokens')
import streamlit as st

# Định nghĩa tone settings
tone_settings = {
    "Formal": {"temperature": 0.3, "top_p": 0.5},
    "Friendly": {"temperature": 0.7, "top_p": 0.8},
    "Humorous": {"temperature": 0.9, "top_p": 1.0},
    "Inspirational": {"temperature": 0.8, "top_p": 0.9},
    "Angry": {"temperature": 0.6, "top_p": 0.7},
    "Urgent": {"temperature": 0.5, "top_p": 0.6},
    "Sincere": {"temperature": 0.4, "top_p": 0.6},
    "Neutral": {"temperature": 0.3, "top_p": 0.5},
    "Optimistic": {"temperature": 0.7, "top_p": 0.8},
    "Pessimistic": {"temperature": 0.6, "top_p": 0.7},
    "Authoritative": {"temperature": 0.4, "top_p": 0.5},
    "Casual": {"temperature": 0.7, "top_p": 0.8},
    "Mysterious": {"temperature": 0.8, "top_p": 0.9},
    "Educational": {"temperature": 0.4, "top_p": 0.5},
    "Provocative": {"temperature": 0.8, "top_p": 1.0},
    "Youthful": {"temperature": 0.9, "top_p": 0.9},
    "Passionate": {"temperature": 0.8, "top_p": 0.9},
    "Calm": {"temperature": 0.4, "top_p": 0.6}
}

# Đặt tên cho biến trạng thái
if 'show_tone_settings' not in st.session_state:
    st.session_state.show_tone_settings = False

# Tạo header cho button
st.header('st.button')

# Tạo button với nhãn 'Guide adjust for tone'
if st.button('Guide adjust for tone'):
    # Đảo ngược giá trị trạng thái mỗi khi button được nhấn
    st.session_state.show_tone_settings = not st.session_state.show_tone_settings

# Hiển thị hoặc ẩn tone settings tùy theo trạng thái
if st.session_state.show_tone_settings:
    # Sử dụng st.json() để hiển thị dictionary tone_settings
    st.json(tone_settings)



st.text('Temperature: {}'.format(temperature))
st.text('Top p: {}'.format(top_p))
st.text('Number of words: {}'.format(max_tokens))
tone = st.selectbox("Tones : ",[
    "Formal",
    "Friendly",
    "Humorous",
    "Inspirational",
    "Angry",
    "Urgent",
    "Sincere",
    "Neutral",
    "Optimistic",
    "Pessimistic",
    "Authoritative",
    "Casual",
    "Mysterious",
    "Educational",
    "Provocative",
    "Youthful",
    "Passionate",
    "Calm"
])

# print the selected hobby
st.write("Your hobby is: ", tone)

if __name__ == "__main__":
    email = generate_email(
        subject=subject,
        to=sender,
        sender=receiver,
        tone=tone,
        temperature = temperature,
        max_tokens = max_tokens,
        top_p = top_p
    )

    st.text(email.subject)
    st.text('----------------')
    st.text(email.message)