aydayya
commited on
Commit
·
f1a8d62
1
Parent(s):
f35e5d4
add file
Browse files- Dockerfile +3 -0
- app.py +106 -0
Dockerfile
CHANGED
@@ -16,5 +16,8 @@ RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
|
16 |
# Copy application files to the container
|
17 |
COPY --chown=user . /app
|
18 |
|
|
|
|
|
|
|
19 |
# Command to run FastAPI using Uvicorn
|
20 |
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
|
|
16 |
# Copy application files to the container
|
17 |
COPY --chown=user . /app
|
18 |
|
19 |
+
# Expose the port that the app will run on
|
20 |
+
EXPOSE 7860
|
21 |
+
|
22 |
# Command to run FastAPI using Uvicorn
|
23 |
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
CHANGED
@@ -1,9 +1,115 @@
|
|
1 |
from fastapi import FastAPI
|
|
|
2 |
from ollama import chat
|
3 |
from ollama import ChatResponse
|
4 |
|
5 |
app = FastAPI()
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
# Route to interact with Ollama's chatbot
|
8 |
@app.get("/chat/{message}")
|
9 |
async def chat(message: str):
|
|
|
1 |
from fastapi import FastAPI
|
2 |
+
from fastapi.responses import HTMLResponse
|
3 |
from ollama import chat
|
4 |
from ollama import ChatResponse
|
5 |
|
6 |
app = FastAPI()
|
7 |
|
8 |
+
# Route to render the chat UI (HTML page)
|
9 |
+
@app.get("/", response_class=HTMLResponse)
|
10 |
+
async def get_chat_ui():
|
11 |
+
return """
|
12 |
+
<!DOCTYPE html>
|
13 |
+
<html lang="en">
|
14 |
+
<head>
|
15 |
+
<meta charset="UTF-8">
|
16 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
17 |
+
<title>Chat with IA</title>
|
18 |
+
<style>
|
19 |
+
body {
|
20 |
+
font-family: Arial, sans-serif;
|
21 |
+
background-color: #f4f4f9;
|
22 |
+
display: flex;
|
23 |
+
justify-content: center;
|
24 |
+
align-items: center;
|
25 |
+
height: 100vh;
|
26 |
+
margin: 0;
|
27 |
+
}
|
28 |
+
.chat-container {
|
29 |
+
width: 400px;
|
30 |
+
background-color: white;
|
31 |
+
border-radius: 10px;
|
32 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
33 |
+
padding: 20px;
|
34 |
+
display: flex;
|
35 |
+
flex-direction: column;
|
36 |
+
height: 500px;
|
37 |
+
}
|
38 |
+
.messages {
|
39 |
+
flex: 1;
|
40 |
+
overflow-y: auto;
|
41 |
+
margin-bottom: 10px;
|
42 |
+
}
|
43 |
+
.message {
|
44 |
+
padding: 8px;
|
45 |
+
margin: 5px;
|
46 |
+
border-radius: 5px;
|
47 |
+
background-color: #f1f1f1;
|
48 |
+
}
|
49 |
+
.message.bot {
|
50 |
+
background-color: #e3f2fd;
|
51 |
+
align-self: flex-start;
|
52 |
+
}
|
53 |
+
.message.user {
|
54 |
+
background-color: #c8e6c9;
|
55 |
+
align-self: flex-end;
|
56 |
+
}
|
57 |
+
input[type="text"] {
|
58 |
+
padding: 10px;
|
59 |
+
border-radius: 5px;
|
60 |
+
border: 1px solid #ddd;
|
61 |
+
width: 100%;
|
62 |
+
box-sizing: border-box;
|
63 |
+
}
|
64 |
+
button {
|
65 |
+
background-color: #007bff;
|
66 |
+
color: white;
|
67 |
+
padding: 10px;
|
68 |
+
border: none;
|
69 |
+
border-radius: 5px;
|
70 |
+
cursor: pointer;
|
71 |
+
}
|
72 |
+
</style>
|
73 |
+
</head>
|
74 |
+
<body>
|
75 |
+
<div class="chat-container">
|
76 |
+
<div class="messages" id="messages">
|
77 |
+
<!-- Chat messages will appear here -->
|
78 |
+
</div>
|
79 |
+
<input type="text" id="userMessage" placeholder="Type your message..." />
|
80 |
+
<button onclick="sendMessage()">Send</button>
|
81 |
+
</div>
|
82 |
+
<script>
|
83 |
+
async function sendMessage() {
|
84 |
+
const userMessage = document.getElementById('userMessage').value;
|
85 |
+
if (!userMessage) return;
|
86 |
+
|
87 |
+
// Show the user's message
|
88 |
+
addMessage(userMessage, 'user');
|
89 |
+
|
90 |
+
// Send the message to FastAPI endpoint
|
91 |
+
const response = await fetch(`/chat/${encodeURIComponent(userMessage)}`);
|
92 |
+
const data = await response.json();
|
93 |
+
const botMessage = data.response;
|
94 |
+
|
95 |
+
// Show the bot's response
|
96 |
+
addMessage(botMessage, 'bot');
|
97 |
+
|
98 |
+
// Clear the input field
|
99 |
+
document.getElementById('userMessage').value = '';
|
100 |
+
}
|
101 |
+
|
102 |
+
function addMessage(message, sender) {
|
103 |
+
const messageContainer = document.createElement('div');
|
104 |
+
messageContainer.classList.add('message', sender);
|
105 |
+
messageContainer.textContent = message;
|
106 |
+
document.getElementById('messages').appendChild(messageContainer);
|
107 |
+
}
|
108 |
+
</script>
|
109 |
+
</body>
|
110 |
+
</html>
|
111 |
+
"""
|
112 |
+
|
113 |
# Route to interact with Ollama's chatbot
|
114 |
@app.get("/chat/{message}")
|
115 |
async def chat(message: str):
|