Spaces:
Running
Running
Upload 5 files
Browse files- app.py +47 -0
- requirements.txt +5 -0
- static/css/style.css +43 -0
- static/js/script.js +44 -0
- templates/index.html +19 -0
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import shutil
|
3 |
+
from flask import Flask, render_template, request, jsonify
|
4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
5 |
+
import torch
|
6 |
+
|
7 |
+
app = Flask(__name__)
|
8 |
+
|
9 |
+
# Kill any process using port 7860
|
10 |
+
def kill_port(port):
|
11 |
+
for proc in psutil.process_iter(attrs=["pid", "connections"]):
|
12 |
+
for conn in proc.info["connections"]:
|
13 |
+
if conn.laddr.port == port:
|
14 |
+
os.kill(proc.info["pid"], 9)
|
15 |
+
|
16 |
+
kill_port(7860) # Ensure Flask doesn't crash due to a used port
|
17 |
+
|
18 |
+
# Define cache directory
|
19 |
+
cache_dir = os.path.expanduser("~/.cache/huggingface")
|
20 |
+
|
21 |
+
# Load Myanmarsar-GPT (1.42B params) from Hugging Face
|
22 |
+
MODEL_NAME = "simbolo-ai/Myanmarsar-GPT"
|
23 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, cache_dir=cache_dir)
|
24 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, cache_dir=cache_dir)
|
25 |
+
|
26 |
+
# Function to generate chatbot responses
|
27 |
+
def generate_response(prompt):
|
28 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
29 |
+
with torch.no_grad():
|
30 |
+
output = model.generate(**inputs, max_length=200)
|
31 |
+
return tokenizer.decode(output[0], skip_special_tokens=True)
|
32 |
+
|
33 |
+
# Serve the HTML page
|
34 |
+
@app.route("/")
|
35 |
+
def home():
|
36 |
+
return render_template("index.html")
|
37 |
+
|
38 |
+
# API route for chatbot responses
|
39 |
+
@app.route("/chat", methods=["POST"])
|
40 |
+
def chat():
|
41 |
+
user_message = request.json.get("message", "")
|
42 |
+
bot_reply = generate_response(user_message)
|
43 |
+
return jsonify({"reply": bot_reply})
|
44 |
+
|
45 |
+
if __name__ == "__main__":
|
46 |
+
port = int(os.environ.get("PORT", 7860)) # Default to 7860, but use any assigned port
|
47 |
+
app.run(host="0.0.0.0", port=port)
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
flask
|
2 |
+
torch
|
3 |
+
transformers==4.37.0
|
4 |
+
accelerate
|
5 |
+
psutil
|
static/css/style.css
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/* static/css/style.css */
|
2 |
+
body {
|
3 |
+
font-family: Arial, sans-serif;
|
4 |
+
display: flex;
|
5 |
+
justify-content: center;
|
6 |
+
align-items: center;
|
7 |
+
height: 100vh;
|
8 |
+
background-color: #f5f5f5;
|
9 |
+
}
|
10 |
+
.chat-container {
|
11 |
+
width: 90%;
|
12 |
+
max-width: 400px;
|
13 |
+
background: white;
|
14 |
+
border-radius: 10px;
|
15 |
+
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
|
16 |
+
overflow: hidden;
|
17 |
+
}
|
18 |
+
.chat-box {
|
19 |
+
height: 400px;
|
20 |
+
overflow-y: auto;
|
21 |
+
padding: 10px;
|
22 |
+
border-bottom: 1px solid #ccc;
|
23 |
+
}
|
24 |
+
.input-area {
|
25 |
+
display: flex;
|
26 |
+
padding: 10px;
|
27 |
+
background: #eee;
|
28 |
+
}
|
29 |
+
input {
|
30 |
+
flex: 1;
|
31 |
+
padding: 8px;
|
32 |
+
border: none;
|
33 |
+
border-radius: 5px;
|
34 |
+
}
|
35 |
+
button {
|
36 |
+
margin-left: 5px;
|
37 |
+
padding: 8px 10px;
|
38 |
+
background: blue;
|
39 |
+
color: white;
|
40 |
+
border: none;
|
41 |
+
border-radius: 5px;
|
42 |
+
cursor: pointer;
|
43 |
+
}
|
static/js/script.js
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
document.getElementById("send-button").addEventListener("click", function() {
|
2 |
+
sendMessage();
|
3 |
+
});
|
4 |
+
|
5 |
+
document.getElementById("user-input").addEventListener("keypress", function(event) {
|
6 |
+
if (event.key === "Enter") {
|
7 |
+
sendMessage();
|
8 |
+
}
|
9 |
+
});
|
10 |
+
|
11 |
+
const micButton = document.getElementById("mic-button");
|
12 |
+
const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
|
13 |
+
recognition.lang = "my-MM"; // Set to Burmese
|
14 |
+
recognition.continuous = false;
|
15 |
+
|
16 |
+
micButton.addEventListener("click", () => {
|
17 |
+
recognition.start();
|
18 |
+
});
|
19 |
+
|
20 |
+
recognition.onresult = (event) => {
|
21 |
+
document.getElementById("user-input").value = event.results[0][0].transcript;
|
22 |
+
};
|
23 |
+
|
24 |
+
function sendMessage() {
|
25 |
+
let userMessage = document.getElementById("user-input").value;
|
26 |
+
let chatbox = document.getElementById("chatbox");
|
27 |
+
|
28 |
+
if (userMessage.trim() === "") return;
|
29 |
+
|
30 |
+
// Display user message
|
31 |
+
chatbox.innerHTML += `<p><strong>You:</strong> ${userMessage}</p>`;
|
32 |
+
document.getElementById("user-input").value = "";
|
33 |
+
|
34 |
+
fetch("/chat", {
|
35 |
+
method: "POST",
|
36 |
+
headers: { "Content-Type": "application/json" },
|
37 |
+
body: JSON.stringify({ message: userMessage })
|
38 |
+
})
|
39 |
+
.then(response => response.json())
|
40 |
+
.then(data => {
|
41 |
+
chatbox.innerHTML += `<p><strong>Bot:</strong> ${data.reply}</p>`;
|
42 |
+
})
|
43 |
+
.catch(error => console.error("Error:", error));
|
44 |
+
}
|
templates/index.html
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<html lang="en">
|
2 |
+
<head>
|
3 |
+
<meta charset="UTF-8">
|
4 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
5 |
+
<title>Chatbot UI</title>
|
6 |
+
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
|
7 |
+
</head>
|
8 |
+
<body>
|
9 |
+
<div class="chat-container">
|
10 |
+
<div class="chat-box" id="chatbox"></div>
|
11 |
+
<div class="input-area">
|
12 |
+
<button id="mic-button">🎤</button>
|
13 |
+
<input type="text" id="user-input" placeholder="Type a message...">
|
14 |
+
<button id="send-button">Send</button>
|
15 |
+
</div>
|
16 |
+
</div>
|
17 |
+
<script src="{{ url_for('static', filename='js/script.js') }}"></script>
|
18 |
+
</body>
|
19 |
+
</html>
|