Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,81 +1,65 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import openai
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Set the page title and favicon
|
| 8 |
st.set_page_config(page_title="Text Correction", page_icon=":bar_chart:")
|
| 9 |
|
|
|
|
| 10 |
openai.api_key = st.secrets['OPENAI_API_KEY']
|
| 11 |
-
st.title('Sentence Correction')
|
| 12 |
-
|
| 13 |
|
|
|
|
|
|
|
| 14 |
|
|
|
|
| 15 |
if 'usage_count' not in st.session_state:
|
| 16 |
-
st.session_state['usage_count'] = 0
|
| 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 |
-
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import openai
|
| 3 |
|
| 4 |
+
# Constants and configurations
|
| 5 |
+
AI_MODEL = "gpt-4-turbo"
|
| 6 |
+
TOKEN_COUNT = 4096
|
| 7 |
+
MAX_USES = 3
|
| 8 |
|
| 9 |
# Set the page title and favicon
|
| 10 |
st.set_page_config(page_title="Text Correction", 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('Sentence Correction')
|
| 17 |
|
| 18 |
+
# Initialize usage count in session state
|
| 19 |
if 'usage_count' not in st.session_state:
|
| 20 |
+
st.session_state['usage_count'] = 0
|
| 21 |
+
|
| 22 |
+
def correct_text(language, input_text):
|
| 23 |
+
"""Generate corrected text using OpenAI API based on the input text and language."""
|
| 24 |
+
if st.session_state['usage_count'] < MAX_USES:
|
| 25 |
+
st.session_state['usage_count'] += 1
|
| 26 |
+
prompt = f"Correct the following {language} sentence. The output language should be {language}. Here is the text: {input_text}"
|
| 27 |
+
with st.spinner('Loading... Please wait.'):
|
| 28 |
+
response = openai.ChatCompletion.create(
|
| 29 |
+
model=AI_MODEL,
|
| 30 |
+
messages=[{"role": "user", "content": prompt}],
|
| 31 |
+
max_tokens=TOKEN_COUNT
|
| 32 |
+
)
|
| 33 |
+
return response["choices"][0]["message"]["content"]
|
| 34 |
+
else:
|
| 35 |
+
st.error("You have reached your maximum usage limit.")
|
| 36 |
+
return None
|
| 37 |
+
|
| 38 |
+
# Text correction form for English
|
| 39 |
+
st.subheader('English')
|
| 40 |
+
en_input = st.text_area("Enter your English text here:", key="en_input")
|
| 41 |
+
if st.button("Correct", key="en_correction"):
|
| 42 |
+
result = correct_text("English", en_input)
|
| 43 |
+
if result:
|
| 44 |
+
st.write(result)
|
| 45 |
+
|
| 46 |
+
st.text(" ") # Spacing between sections
|
| 47 |
+
|
| 48 |
+
# Text correction form for Japanese
|
| 49 |
+
st.subheader('ζ₯ζ¬θͺ')
|
| 50 |
+
ja_input = st.text_area("ζ₯ζ¬θͺγε
₯εγγ γγ", key="ja_input")
|
| 51 |
+
if st.button("ζ ‘ζ£γγ", key="ja_correction"):
|
| 52 |
+
result = correct_text("Japanese", ja_input)
|
| 53 |
+
if result:
|
| 54 |
+
st.write(result)
|
| 55 |
+
|
| 56 |
+
# Style adjustments (optional, remove if not needed)
|
| 57 |
+
st.markdown(
|
| 58 |
+
"""
|
| 59 |
+
<style>
|
| 60 |
+
/* Custom style adjustments */
|
| 61 |
+
.st-emotion-cache-iiif1v { display: none !important; }
|
| 62 |
+
</style>
|
| 63 |
+
""",
|
| 64 |
+
unsafe_allow_html=True,
|
| 65 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|