Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import openai
|
| 3 |
+
|
| 4 |
+
ai_model = "gpt-4-turbo"
|
| 5 |
+
token = 4096
|
| 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 # Usage counter
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
max_uses = 3
|
| 20 |
+
|
| 21 |
+
if st.session_state['usage_count'] < max_uses:
|
| 22 |
+
# Main Contents Start from here -------------------------------
|
| 23 |
+
|
| 24 |
+
st.subheader('English')
|
| 25 |
+
en_input = st.text_area("Enter your English text here:", key="en_input")
|
| 26 |
+
if st.button("Correct", key="en_correction"):
|
| 27 |
+
st.session_state['usage_count'] += 1 # Increment the usage counter
|
| 28 |
+
# Create a prompt based on the user input
|
| 29 |
+
en_prompt = f"Correct the following English sentence. The output language should be English. Here is the text: {en_input}"
|
| 30 |
+
# Make a request to the API to generate text
|
| 31 |
+
en_response = openai.ChatCompletion.create(
|
| 32 |
+
model=ai_model, # Use the engine of your choice
|
| 33 |
+
messages=[{"role": "user", "content": en_prompt}],
|
| 34 |
+
max_tokens=token
|
| 35 |
+
)
|
| 36 |
+
st.write(en_response["choices"][0]["message"]["content"])
|
| 37 |
+
|
| 38 |
+
st.text(" ")
|
| 39 |
+
st.text(" ")
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
st.subheader('日本語')
|
| 43 |
+
ja_input = st.text_area("日本語を入力ください", key="ja_input")
|
| 44 |
+
if st.button("校正する", key="ja_correction"):
|
| 45 |
+
st.session_state['usage_count'] += 1 # Increment the usage counter
|
| 46 |
+
# Create a prompt based on the user input
|
| 47 |
+
ja_prompt = f"Correct the following Japanese sentence. The output language should be Japanese. Here is the text: {ja_input}"
|
| 48 |
+
# Make a request to the API to generate text
|
| 49 |
+
ja_response = openai.ChatCompletion.create(
|
| 50 |
+
model=ai_model, # Use the engine of your choice
|
| 51 |
+
messages=[{"role": "user", "content": ja_prompt}],
|
| 52 |
+
max_tokens=token
|
| 53 |
+
)
|
| 54 |
+
st.write(ja_response["choices"][0]["message"]["content"])
|
| 55 |
+
else:
|
| 56 |
+
st.error("You have reached your maximum usage limit.")
|
| 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 |
+
|