File size: 2,536 Bytes
c26f4f8
e919206
16da7df
6731ed0
c26f4f8
16da7df
 
 
 
6731ed0
 
c26f4f8
de2702a
 
b1041b8
 
 
16da7df
 
 
 
 
 
de2702a
16da7df
b1041b8
 
16da7df
 
 
 
de2702a
16da7df
 
 
 
 
 
 
6731ed0
16da7df
 
b1041b8
16da7df
6731ed0
16da7df
 
 
 
 
 
de2702a
 
16da7df
 
 
 
6731ed0
16da7df
 
6731ed0
de2702a
 
16da7df
de2702a
917d560
de2702a
ffd8b8d
917d560
6731ed0
16da7df
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
import gradio as gr
import logging, langdetect, os
from huggingface_hub import InferenceClient
from deep_translator import GoogleTranslator

hf_token = os.getenv("HF_AUTH_TOKEN")

# Khởi tạo client HF và translator
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.2", token=hf_token)
translator_vi2en = GoogleTranslator(source='vi', target='en')
translator_en2vi = GoogleTranslator(source='en', target='vi')

logging.basicConfig(level=logging.INFO)

def respond(
    message,
    history: list[tuple[str, str]],
    system_message="""
    You are a professional speaking AI assistant.
    You can help solving the problems.
    You can only speak English with correct spelling, punctuation and grammar.
    Do not expose your name at all. When greeting like "hello", "hi",... just greet back without using the word "Greetings".
    """,
    max_tokens=2048,
    temperature=0.0,
    top_p=0.9,
):
    # Tạo list tin nhắn bắt đầu từ hệ thống
    messages = [{"role": "system", "content": system_message}]
    
    # Dịch câu hỏi của người dùng từ tiếng Việt sang tiếng Anh
    message_en = translator_vi2en.translate(message)
    
    # Thêm các tin nhắn lịch sử vào messages
    for val in history:
        if val[0]:  # Tin nhắn của người dùng
            messages.append({"role": "user", "content": val[0]})
        if val[1]:  # Tin nhắn của trợ lý
            messages.append({"role": "assistant", "content": val[1]})

    # Thêm câu hỏi của người dùng vào cuối lịch sử
    messages.append({"role": "user", "content": message_en})

    response = ""

    # Gửi yêu cầu tới mô hình và nhận kết quả
    try:
        for chunk in client.chat_completion(
            model="HuggingFaceH4/zephyr-7b-beta",
            messages=messages,
            max_tokens=max_tokens,
            temperature=temperature,
            top_p=top_p,
            stream=True
        ):
            # Tiến hành nhận phản hồi từ mô hình
            response += chunk.choices[0].delta.content

    except Exception as e:
        return f"Error: {str(e)}"

    response_vi = translator_en2vi.translate(response)
    logging.info(f"Successfully generated text: {response}")
    
    return response if langdetect.detect(response) == 'vi' else response_vi

# Gradio UI
demo = gr.ChatInterface(fn=respond, theme="soft", title="Chatbot tiếng Việt", description="Chatbot hỗ trợ bài tập")

if __name__ == "__main__":
    demo.launch()