File size: 5,672 Bytes
d1a9496 dac3f07 d1a9496 ae6c842 d1a9496 ae6c842 d1a9496 dac3f07 d1a9496 a0e137a d1a9496 a0e137a d1a9496 a0e137a d1a9496 a0e137a d1a9496 a0e137a d1a9496 dac3f07 d1a9496 ae6c842 d1a9496 a0e137a d1a9496 fa0996d d1a9496 fa0996d d1a9496 a0e137a d1a9496 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
from fastapi import FastAPI, HTTPException
from fastapi.responses import HTMLResponse
from transformers import AutoModelForCausalLM, AutoTokenizer
app = FastAPI(title="Chatbot")
# Load the chatbot model (DialoGPT-medium) at startup
model_name = "microsoft/DialoGPT-medium"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
# Function to generate chatbot response
def get_chatbot_response(user_input: str, max_length=100):
if not user_input:
return "Please say something!"
# Encode the input and generate a response
input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors="pt")
chat_history_ids = model.generate(
input_ids,
max_length=max_length,
pad_token_id=tokenizer.eos_token_id,
no_repeat_ngram_size=3,
do_sample=True,
top_k=50,
top_p=0.95,
temperature=0.8
)
# Decode the response, skipping the input part
response = tokenizer.decode(chat_history_ids[:, input_ids.shape[-1]:][0], skip_special_tokens=True)
return response.strip()
# HTML, CSS, and JS embedded
HTML_CONTENT = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chatbot</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f2f5;
margin: 0;
padding: 20px;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
.container {
max-width: 800px;
width: 100%;
padding: 2rem;
background: white;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
h1 {
color: #2c3e50;
text-align: center;
margin-bottom: 2rem;
}
.chat-area {
max-height: 400px;
overflow-y: auto;
margin-bottom: 1.5rem;
padding: 1rem;
background: #f9f9f9;
border: 2px solid #eee;
border-radius: 5px;
}
.message {
margin: 0.5rem 0;
padding: 0.8rem;
border-radius: 5px;
}
.user-message {
background-color: #3498db;
color: white;
margin-left: 20%;
text-align: right;
}
.bot-message {
background-color: #ecf0f1;
color: #2c3e50;
margin-right: 20%;
}
.input-section {
display: flex;
gap: 1rem;
}
input[type="text"] {
flex: 1;
padding: 0.8rem;
border: 2px solid #ddd;
border-radius: 5px;
font-size: 1rem;
}
button {
padding: 0.8rem 1.5rem;
background-color: #3498db;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 1rem;
transition: background-color 0.3s;
}
button:hover {
background-color: #2980b9;
}
</style>
</head>
<body>
<div class="container">
<h1>Chatbot</h1>
<div class="chat-area" id="chatArea"></div>
<div class="input-section">
<input type="text" id="userInput" placeholder="Type your message..." onkeypress="if(event.key === 'Enter') sendMessage();">
<button onclick="sendMessage()">Send</button>
</div>
</div>
<script>
const chatArea = document.getElementById("chatArea");
const userInput = document.getElementById("userInput");
function addMessage(text, isUser = false) {
const messageDiv = document.createElement("div");
messageDiv.className = "message " + (isUser ? "user-message" : "bot-message");
messageDiv.textContent = text;
chatArea.appendChild(messageDiv);
chatArea.scrollTop = chatArea.scrollHeight;
}
async function sendMessage() {
const text = userInput.value.trim();
if (!text) return;
addMessage(text, true);
userInput.value = "";
addMessage("Thinking...");
try {
const response = await fetch("/chat", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ message: text })
});
const data = await response.json();
if (!response.ok) throw new Error(data.detail || "Chat error");
chatArea.lastChild.remove();
addMessage(data.response);
} catch (error) {
chatArea.lastChild.remove();
addMessage(`Error: ${error.message}`);
}
}
</script>
</body>
</html>
"""
@app.get("/", response_class=HTMLResponse)
async def read_root():
return HTML_CONTENT
@app.post("/chat")
async def chat_endpoint(data: dict):
message = data.get("message", "")
if not message:
raise HTTPException(status_code=400, detail="No message provided")
try:
response = get_chatbot_response(message)
return {"response": response}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Chat error: {str(e)}")
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860) |