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)