Update app.py
Browse files
app.py
CHANGED
|
@@ -1,105 +1,68 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
import dashscope
|
| 6 |
-
from dashscope import Generation
|
| 7 |
-
from dashscope.api_entities.dashscope_response import Role
|
| 8 |
-
from typing import List, Optional, Tuple, Dict
|
| 9 |
-
from urllib.error import HTTPError
|
| 10 |
-
default_system = 'You are a helpful assistant.'
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
def
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
def
|
| 22 |
-
if
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
return system, history
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
def model_chat(query: Optional[str], history: Optional[History], system: str
|
| 44 |
-
) -> Tuple[str, str, History]:
|
| 45 |
-
if query is None:
|
| 46 |
-
query = ''
|
| 47 |
-
if history is None:
|
| 48 |
-
history = []
|
| 49 |
-
messages = history_to_messages(history, system)
|
| 50 |
-
messages.append({'role': Role.USER, 'content': query})
|
| 51 |
-
gen = Generation.call(
|
| 52 |
-
model='qwen2-72b-instruct',
|
| 53 |
-
messages=messages,
|
| 54 |
-
result_format='message',
|
| 55 |
-
stream=True
|
| 56 |
-
)
|
| 57 |
-
for response in gen:
|
| 58 |
-
if response.status_code == HTTPStatus.OK:
|
| 59 |
-
role = response.output.choices[0].message.role
|
| 60 |
-
response = response.output.choices[0].message.content
|
| 61 |
-
system, history = messages_to_history(messages + [{'role': role, 'content': response}])
|
| 62 |
-
yield '', history, system
|
| 63 |
-
else:
|
| 64 |
-
raise ValueError('Request id: %s, Status code: %s, error code: %s, error message: %s' % (
|
| 65 |
-
response.request_id, response.status_code,
|
| 66 |
-
response.code, response.message
|
| 67 |
-
))
|
| 68 |
|
|
|
|
| 69 |
|
| 70 |
-
|
| 71 |
-
gr.Markdown("""<center><font size=8>Qwen2-72B-instruct Chat👾</center>""")
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
system_input = gr.Textbox(value=default_system, lines=1, label='System')
|
| 76 |
-
with gr.Column(scale=1):
|
| 77 |
-
modify_system = gr.Button("🛠️ Set system prompt and clear history", scale=2)
|
| 78 |
-
system_state = gr.Textbox(value=default_system, visible=False)
|
| 79 |
-
chatbot = gr.Chatbot(label='qwen2-72B-instruct')
|
| 80 |
-
textbox = gr.Textbox(lines=1, label='Input')
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
inputs=[],
|
| 97 |
-
outputs=[textbox, chatbot],
|
| 98 |
-
concurrency_limit = 40)
|
| 99 |
-
modify_system.click(fn=modify_system_session,
|
| 100 |
-
inputs=[system_input],
|
| 101 |
-
outputs=[system_state, system_input, chatbot],
|
| 102 |
-
concurrency_limit = 40)
|
| 103 |
-
|
| 104 |
-
demo.queue(api_open=False)
|
| 105 |
-
demo.launch(max_threads=40)
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
import torch
|
| 4 |
+
import random
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
@st.cache_resource
|
| 7 |
+
def load_model():
|
| 8 |
+
model_name = "sberbank-ai/rugpt3small_based_on_gpt2"
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 11 |
+
return tokenizer, model
|
| 12 |
|
| 13 |
+
def generate_response(prompt, tokenizer, model):
|
| 14 |
+
inputs = tokenizer.encode(prompt, return_tensors='pt')
|
| 15 |
+
with torch.no_grad():
|
| 16 |
+
outputs = model.generate(inputs, max_length=100, num_return_sequences=1,
|
| 17 |
+
temperature=0.9, top_k=50, top_p=0.95)
|
| 18 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 19 |
+
return add_mistakes(response)
|
| 20 |
|
| 21 |
+
def add_mistakes(text):
|
| 22 |
+
words = text.split()
|
| 23 |
+
for i in range(len(words)):
|
| 24 |
+
if random.random() < 0.2: # 20% шанс ошибки в слове
|
| 25 |
+
words[i] = misspell_word(words[i])
|
| 26 |
+
return ' '.join(words)
|
| 27 |
|
| 28 |
+
def misspell_word(word):
|
| 29 |
+
if len(word) < 3:
|
| 30 |
+
return word
|
| 31 |
+
vowels = 'аеёиоуыэюя'
|
| 32 |
+
consonants = 'бвгджзйклмнпрстфхцчшщ'
|
| 33 |
+
|
| 34 |
+
if random.random() < 0.5:
|
| 35 |
+
# Заменяем случайную гласную
|
| 36 |
+
for i, char in enumerate(word):
|
| 37 |
+
if char.lower() in vowels:
|
| 38 |
+
replacement = random.choice(vowels)
|
| 39 |
+
return word[:i] + replacement + word[i+1:]
|
| 40 |
+
else:
|
| 41 |
+
# Заменяем случайную согласную
|
| 42 |
+
for i, char in enumerate(word):
|
| 43 |
+
if char.lower() in consonants:
|
| 44 |
+
replacement = random.choice(consonants)
|
| 45 |
+
return word[:i] + replacement + word[i+1:]
|
| 46 |
+
return word
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
st.title("AI Чат с простой русской моделью")
|
| 49 |
|
| 50 |
+
tokenizer, model = load_model()
|
|
|
|
| 51 |
|
| 52 |
+
if "messages" not in st.session_state:
|
| 53 |
+
st.session_state.messages = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
for message in st.session_state.messages:
|
| 56 |
+
with st.chat_message(message["role"]):
|
| 57 |
+
st.markdown(message["content"])
|
| 58 |
|
| 59 |
+
if prompt := st.chat_input("Введите ваше сообщение"):
|
| 60 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 61 |
+
with st.chat_message("user"):
|
| 62 |
+
st.markdown(prompt)
|
| 63 |
|
| 64 |
+
with st.chat_message("assistant"):
|
| 65 |
+
response = generate_response(prompt, tokenizer, model)
|
| 66 |
+
st.markdown(response)
|
| 67 |
+
|
| 68 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|