shiva9876 commited on
Commit
7b4e6e0
·
verified ·
1 Parent(s): 7f31646

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +155 -154
app.py CHANGED
@@ -1,154 +1,155 @@
1
- import streamlit as st
2
- import requests
3
- import re
4
-
5
- # Page Config with Logo
6
- st.set_page_config(
7
- page_title="Write any code",
8
- page_icon="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f4/Code.org_logo.svg/1200px-Code.org_logo.svg.png",
9
- layout="centered",
10
- )
11
-
12
- # Custom CSS for Logo & UI
13
- st.markdown("""
14
- <style>
15
- /* Full Center Alignment */
16
- .main-container {
17
- display: flex;
18
- flex-direction: column;
19
- justify-content: center;
20
- align-items: center;
21
- height: 90vh;
22
- }
23
-
24
- /* Logo Centered */
25
- .logo {
26
- display: flex;
27
- justify-content: center;
28
- align-items: center;
29
- width: 100%;
30
- margin-bottom: 10px;
31
- }
32
-
33
- /* Title Centered */
34
- .title {
35
- text-align: center;
36
- font-size: 28px;
37
- font-weight: bold;
38
- color: #03dac6;
39
- }
40
-
41
- /* Chatbox UI */
42
- .chat-container {
43
- border-radius: 8px;
44
- padding: 10px;
45
- margin-bottom: 10px;
46
- background-color: #262626;
47
- color: white;
48
- }
49
-
50
- /* User & AI Message Styling */
51
- .user-message { border-left: 5px solid #03dac6; padding-left: 10px; }
52
- .ai-message { border-left: 5px solid #ff9800; padding-left: 10px; }
53
- pre { white-space: pre-wrap; word-wrap: break-word; }
54
-
55
- /* Search Bar Styling */
56
- .search-container {
57
- display: flex;
58
- align-items: center;
59
- width: 100%;
60
- max-width: 600px;
61
- margin: auto;
62
- }
63
-
64
- .search-input {
65
- flex-grow: 1;
66
- padding: 12px 15px;
67
- border-radius: 25px;
68
- border: 2px solid #03dac6;
69
- background-color: #262626;
70
- color: white;
71
- font-size: 16px;
72
- outline: none;
73
- }
74
-
75
- /* Search Button */
76
- .search-button {
77
- width: 50px;
78
- height: 50px;
79
- border-radius: 50%;
80
- border: none;
81
- background-color: #00ffcc;
82
- color: black;
83
- font-size: 16px;
84
- cursor: pointer;
85
- transition: 0.3s;
86
- text-align: center;
87
- }
88
-
89
- .search-button:hover {
90
- background-color: #02c2ad;
91
- }
92
- </style>
93
- """, unsafe_allow_html=True)
94
-
95
- # Logo
96
- st.markdown(
97
- '<div class="logo"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f4/Code.org_logo.svg/1200px-Code.org_logo.svg.png" width="80"></div>',
98
- unsafe_allow_html=True
99
- )
100
-
101
- # Title
102
- st.markdown('<div class="title">Write any code: Verbose❇️</div>', unsafe_allow_html=True)
103
-
104
- # Chat history
105
- if "messages" not in st.session_state:
106
- st.session_state.messages = []
107
-
108
- for msg in st.session_state.messages:
109
- if msg["role"] == "user":
110
- st.markdown(f'<div class="chat-container user-message"><b>User:</b><br>{msg["content"]}</div>',
111
- unsafe_allow_html=True)
112
- elif msg["role"] == "ai":
113
- explanation, code = msg["content"]
114
- st.markdown(f'<div class="chat-container ai-message"><b>AI:</b><br>{explanation}</div>', unsafe_allow_html=True)
115
- if code:
116
- # st.subheader("Generated Code:")
117
- st.code(code, language="python")
118
-
119
- # Input Form
120
- st.markdown('<div class="search-container">', unsafe_allow_html=True)
121
-
122
- with st.form(key="input_form"):
123
- user_input = st.text_input("Your Prompt:", key="user_prompt", placeholder="Enter your prompt...")
124
-
125
- # Single send button
126
- submit_button = st.form_submit_button("SEND ↗️")
127
-
128
- st.markdown("</div>", unsafe_allow_html=True)
129
-
130
- # API Call Logic
131
- if submit_button:
132
- if user_input.strip():
133
- st.session_state.messages.append({"role": "user", "content": user_input})
134
- try:
135
- response = requests.post("http://127.0.0.1:8000/generate/", json={"prompt": user_input, "response_type": "both"})
136
- if response.status_code == 200:
137
- data = response.json()
138
- raw_text = data.get("response", "").strip()
139
- if not raw_text or raw_text.lower() == "none":
140
- st.warning("⚠️ No valid response generated. Try a different prompt.")
141
- else:
142
- explanation, code = raw_text, None
143
- code_match = re.search(r"```(?:python)?\n(.*?)\n```", raw_text, re.DOTALL)
144
- if code_match:
145
- code = code_match.group(1).strip()
146
- explanation = raw_text.replace(code_match.group(0), "").strip()
147
- st.session_state.messages.append({"role": "ai", "content": (explanation, code)})
148
- st.rerun()
149
- else:
150
- st.error(f"API Error: {response.status_code} - {response.text}")
151
- except requests.exceptions.ConnectionError:
152
- st.error("⚠️ Cannot connect to backend! Make sure FastAPI is running.")
153
- else:
154
- st.warning("Write something in input cell.")
 
 
1
+ BACKEND_URL = "https://shiva9876-code-gen-app.hf.space"
2
+ import streamlit as st
3
+ import requests
4
+ import re
5
+
6
+ # Page Config with Logo
7
+ st.set_page_config(
8
+ page_title="Write any code",
9
+ page_icon="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f4/Code.org_logo.svg/1200px-Code.org_logo.svg.png",
10
+ layout="centered",
11
+ )
12
+
13
+ # Custom CSS for Logo & UI
14
+ st.markdown("""
15
+ <style>
16
+ /* Full Center Alignment */
17
+ .main-container {
18
+ display: flex;
19
+ flex-direction: column;
20
+ justify-content: center;
21
+ align-items: center;
22
+ height: 90vh;
23
+ }
24
+
25
+ /* Logo Centered */
26
+ .logo {
27
+ display: flex;
28
+ justify-content: center;
29
+ align-items: center;
30
+ width: 100%;
31
+ margin-bottom: 10px;
32
+ }
33
+
34
+ /* Title Centered */
35
+ .title {
36
+ text-align: center;
37
+ font-size: 28px;
38
+ font-weight: bold;
39
+ color: #03dac6;
40
+ }
41
+
42
+ /* Chatbox UI */
43
+ .chat-container {
44
+ border-radius: 8px;
45
+ padding: 10px;
46
+ margin-bottom: 10px;
47
+ background-color: #262626;
48
+ color: white;
49
+ }
50
+
51
+ /* User & AI Message Styling */
52
+ .user-message { border-left: 5px solid #03dac6; padding-left: 10px; }
53
+ .ai-message { border-left: 5px solid #ff9800; padding-left: 10px; }
54
+ pre { white-space: pre-wrap; word-wrap: break-word; }
55
+
56
+ /* Search Bar Styling */
57
+ .search-container {
58
+ display: flex;
59
+ align-items: center;
60
+ width: 100%;
61
+ max-width: 600px;
62
+ margin: auto;
63
+ }
64
+
65
+ .search-input {
66
+ flex-grow: 1;
67
+ padding: 12px 15px;
68
+ border-radius: 25px;
69
+ border: 2px solid #03dac6;
70
+ background-color: #262626;
71
+ color: white;
72
+ font-size: 16px;
73
+ outline: none;
74
+ }
75
+
76
+ /* Search Button */
77
+ .search-button {
78
+ width: 50px;
79
+ height: 50px;
80
+ border-radius: 50%;
81
+ border: none;
82
+ background-color: #00ffcc;
83
+ color: black;
84
+ font-size: 16px;
85
+ cursor: pointer;
86
+ transition: 0.3s;
87
+ text-align: center;
88
+ }
89
+
90
+ .search-button:hover {
91
+ background-color: #02c2ad;
92
+ }
93
+ </style>
94
+ """, unsafe_allow_html=True)
95
+
96
+ # Logo
97
+ st.markdown(
98
+ '<div class="logo"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f4/Code.org_logo.svg/1200px-Code.org_logo.svg.png" width="80"></div>',
99
+ unsafe_allow_html=True
100
+ )
101
+
102
+ # Title
103
+ st.markdown('<div class="title">Write any code: Verbose❇️</div>', unsafe_allow_html=True)
104
+
105
+ # Chat history
106
+ if "messages" not in st.session_state:
107
+ st.session_state.messages = []
108
+
109
+ for msg in st.session_state.messages:
110
+ if msg["role"] == "user":
111
+ st.markdown(f'<div class="chat-container user-message"><b>User:</b><br>{msg["content"]}</div>',
112
+ unsafe_allow_html=True)
113
+ elif msg["role"] == "ai":
114
+ explanation, code = msg["content"]
115
+ st.markdown(f'<div class="chat-container ai-message"><b>AI:</b><br>{explanation}</div>', unsafe_allow_html=True)
116
+ if code:
117
+ # st.subheader("Generated Code:")
118
+ st.code(code, language="python")
119
+
120
+ # Input Form
121
+ st.markdown('<div class="search-container">', unsafe_allow_html=True)
122
+
123
+ with st.form(key="input_form"):
124
+ user_input = st.text_input("Your Prompt:", key="user_prompt", placeholder="Enter your prompt...")
125
+
126
+ # Single send button
127
+ submit_button = st.form_submit_button("SEND ↗️")
128
+
129
+ st.markdown("</div>", unsafe_allow_html=True)
130
+
131
+ # API Call Logic
132
+ if submit_button:
133
+ if user_input.strip():
134
+ st.session_state.messages.append({"role": "user", "content": user_input})
135
+ try:
136
+ response = requests.post("http://127.0.0.1:8000/generate/", json={"prompt": user_input, "response_type": "both"})
137
+ if response.status_code == 200:
138
+ data = response.json()
139
+ raw_text = data.get("response", "").strip()
140
+ if not raw_text or raw_text.lower() == "none":
141
+ st.warning("⚠️ No valid response generated. Try a different prompt.")
142
+ else:
143
+ explanation, code = raw_text, None
144
+ code_match = re.search(r"```(?:python)?\n(.*?)\n```", raw_text, re.DOTALL)
145
+ if code_match:
146
+ code = code_match.group(1).strip()
147
+ explanation = raw_text.replace(code_match.group(0), "").strip()
148
+ st.session_state.messages.append({"role": "ai", "content": (explanation, code)})
149
+ st.rerun()
150
+ else:
151
+ st.error(f"API Error: {response.status_code} - {response.text}")
152
+ except requests.exceptions.ConnectionError:
153
+ st.error("⚠️ Cannot connect to backend! Make sure FastAPI is running.")
154
+ else:
155
+ st.warning("Write something in input cell.")