Spaces:
Sleeping
Sleeping
Rename index.html to app.py
Browse files- app.py +46 -0
- index.html +0 -256
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import os
|
4 |
+
|
5 |
+
# Your API key logic
|
6 |
+
OPENROUTER_API_KEY = os.environ.get("OPENROUTER_API_KEY")
|
7 |
+
|
8 |
+
def generate_response(message, history):
|
9 |
+
# (Same function as above)
|
10 |
+
if not OPENROUTER_API_KEY:
|
11 |
+
return "Error: OPENROUTER_API_KEY not set."
|
12 |
+
|
13 |
+
url = "https://openrouter.ai/api/v1/chat/completions"
|
14 |
+
headers = {
|
15 |
+
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
|
16 |
+
"Content-Type": "application/json"
|
17 |
+
}
|
18 |
+
|
19 |
+
messages = [{"role": "system", "content": "You are a helpful AI assistant."}]
|
20 |
+
for user_msg, bot_msg in history:
|
21 |
+
messages.append({"role": "user", "content": user_msg})
|
22 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
23 |
+
messages.append({"role": "user", "content": message})
|
24 |
+
|
25 |
+
data = {
|
26 |
+
"model": "openai/gpt-3.5-turbo",
|
27 |
+
"messages": messages,
|
28 |
+
}
|
29 |
+
|
30 |
+
try:
|
31 |
+
response = requests.post(url, headers=headers, json=data)
|
32 |
+
response.raise_for_status()
|
33 |
+
result = response.json()
|
34 |
+
return result['choices'][0]['message']['content']
|
35 |
+
|
36 |
+
except requests.exceptions.RequestException as e:
|
37 |
+
return f"An error occurred: {e}"
|
38 |
+
|
39 |
+
# Use the Glass theme
|
40 |
+
demo = gr.ChatInterface(
|
41 |
+
generate_response,
|
42 |
+
theme=gr.themes.Glass(),
|
43 |
+
title="My Glassy Chatbot"
|
44 |
+
)
|
45 |
+
|
46 |
+
demo.launch()
|
index.html
DELETED
@@ -1,256 +0,0 @@
|
|
1 |
-
<!DOCTYPE html>
|
2 |
-
<html lang="en">
|
3 |
-
<head>
|
4 |
-
<meta charset="UTF-8">
|
5 |
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
-
<title>My Chatbot</title>
|
7 |
-
<style>
|
8 |
-
body {
|
9 |
-
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif;
|
10 |
-
margin: 0;
|
11 |
-
padding: 0;
|
12 |
-
background-color: #f7f7f7;
|
13 |
-
display: flex;
|
14 |
-
justify-content: center;
|
15 |
-
align-items: center;
|
16 |
-
height: 100vh;
|
17 |
-
}
|
18 |
-
|
19 |
-
.chatbot-container {
|
20 |
-
width: 90%;
|
21 |
-
max-width: 900px;
|
22 |
-
height: 90vh;
|
23 |
-
display: flex;
|
24 |
-
flex-direction: column;
|
25 |
-
background-color: #fff;
|
26 |
-
border-radius: 12px;
|
27 |
-
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
28 |
-
overflow: hidden;
|
29 |
-
}
|
30 |
-
|
31 |
-
.chatbot-header {
|
32 |
-
display: flex;
|
33 |
-
justify-content: space-between;
|
34 |
-
align-items: center;
|
35 |
-
padding: 16px 24px;
|
36 |
-
border-bottom: 1px solid #e5e5e5;
|
37 |
-
}
|
38 |
-
|
39 |
-
.chatbot-logo {
|
40 |
-
font-weight: 600;
|
41 |
-
font-size: 1.25rem;
|
42 |
-
color: #333;
|
43 |
-
}
|
44 |
-
|
45 |
-
.chatbot-info .status {
|
46 |
-
color: #666;
|
47 |
-
font-size: 0.8rem;
|
48 |
-
margin-right: 10px;
|
49 |
-
}
|
50 |
-
|
51 |
-
.plus-button {
|
52 |
-
background-color: #1a73e8;
|
53 |
-
color: #fff;
|
54 |
-
border: none;
|
55 |
-
padding: 8px 16px;
|
56 |
-
border-radius: 20px;
|
57 |
-
font-size: 0.8rem;
|
58 |
-
cursor: pointer;
|
59 |
-
}
|
60 |
-
|
61 |
-
.chatbot-main {
|
62 |
-
flex-grow: 1;
|
63 |
-
display: flex;
|
64 |
-
flex-direction: column;
|
65 |
-
justify-content: center;
|
66 |
-
align-items: center;
|
67 |
-
text-align: center;
|
68 |
-
padding: 20px;
|
69 |
-
overflow-y: auto;
|
70 |
-
}
|
71 |
-
|
72 |
-
.intro-message {
|
73 |
-
max-width: 500px;
|
74 |
-
margin-bottom: 20px;
|
75 |
-
color: #444;
|
76 |
-
}
|
77 |
-
|
78 |
-
.intro-message h2 {
|
79 |
-
font-size: 2rem;
|
80 |
-
font-weight: 600;
|
81 |
-
margin-bottom: 10px;
|
82 |
-
}
|
83 |
-
|
84 |
-
.chat-area {
|
85 |
-
width: 100%;
|
86 |
-
max-width: 700px;
|
87 |
-
display: flex;
|
88 |
-
flex-direction: column;
|
89 |
-
gap: 15px;
|
90 |
-
overflow-y: auto;
|
91 |
-
padding: 20px 0;
|
92 |
-
}
|
93 |
-
|
94 |
-
.message {
|
95 |
-
padding: 10px 15px;
|
96 |
-
border-radius: 18px;
|
97 |
-
max-width: 70%;
|
98 |
-
word-wrap: break-word;
|
99 |
-
}
|
100 |
-
|
101 |
-
.user-message {
|
102 |
-
background-color: #f0f0f0;
|
103 |
-
align-self: flex-end;
|
104 |
-
}
|
105 |
-
|
106 |
-
.bot-message {
|
107 |
-
background-color: #e6f7ff;
|
108 |
-
align-self: flex-start;
|
109 |
-
}
|
110 |
-
|
111 |
-
.chatbot-input-form {
|
112 |
-
padding: 16px 24px;
|
113 |
-
border-top: 1px solid #e5e5e5;
|
114 |
-
}
|
115 |
-
|
116 |
-
.input-container {
|
117 |
-
display: flex;
|
118 |
-
border: 1px solid #e5e5e5;
|
119 |
-
border-radius: 24px;
|
120 |
-
padding: 8px;
|
121 |
-
background-color: #f7f7f7;
|
122 |
-
}
|
123 |
-
|
124 |
-
#chat-input {
|
125 |
-
flex-grow: 1;
|
126 |
-
border: none;
|
127 |
-
background: transparent;
|
128 |
-
font-size: 1rem;
|
129 |
-
padding: 0 10px;
|
130 |
-
}
|
131 |
-
|
132 |
-
#chat-input:focus {
|
133 |
-
outline: none;
|
134 |
-
}
|
135 |
-
|
136 |
-
.send-button {
|
137 |
-
background-color: #1a73e8;
|
138 |
-
border: none;
|
139 |
-
border-radius: 50%;
|
140 |
-
width: 36px;
|
141 |
-
height: 36px;
|
142 |
-
display: flex;
|
143 |
-
justify-content: center;
|
144 |
-
align-items: center;
|
145 |
-
cursor: pointer;
|
146 |
-
}
|
147 |
-
|
148 |
-
.send-button svg {
|
149 |
-
fill: #fff;
|
150 |
-
transform: rotate(45deg);
|
151 |
-
}
|
152 |
-
</style>
|
153 |
-
</head>
|
154 |
-
<body>
|
155 |
-
<div class="chatbot-container">
|
156 |
-
<header class="chatbot-header">
|
157 |
-
<div class="chatbot-logo">ChatGPT</div>
|
158 |
-
<div class="chatbot-info">
|
159 |
-
<span class="status">Saved memory full</span>
|
160 |
-
<button class="plus-button">+ Get Plus</button>
|
161 |
-
</div>
|
162 |
-
</header>
|
163 |
-
|
164 |
-
<main class="chatbot-main">
|
165 |
-
<div class="intro-message">
|
166 |
-
<h2>Introducing GPT-5</h2>
|
167 |
-
<p>ChatGPT now has our smartest, fastest, most useful model yet, with thinking built in — so you get the best answer, every time.</p>
|
168 |
-
</div>
|
169 |
-
<div class="chat-area">
|
170 |
-
</div>
|
171 |
-
</main>
|
172 |
-
|
173 |
-
<form class="chatbot-input-form" id="chat-form">
|
174 |
-
<div class="input-container">
|
175 |
-
<input type="text" id="chat-input" placeholder="Ask anything" autocomplete="off">
|
176 |
-
<button type="submit" class="send-button">
|
177 |
-
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" class="text-white dark:text-black">
|
178 |
-
<path d="M12 4L3 9L12 14L21 9L12 4ZM12 16V22L21 9L12 16Z" fill="currentColor"></path>
|
179 |
-
</svg>
|
180 |
-
</button>
|
181 |
-
</div>
|
182 |
-
</form>
|
183 |
-
</div>
|
184 |
-
|
185 |
-
<script>
|
186 |
-
document.addEventListener('DOMContentLoaded', () => {
|
187 |
-
const chatForm = document.getElementById('chat-form');
|
188 |
-
const chatInput = document.getElementById('chat-input');
|
189 |
-
const chatArea = document.querySelector('.chat-area');
|
190 |
-
const introMessage = document.querySelector('.intro-message');
|
191 |
-
|
192 |
-
// Replace with your OpenRouter API key
|
193 |
-
const OPENROUTER_API_KEY = 'YOUR_OPENROUTER_API_KEY';
|
194 |
-
|
195 |
-
const MODEL_NAME = 'gpt-3.5-turbo';
|
196 |
-
|
197 |
-
const appendMessage = (message, sender) => {
|
198 |
-
const messageDiv = document.createElement('div');
|
199 |
-
messageDiv.classList.add('message');
|
200 |
-
messageDiv.classList.add(`${sender}-message`);
|
201 |
-
messageDiv.textContent = message;
|
202 |
-
chatArea.appendChild(messageDiv);
|
203 |
-
chatArea.scrollTop = chatArea.scrollHeight;
|
204 |
-
};
|
205 |
-
|
206 |
-
const sendMessage = async (userMessage) => {
|
207 |
-
// Hide the intro message when the first message is sent
|
208 |
-
if (introMessage) {
|
209 |
-
introMessage.style.display = 'none';
|
210 |
-
}
|
211 |
-
|
212 |
-
appendMessage(userMessage, 'user');
|
213 |
-
chatInput.value = '';
|
214 |
-
|
215 |
-
try {
|
216 |
-
const response = await fetch('https://openrouter.ai/api/v1/chat/completions', {
|
217 |
-
method: 'POST',
|
218 |
-
headers: {
|
219 |
-
'Authorization': `Bearer ${OPENROUTER_API_KEY}`,
|
220 |
-
'Content-Type': 'application/json',
|
221 |
-
},
|
222 |
-
body: JSON.stringify({
|
223 |
-
model: MODEL_NAME,
|
224 |
-
messages: [
|
225 |
-
{ role: 'system', content: 'You are a helpful assistant.' },
|
226 |
-
{ role: 'user', content: userMessage }
|
227 |
-
],
|
228 |
-
}),
|
229 |
-
});
|
230 |
-
|
231 |
-
if (!response.ok) {
|
232 |
-
const errorData = await response.json();
|
233 |
-
throw new Error(errorData.error.message || `API error: ${response.statusText}`);
|
234 |
-
}
|
235 |
-
|
236 |
-
const data = await response.json();
|
237 |
-
const botMessage = data.choices[0].message.content;
|
238 |
-
appendMessage(botMessage, 'bot');
|
239 |
-
|
240 |
-
} catch (error) {
|
241 |
-
console.error('Error:', error);
|
242 |
-
appendMessage('Sorry, I am unable to connect right now. Please try again later.', 'bot');
|
243 |
-
}
|
244 |
-
};
|
245 |
-
|
246 |
-
chatForm.addEventListener('submit', (e) => {
|
247 |
-
e.preventDefault();
|
248 |
-
const userMessage = chatInput.value.trim();
|
249 |
-
if (userMessage) {
|
250 |
-
sendMessage(userMessage);
|
251 |
-
}
|
252 |
-
});
|
253 |
-
});
|
254 |
-
</script>
|
255 |
-
</body>
|
256 |
-
</html>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|