heysho commited on
Commit
1491ee6
Β·
verified Β·
1 Parent(s): cfb41b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -70
app.py CHANGED
@@ -1,81 +1,65 @@
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
-
 
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
+ )