ver3
Browse files- app.py +49 -12
- templates/chat.html +25 -138
app.py
CHANGED
@@ -1,11 +1,11 @@
|
|
1 |
-
from fastapi import FastAPI
|
2 |
-
from fastapi.responses import HTMLResponse
|
3 |
-
from fastapi import FastAPI
|
4 |
-
from fastapi.responses import HTMLResponse
|
5 |
from fastapi.templating import Jinja2Templates
|
6 |
-
from fastapi import Request
|
7 |
from fastapi.staticfiles import StaticFiles
|
8 |
-
|
|
|
|
|
|
|
9 |
|
10 |
app = FastAPI()
|
11 |
|
@@ -14,7 +14,11 @@ templates = Jinja2Templates(directory="templates")
|
|
14 |
|
15 |
# Define el personaje
|
16 |
personaje = "rias"
|
17 |
-
|
|
|
|
|
|
|
|
|
18 |
# Monta la carpeta 'static' para servir archivos estáticos
|
19 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
20 |
|
@@ -33,9 +37,42 @@ async def personaje_detalle(request: Request, personaje: str):
|
|
33 |
|
34 |
}
|
35 |
return templates.TemplateResponse("chat.html", {"request": request, **context})
|
36 |
-
@app.get("/", response_class=HTMLResponse)
|
37 |
-
def read_html():
|
38 |
-
with open("templates/chat.html", "r") as f:
|
39 |
-
html_content = f.read()
|
40 |
-
return html_content
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, Request, Form
|
2 |
+
from fastapi.responses import HTMLResponse, JSONResponse
|
|
|
|
|
3 |
from fastapi.templating import Jinja2Templates
|
|
|
4 |
from fastapi.staticfiles import StaticFiles
|
5 |
+
from fastapi.middleware.cors import CORSMiddleware
|
6 |
+
from pydantic import BaseModel
|
7 |
+
import torch
|
8 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
9 |
|
10 |
app = FastAPI()
|
11 |
|
|
|
14 |
|
15 |
# Define el personaje
|
16 |
personaje = "rias"
|
17 |
+
user="user"
|
18 |
+
chat={
|
19 |
+
personaje: f"hola soy {personaje} no esperaba verte por aqui",
|
20 |
+
user:f"hola "
|
21 |
+
}
|
22 |
# Monta la carpeta 'static' para servir archivos estáticos
|
23 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
24 |
|
|
|
37 |
|
38 |
}
|
39 |
return templates.TemplateResponse("chat.html", {"request": request, **context})
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
+
|
42 |
+
|
43 |
+
|
44 |
+
|
45 |
+
|
46 |
+
model_name = "nakodanei/Blue-Orchid-2x7b"
|
47 |
+
|
48 |
+
# Inicialización global
|
49 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
50 |
+
model = AutoModelForCausalLM.from_pretrained(
|
51 |
+
model_name,
|
52 |
+
device_map="auto",
|
53 |
+
torch_dtype=torch.float16
|
54 |
+
)
|
55 |
+
|
56 |
+
@app.post("/personajes/{personaje}/chat", response_class=HTMLResponse)
|
57 |
+
async def chat_with_character(request: Request, personaje: str, user_input: str):
|
58 |
+
prompt = f"""
|
59 |
+
### Instruction:
|
60 |
+
You are {personaje}, a sexy girl who has been dating the user for 2 months.
|
61 |
+
|
62 |
+
### Input:
|
63 |
+
User: {user_input}
|
64 |
+
|
65 |
+
### Response:
|
66 |
+
Character:
|
67 |
+
"""
|
68 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cuda" if torch.cuda.is_available() else "cpu")
|
69 |
+
outputs = model.generate(
|
70 |
+
**inputs,
|
71 |
+
max_new_tokens=500,
|
72 |
+
pad_token_id=tokenizer.eos_token_id,
|
73 |
+
do_sample=True,
|
74 |
+
temperature=0.7
|
75 |
+
)
|
76 |
+
generated_response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
77 |
+
|
78 |
+
return {"response": generated_response}
|
templates/chat.html
CHANGED
@@ -1,140 +1,27 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
<
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
|
8 |
-
<style>
|
9 |
-
body {
|
10 |
-
background: #F4F7FC;
|
11 |
-
font-family: 'Roboto', sans-serif;
|
12 |
-
margin: 0;
|
13 |
-
padding: 0;
|
14 |
-
display: flex;
|
15 |
-
justify-content: center;
|
16 |
-
align-items: center;
|
17 |
-
height: 100vh;
|
18 |
-
}
|
19 |
-
|
20 |
-
.chat-container {
|
21 |
-
width: 100%;
|
22 |
-
max-width: 900px;
|
23 |
-
height: 90vh;
|
24 |
-
background: #fff;
|
25 |
-
border-radius: 15px;
|
26 |
-
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
27 |
-
display: flex;
|
28 |
-
flex-direction: column;
|
29 |
-
}
|
30 |
-
|
31 |
-
.chat-header {
|
32 |
-
background: #04CB28;
|
33 |
-
border-radius: 15px 15px 0 0;
|
34 |
-
padding: 20px;
|
35 |
-
display: flex;
|
36 |
-
justify-content: space-between;
|
37 |
-
align-items: center;
|
38 |
-
color: #fff;
|
39 |
-
font-weight: bold;
|
40 |
-
}
|
41 |
-
|
42 |
-
.chat-bubble {
|
43 |
-
border-radius: 20px;
|
44 |
-
padding: 12px;
|
45 |
-
font-size: 14px;
|
46 |
-
background: #E2FFE8;
|
47 |
-
margin-bottom: 10px;
|
48 |
-
word-wrap: break-word;
|
49 |
-
}
|
50 |
-
|
51 |
-
.chat-bubble.bg-white {
|
52 |
-
background: #F8F9FA;
|
53 |
-
border: 1px solid #E7E7E9;
|
54 |
-
}
|
55 |
-
|
56 |
-
.message-container {
|
57 |
-
flex-grow: 1;
|
58 |
-
overflow-y: auto;
|
59 |
-
padding: 20px;
|
60 |
-
}
|
61 |
-
|
62 |
-
.message {
|
63 |
-
display: flex;
|
64 |
-
align-items: flex-start;
|
65 |
-
margin-bottom: 15px;
|
66 |
-
}
|
67 |
-
|
68 |
-
.message.right {
|
69 |
-
justify-content: flex-end;
|
70 |
-
}
|
71 |
-
|
72 |
-
.message.left {
|
73 |
-
justify-content: flex-start;
|
74 |
-
}
|
75 |
-
|
76 |
-
.message img {
|
77 |
-
border-radius: 50%;
|
78 |
-
width: 45px;
|
79 |
-
height: 45px;
|
80 |
-
margin-right: 10px;
|
81 |
-
}
|
82 |
-
|
83 |
-
.form-control {
|
84 |
-
width: 100%;
|
85 |
-
border-radius: 12px;
|
86 |
-
border: 1px solid #F0F0F0;
|
87 |
-
padding: 12px;
|
88 |
-
font-size: 14px;
|
89 |
-
margin-top: 10px;
|
90 |
-
resize: none;
|
91 |
-
}
|
92 |
-
|
93 |
-
.form-control:focus {
|
94 |
-
border-color: #04CB28;
|
95 |
-
box-shadow: none;
|
96 |
-
}
|
97 |
-
|
98 |
-
.form-control::placeholder {
|
99 |
-
font-size: 14px;
|
100 |
-
color: #C4C4C4;
|
101 |
-
}
|
102 |
-
</style>
|
103 |
-
</head>
|
104 |
-
<body>
|
105 |
-
<div class="chat-container">
|
106 |
-
<div class="chat-header">
|
107 |
-
<span>Live Chat - {{ character_name }}</span>
|
108 |
-
</div>
|
109 |
-
|
110 |
-
<div class="message-container">
|
111 |
-
<!-- Mensaje de usuario con imagen dinámica -->
|
112 |
-
<div class="message left">
|
113 |
-
<img src="/static/{{ character_image }}" alt="User Profile" id="user-img">
|
114 |
-
<div class="chat-bubble">
|
115 |
-
<span id="user-name">Usuario</span>: ¡Hola, soy {{ character_name }} Gracias por visitar mi chat.
|
116 |
-
</div>
|
117 |
-
</div>
|
118 |
-
<!-- Mensaje de bot con imagen dinámica -->
|
119 |
-
<div class="message right">
|
120 |
-
<div class="chat-bubble bg-white">
|
121 |
-
<span class="text-muted">¿Cómo estás?</span>
|
122 |
-
</div>
|
123 |
-
<img src="/static/Makoto_anime.webp" alt="Bot Profile" id="bot-img">
|
124 |
</div>
|
125 |
</div>
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
</
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
document.getElementById(
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
|
|
|
|
|
|
|
|
|
1 |
+
<div class="chat-bubbles">
|
2 |
+
{% for message in messages %}
|
3 |
+
<div class="message {{ message.type }}">
|
4 |
+
<img src="/static/{{ message.image }}" alt="Profile">
|
5 |
+
<div class="chat-bubble">
|
6 |
+
{{ message.text }}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
</div>
|
8 |
</div>
|
9 |
+
{% endfor %}
|
10 |
+
</div>
|
11 |
+
<form id="chat-form" method="POST">
|
12 |
+
<input type="text" id="user-input" name="user_input" placeholder="Type your message..." required>
|
13 |
+
<button type="submit">Send</button>
|
14 |
+
</form>
|
15 |
+
<script>
|
16 |
+
document.getElementById("chat-form").addEventListener("submit", async function(e) {
|
17 |
+
e.preventDefault();
|
18 |
+
const userInput = document.getElementById("user-input").value;
|
19 |
+
const response = await fetch(`/personajes/{{ character_name }}/chat`, {
|
20 |
+
method: "POST",
|
21 |
+
headers: { "Content-Type": "application/json" },
|
22 |
+
body: JSON.stringify({ user_input: userInput })
|
23 |
+
});
|
24 |
+
const data = await response.json();
|
25 |
+
console.log(data.response);
|
26 |
+
});
|
27 |
+
</script>
|