-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
151 lines (132 loc) Β· 4.74 KB
/
app.py
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import streamlit as st
from openai import OpenAI
import os
from dotenv import load_dotenv
from deep_translator import GoogleTranslator
# Load environment variables
load_dotenv()
# Initialize OpenAI client
client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
MODEL_NAME = "ft:gpt-4o-mini-2024-07-18:personal:my-poaster-v2:AoUBJiKP"
# Initialize session state variables
if 'generated_text' not in st.session_state:
st.session_state.generated_text = None
if 'translated_text' not in st.session_state:
st.session_state.translated_text = None
# Function to handle translation
# Function to handle translation
def translate_text():
if st.session_state.generated_text:
try:
translator = GoogleTranslator(source='en', target='fr')
st.session_state.translated_text = translator.translate(st.session_state.generated_text)
except Exception as e:
print(f"Translation error: {e}")
st.session_state.translated_text = st.session_state.generated_text + "\n\n(Translation failed)"
# System prompt used in training
SYSTEM_PROMPT = "You are a schizophrenic poaster from Twitter. You are unhinged and tweet overly verbose yet cogent updates on the state of technology."
# Set page config
st.set_page_config(
page_title="TweetosGPT",
page_icon="π€",
layout="wide",
initial_sidebar_state="collapsed"
)
# Title
st.markdown("# TweetosGPT")
# Subtitle
st.markdown("#### Just give it a topic e.g. tech jobs in sf")
# Create two columns
col1, col2 = st.columns([3, 1])
with col1:
# Input field with label
user_input = st.text_input(
label="Topic Input",
placeholder="Enter a topic...",
label_visibility="collapsed"
)
with col2:
# Settings expander
with st.expander("βοΈ Settings"):
# Temperature slider
st.markdown("##### Temperature")
temperature = st.slider(
"Temperature control",
min_value=0.0,
max_value=1.0,
value=0.7,
step=0.1,
help="Higher values make the output more random, lower values make it more focused and deterministic.",
label_visibility="collapsed"
)
st.markdown("""
<div class="settings-guide">
π§ 0.0 = More focused<br>
π₯ 1.0 = More creative
</div>
""", unsafe_allow_html=True)
# Max tokens slider
st.markdown("##### Length")
max_tokens = st.slider(
"Length control",
min_value=50,
max_value=300,
value=150,
step=10,
help="Controls the maximum length of the generated tweet.",
label_visibility="collapsed"
)
st.markdown("""
<div class="settings-guide">
π 50 = Short tweet<br>
π 300 = Long tweet
</div>
""", unsafe_allow_html=True)
def get_ai_response(topic: str, temp: float, tokens: int) -> str:
try:
# Format the prompt by adding "Write a tweet about" prefix
formatted_prompt = f"Write a tweet about {topic}"
response = client.chat.completions.create(
model=MODEL_NAME,
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": formatted_prompt}
],
max_tokens=tokens,
temperature=temp
)
return response.choices[0].message.content
except Exception as e:
print(f"OpenAI API error: {e}")
return "Sorry, I encountered an error processing your request."
# Generate button
if st.button("Generate"):
if user_input:
with st.spinner('Generating response...'):
response = get_ai_response(user_input, temperature, max_tokens)
st.session_state.generated_text = response
st.session_state.translated_text = None # Reset translation when generating new text
# Display generated text if it exists
if st.session_state.generated_text:
st.markdown("**TweetosGPT says:**")
st.markdown(f"""
<div class="response-box">
{st.session_state.generated_text}
</div>
""", unsafe_allow_html=True)
# Add translate button after the generated text
st.button("π«π· Translate to French", on_click=translate_text)
# Show translation if it exists
if st.session_state.translated_text:
st.markdown("**French translation:**")
st.markdown(f"""
<div class="response-box">
{st.session_state.translated_text}
</div>
""", unsafe_allow_html=True)
# Add footer with small text
st.markdown("""
<div style='position: fixed; bottom: 20px; right: 20px; color: gray; font-size: 12px;'>
Powered by OpenAI
</div>
""", unsafe_allow_html=True)