File size: 2,386 Bytes
b594867
 
 
 
 
 
 
 
64b4fcd
b594867
 
 
64b4fcd
b594867
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36f9f0f
b594867
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f8d9e3
b5dc5de
b594867
 
 
36f9f0f
6737905
b594867
 
 
 
 
64b4fcd
 
 
 
b594867
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
"""Interface module for the AI Career Digital Twin application."""

from logging import getLogger
from os import getenv
from secrets import choice
from string import ascii_letters, digits

from dotenv import load_dotenv
from gradio import Chatbot, ChatInterface, Markdown
from gradio import __version__ as gr_version

from .assistant import Assistant
from .prompts import FOOTER_DISCLAIMER, get_welcome_message


_logger = getLogger(__name__)

# Environment initialization.
load_dotenv(override=True)


# Load or generate the secret for saved conversations.
# This is used to encrypt the saved conversations in the browser local storage,
# so it's important to keep it secret consistent across restarts if you want to
# keep the conversation history.
if MY_CHAT_SECRET := getenv("GRADIO_STATE_SECRET_CV_TWIN"):
    _logger.info("FIXED SECRET")
else:
    _logger.error("RANDOM SECRET")
    MY_CHAT_SECRET = "".join(choice(ascii_letters + digits) for _ in range(16))


def get_interface(name, profile_pdf, summary_text, repo_id):
    """Get the Gradio ChatInterface for the AI Career Digital Twin."""
    match gr_version[0]:
        case "5":
            _logger.info(f"GRADIO 5 DETECTED: {gr_version}")
            chat_ifz_conf = {"type": "messages", "show_api": "false", "api_name": False}
            chat_bot_conf = {"type": "messages",
                            "show_copy_all_button": True,
                            "show_copy_button": True}
        case "6":
            _logger.info(f"GRADIO 6 DETECTED: {gr_version}")
            chat_ifz_conf = {"api_visibility": "private"}
            chat_bot_conf = {"buttons": ["copy", "copy_all"]}

    my_chatbot = Chatbot(value=[{"role": "assistant",
                                 "content": get_welcome_message(name)}],
                            label=f"{name} Digital Twin",
                            height=None, scale=1,
                            **chat_bot_conf)

    app = ChatInterface(Assistant(
        name, profile_pdf, summary_text, repo_id).chat,
        chatbot=my_chatbot, autofocus=False, **chat_ifz_conf,
        save_history=True, title="Carlos Bazaga's virtual CV")

    # Set the secret for encrypting saved conversations.
    app.saved_conversations.secret = MY_CHAT_SECRET

    # Footer with disclaimer.
    with app:
        Markdown(FOOTER_DISCLAIMER, elem_id="footer-disclaimer")

    return app