Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import openai
|
3 |
+
|
4 |
+
# Constants and configurations
|
5 |
+
AI_MODEL = "gpt-4o"
|
6 |
+
TOKEN_COUNT = 4096
|
7 |
+
MAX_USES = 3
|
8 |
+
|
9 |
+
# Set the page title and favicon
|
10 |
+
st.set_page_config(page_title="AI Text Summerizer", page_icon=":bar_chart:")
|
11 |
+
|
12 |
+
# Load the API key
|
13 |
+
openai.api_key = st.secrets['OPENAI_API_KEY']
|
14 |
+
|
15 |
+
# Page title
|
16 |
+
st.title('AI Text Summerizer')
|
17 |
+
|
18 |
+
# Style adjustments (optional, remove if not needed)
|
19 |
+
st.markdown(
|
20 |
+
"""
|
21 |
+
<style>
|
22 |
+
/* Custom style adjustments */
|
23 |
+
.st-emotion-cache-iiif1v { display: none !important; }
|
24 |
+
.st-emotion-cache-gh2jqd {padding: 6rem 1rem 0rem;}
|
25 |
+
@media (max-width: 50.5rem) {
|
26 |
+
.st-emotion-cache-gh2jqd {
|
27 |
+
max-width: calc(0rem + 100vw);
|
28 |
+
}
|
29 |
+
}
|
30 |
+
</style>
|
31 |
+
""",
|
32 |
+
unsafe_allow_html=True,
|
33 |
+
)
|
34 |
+
|
35 |
+
# Initialize usage count and language in session state
|
36 |
+
if 'usage_count' not in st.session_state:
|
37 |
+
st.session_state['usage_count'] = 0
|
38 |
+
if 'language' not in st.session_state:
|
39 |
+
st.session_state['language'] = 'English'
|
40 |
+
|
41 |
+
def summarize_text(language, input_text):
|
42 |
+
"""Generate summarized text using OpenAI API based on the input text and language."""
|
43 |
+
if st.session_state['usage_count'] < MAX_USES:
|
44 |
+
st.session_state['usage_count'] += 1
|
45 |
+
prompt = f"Summarize the following {language} sentence. The output language should be {language}. Here is the text: {input_text}"
|
46 |
+
with st.spinner('Loading... Please wait.'):
|
47 |
+
response = openai.ChatCompletion.create(
|
48 |
+
model=AI_MODEL,
|
49 |
+
messages=[{"role": "user", "content": prompt}],
|
50 |
+
max_tokens=TOKEN_COUNT
|
51 |
+
)
|
52 |
+
return response["choices"][0]["message"]["content"]
|
53 |
+
else:
|
54 |
+
st.error("You have reached your maximum usage limit.")
|
55 |
+
return None
|
56 |
+
|
57 |
+
# Determine button text based on current language
|
58 |
+
if st.session_state['language'] == 'English':
|
59 |
+
switch_button_text = 'Japanese(日本語)'
|
60 |
+
else:
|
61 |
+
switch_button_text = 'English'
|
62 |
+
|
63 |
+
# Language switcher button
|
64 |
+
if st.button(switch_button_text):
|
65 |
+
if st.session_state['language'] == 'English':
|
66 |
+
st.session_state['language'] = 'Japanese'
|
67 |
+
else:
|
68 |
+
st.session_state['language'] = 'English'
|
69 |
+
st.experimental_rerun()
|
70 |
+
|
71 |
+
# Display form based on selected language
|
72 |
+
if st.session_state['language'] == 'English':
|
73 |
+
st.subheader('English')
|
74 |
+
en_input = st.text_area("Enter your text here:", key="en_input")
|
75 |
+
if st.button("Summarize", key="en_summarize"):
|
76 |
+
result = summarize_text("English", en_input)
|
77 |
+
if result:
|
78 |
+
st.write(result)
|
79 |
+
else:
|
80 |
+
st.subheader('日本語')
|
81 |
+
ja_input = st.text_area("日本語を入力ください", key="ja_input")
|
82 |
+
if st.button("要約する", key="ja_summarize"):
|
83 |
+
result = summarize_text("Japanese", ja_input)
|
84 |
+
if result:
|
85 |
+
st.write(result)
|