Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import openai
|
| 3 |
+
|
| 4 |
+
# Constants
|
| 5 |
+
AI_MODEL = "gpt-4o"
|
| 6 |
+
TOKEN_COUNT = 4096
|
| 7 |
+
MAX_USES = 3
|
| 8 |
+
|
| 9 |
+
# Set page configuration
|
| 10 |
+
st.set_page_config(page_title="Code Debugging Tool", page_icon=":bar_chart:")
|
| 11 |
+
|
| 12 |
+
# Load API key and prompt from secrets
|
| 13 |
+
openai.api_key = st.secrets['OPENAI_API_KEY']
|
| 14 |
+
# prompt_template = st.secrets['PROMPT_DEBUG']
|
| 15 |
+
|
| 16 |
+
# Page title
|
| 17 |
+
st.title('Code Debugging Tool')
|
| 18 |
+
|
| 19 |
+
# Initialize usage counter
|
| 20 |
+
if 'usage_count' not in st.session_state:
|
| 21 |
+
st.session_state['usage_count'] = 0
|
| 22 |
+
|
| 23 |
+
def generate_from_ai(language, input_task, input_code, input_error):
|
| 24 |
+
"""Generates requirements based on the given instructions and background context."""
|
| 25 |
+
if st.session_state['usage_count'] < MAX_USES:
|
| 26 |
+
st.session_state['usage_count'] += 1 # Increment the usage counter
|
| 27 |
+
task_prompt = f"""
|
| 28 |
+
- Output language: {language}
|
| 29 |
+
- Task: {input_task}
|
| 30 |
+
- Current Code: {input_code}
|
| 31 |
+
- Error Message: {input_error}
|
| 32 |
+
"""
|
| 33 |
+
with st.spinner('Defining requirements...'):
|
| 34 |
+
response = openai.ChatCompletion.create(
|
| 35 |
+
model=AI_MODEL,
|
| 36 |
+
messages=[{"role": "user", "content": task_prompt}],
|
| 37 |
+
max_tokens=TOKEN_COUNT
|
| 38 |
+
)
|
| 39 |
+
return response["choices"][0]["message"]["content"]
|
| 40 |
+
else:
|
| 41 |
+
st.error("You have reached your maximum usage limit.")
|
| 42 |
+
return None
|
| 43 |
+
|
| 44 |
+
# Input and button for English requirements
|
| 45 |
+
st.subheader('English')
|
| 46 |
+
en_input_task = st.text_input(
|
| 47 |
+
"Enter the Task, What do you want to do (e.g., Review my code below and correct the mistakes)",
|
| 48 |
+
key="en_input_task"
|
| 49 |
+
)
|
| 50 |
+
en_input_code = st.text_area(
|
| 51 |
+
"Paste your code here",
|
| 52 |
+
key="en_input_code"
|
| 53 |
+
)
|
| 54 |
+
en_input_error = st.text_area(
|
| 55 |
+
"Paste the error message here",
|
| 56 |
+
key="en_input_error"
|
| 57 |
+
)
|
| 58 |
+
if st.button("Debug", key="en_fixed_code"):
|
| 59 |
+
result = generate_from_ai("English", en_input_task, en_input_code,en_input_error)
|
| 60 |
+
if result:
|
| 61 |
+
st.write(result)
|
| 62 |
+
|
| 63 |
+
# Input and button for Japanese requirements
|
| 64 |
+
st.subheader('日本語')
|
| 65 |
+
ja_input_task = st.text_input(
|
| 66 |
+
"タスクの内容、またはしたいことを入力ください。 (例:下記のコードをレビューして修正ください。)",
|
| 67 |
+
key="ja_input_task"
|
| 68 |
+
)
|
| 69 |
+
ja_input_code = st.text_area(
|
| 70 |
+
"現在のコードを入力ください",
|
| 71 |
+
key="ja_input_code"
|
| 72 |
+
)
|
| 73 |
+
ja_input_error = st.text_area(
|
| 74 |
+
"エラーメッセージを入力ください",
|
| 75 |
+
key="ja_input_error"
|
| 76 |
+
)
|
| 77 |
+
if st.button("デバッグする", key="ja_fixed_code"):
|
| 78 |
+
result = generate_from_ai("English", ja_input_task, ja_input_code,ja_input_error)
|
| 79 |
+
if result:
|
| 80 |
+
st.write(result)
|
| 81 |
+
|
| 82 |
+
st.text("")
|
| 83 |
+
|
| 84 |
+
# Styling (optional)
|
| 85 |
+
st.markdown(
|
| 86 |
+
"""
|
| 87 |
+
<style>
|
| 88 |
+
/* Custom style adjustments */
|
| 89 |
+
.st-emotion-cache-iiif1v { display: none !important; }
|
| 90 |
+
</style>
|
| 91 |
+
""",
|
| 92 |
+
unsafe_allow_html=True,
|
| 93 |
+
)
|
| 94 |
+
|