feat: init

This commit is contained in:
SouthFox 2024-08-13 00:23:42 +08:00
commit b00cdb6b10
8 changed files with 1518 additions and 0 deletions

2
.env.SAMPLE Normal file
View file

@ -0,0 +1,2 @@
GOOGLE_APPLICATION_CREDENTIALS=key.json
PROJECT_ID=xxx

12
.gitignore vendored Normal file
View file

@ -0,0 +1,12 @@
.DS_Store
.idea
*.log
tmp/
*.py[cod]
*.egg
build
htmlcov
*.json
.env

80
app.py Normal file
View file

@ -0,0 +1,80 @@
#!/usr/bin/env python3
import os
import vertexai
from fastapi import FastAPI
from fastapi.responses import StreamingResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
from vertexai.generative_models import ChatSession, GenerativeModel, Part, SafetySetting
from dotenv import load_dotenv
load_dotenv()
PROJECT_ID = os.environ.get('PROJECT_ID')
def generate():
vertexai.init(project=PROJECT_ID, location="us-central1")
model = GenerativeModel(
"gemini-1.5-flash-001",
system_instruction=[textsi_1]
)
return model.start_chat()
textsi_1 = """你要用中文回答一个德语刚入门的新手的回答,先用德语回答然后在用中文解释这个回答并给出建议"""
generation_config = {
"max_output_tokens": 1024,
"temperature": 0.2,
"top_p": 0,
}
safety_settings = [
SafetySetting(
category=SafetySetting.HarmCategory.HARM_CATEGORY_HATE_SPEECH,
threshold=SafetySetting.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE
),
SafetySetting(
category=SafetySetting.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
threshold=SafetySetting.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE
),
SafetySetting(
category=SafetySetting.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
threshold=SafetySetting.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE
),
SafetySetting(
category=SafetySetting.HarmCategory.HARM_CATEGORY_HARASSMENT,
threshold=SafetySetting.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE
),
]
model = generate()
def stream_chat(model : ChatSession, _input):
responses = model.send_message(
[_input],
generation_config=generation_config,
safety_settings=safety_settings,
stream=True,
)
for chunk in responses:
yield chunk.text
app = FastAPI(
title="AAII",
version="0.0.1"
)
class ChatRequest(BaseModel):
prompt: str
@app.post("/generate_text_stream")
async def generate_text(request : ChatRequest):
return StreamingResponse(stream_chat(model, request.prompt),media_type="text/plain")
app.mount("/", StaticFiles(directory="static", html=True), name="static")

1101
poetry.lock generated Normal file

File diff suppressed because it is too large Load diff

18
pyproject.toml Normal file
View file

@ -0,0 +1,18 @@
[tool.poetry]
name = "aaii"
version = "0.1.0"
description = ""
authors = ["SouthFox <master@southfox.me>"]
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.10"
google-cloud-aiplatform = "^1.61.0"
fastapi = "^0.112.0"
uvicorn = "^0.30.5"
python-dotenv = "^1.0.1"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

28
static/index.html Normal file
View file

@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AAII</title>
<link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet" />
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container bg-white rounded-lg shadow-md">
<h1 class="text-3xl font-bold mb-4 text-center">AAII</h1>
<div class="chat" id="chatContainer">
</div>
<div class="flex">
<input type="text" id="userInput" placeholder="Type your message here..." class="outline-none">
<button class="send-button" id="sendButton" >Send</button>
</div>
<div class="typing-indicator" id="typingIndicator">
</div>
</div>
<script type="module" src="script.js"></script>
</body>
</html>

105
static/script.js Normal file
View file

