shiva9876 commited on
Commit
6abab29
·
verified ·
1 Parent(s): 7526f9d

Upload frontend.py

Browse files
Files changed (1) hide show
  1. frontend.py +154 -0
frontend.py ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.")