@ -0,0 +1,105 @@
import { marked } from "https://cdn.jsdelivr.net/npm/marked/lib/marked.esm.js";
const promptInput = document.getElementById("userInput");
const chatContainer = document.getElementById("chatContainer");
const typingIndicator = document.getElementById("typingIndicator");
const sendButton = document.getElementById("sendButton");
sendButton.addEventListener("click", () => {
sendMessage();
}
);
promptInput.addEventListener("keyup", () => {
handleKeyPress(event);
}
);
async function sendMessage() {
const prompt = promptInput.value.trim();
if (!prompt) {
alert("Please enter a message.");
return;
}
addMessage(prompt, 'user');
promptInput.value = "";
showTypingIndicator();
const generatedText = await generateText(prompt);
addMessage(generatedText, 'bot');
hideTypingIndicator();
}
async function generateText(prompt) {
try {
const response = await fetch("/generate_text_stream", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ prompt }),
});
if (!response.ok) {
console.error("Error:", response.statusText);
return "Error occurred while generating response.";
}
const reader = response.body.getReader();
const decoder = new TextDecoder();
let isFinished = false;
let generatedTextContent = "";
while (!isFinished) {
const { done, value } = await reader.read();
if (done) {
isFinished = true;
break;
}
generatedTextContent += decoder.decode(value, {stream: true});
}
return generatedTextContent;
} catch (error) {
console.error("Error:", error);
return "An error occurred.";
}
}
function addMessage(rawText, type) {
const messageDiv = document.createElement("div");
messageDiv.className = `message ${type}`;
var text = marked.parse(rawText)
messageDiv.innerHTML = `<div class="message-bubble fadeIn">${text}</div>`;
chatContainer.appendChild(messageDiv);
chatContainer.scrollTop = chatContainer.scrollHeight;
hideTypingIndicator();
}
let typingTimeout;
function showTypingIndicator() {
clearTimeout(typingTimeout);
typingIndicator.style.display = "inline-block";
}
function hideTypingIndicator() {
typingTimeout = setTimeout(() => {
typingIndicator.style.display = "none";
}, 1000);
}
function handleKeyPress(event) {
if (event.key === "Enter") {
sendMessage();
}
}
window.onload = () => addMessage("Hello! How can I assist you today?", 'bot');

172
static/style.css Normal file
View file

@ -0,0 +1,172 @@
h1,
h2,
h3,
ol,
ul {
margin: 0;
}
h1 {
margin-bottom: 0.5rem;
}
body {
display: flex;
justify-content: center;
align-items: center;
height: calc(100vh - 60px);
margin: 0;
padding: 0;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
color: #333;
}
.container {
margin-top: 0;
width: 90%;
max-width: 500px;
margin: 10px auto 0;
background-color: #fff;
border-radius: 12px;
box-shadow: 0 6px 18px rgba(0, 0, 0, 0.1);
padding: 20px;
transition: all 0.3s;
}
.chat {
overflow-y: auto;
height: 400px;
margin-bottom: 20px;
border-bottom: 2px solid #e2e2e2;
}
.message {
display: flex;
margin-bottom: 12px;
}
.message.user {
justify-content: flex-end;
}
.message-bubble {
padding: 12px 18px;
max-width: 70%;
border-radius: 20px;
line-height: 1.6;
font-size: 0.95rem;
}
.message.user .message-bubble {
background-color: #3182ce;
color: white;
}
.message.bot .message-bubble {
background-color: #e2e2e2;
color: #333;
}
input[type="text"] {
width: calc(100% - 110px);
padding: 12px 18px;
border: 2px solid #e2e2e2;
border-radius: 8px 0 0 8px;
font-size: 1rem;
outline: none;
}
.send-button {
width: 110px;
background-color: #3182ce;
color: white;
padding: 12px 18px;
border: none;
border-radius: 0 8px 8px 0;
cursor: pointer;
transition: background-color 0.3s;
}
.send-button:hover {
background-color: #2c5282;
}
.footer {
text-align: center;
padding: 15px 0;
font-size: 0.9rem;
color: #666;
position: static;
border-top: 1px solid #e2e2e2;
background-color: #fff;
position: fixed;
bottom: 0;
width: 100%;
}
@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
.fadeIn {
animation: fadeIn 1s;
}
@media (max-width: 600px) {
.container {
width: 95%;
margin: 10px auto 0;
}
.chat {
height: 300px;
}
input[type="text"],
.send-button {
padding: 10px 14px;
font-size: 0.9rem;
}
.footer {
font-size: 0.8rem;
margin-top: 30px;
}
}
.typing-indicator {
display: none;
align-items: center;
justify-content: flex-end;
margin-top: 8px;
width: 10px;
height: 10px;
background-color: #333;
border-radius: 50%;
margin-left: 4px;
animation: typing 1s infinite;
}
@keyframes typing {
0%,
100% {
transform: scale(1);
opacity: 1;
}
50% {
transform: scale(1.2);
opacity: 0.7;
}
}