File size: 4,603 Bytes
5b1c7ed
 
 
 
 
 
 
 
 
 
3a0c83a
 
 
 
 
 
 
 
 
5b1c7ed
3a0c83a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b1c7ed
 
 
 
 
 
3a0c83a
 
 
 
 
 
 
5b1c7ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a0c83a
5b1c7ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a0c83a
5b1c7ed
 
 
 
 
 
3a0c83a
5b1c7ed
3a0c83a
 
 
5b1c7ed
 
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import streamlit as st
import logging
from huggingface_hub import InferenceClient
from helpers.systemPrompts import base, tutor
import os
logger = logging.getLogger(__name__)

api_key = os.environ.get('hf_api')
client = InferenceClient(api_key=api_key)

def hf_generator(model,prompt,data,system=None):
    if system:    
        messages = [
            {
                "role": "system",
                "content": [
                    {
                        "type": "text",
                        "text": system
                    }
                ]
            },
            {
                "role": "user",
                "content": [
                    {
                        "type": "text",
                        "text": prompt
                    },
                    {
                        "type": "image_url",
                        "image_url": {
                            "url": data
                        }
                    }
                ]
            }
        ]
    else:
                messages = [
            {
                "role": "user",
                "content": [
                    {
                        "type": "text",
                        "text": prompt
                    },
                    {
                        "type": "image_url",
                        "image_url": {
                            "url": data
                        }
                    }
                ]
            }
        ]

    completion = client.chat.completions.create(
        model=model, 
        messages=messages, 
        max_tokens=500
    )
    response = completion.choices[0].message.content
    logger.info({"role": "assistant", "content": response})
    st.session_state.messages.append({"role": "assistant", "content": response})
    return completion.choices[0].message.content
    

    

def basicChat():
# Accept user input and then writes the response
    if prompt := st.chat_input("How may I help you learn math today?"):
        # Add user message to chat history
        st.session_state.messages.append({"role": "user", "content": prompt})
        logger.info(st.session_state.messages[-1])
        # Display user message in chat message container
        with st.chat_message("user"):
            st.markdown(prompt)

        with st.chat_message(st.session_state.model):
            logger.info(f"""Message to {st.session_state.model}: {[
                    {"role": m["role"], "content": m["content"]}
                    for m in st.session_state.messages
                ]}""")
            response = st.write_stream(hf_generator(
                st.session_state.model,
                [
                    {"role": m["role"], "content": m["content"]}
                    for m in st.session_state.messages
                ]
            ))
            st.session_state.messages.append({"role": "assistant", "content": response})
            logger.info(st.session_state.messages[-1])


def mmChat(data):
    if prompt := st.chat_input("How may I help you learn math today?"):
         # Add user message to chat history
        st.session_state.messages.append({"role": "user", "content": prompt,"images":[data]})
        logger.info(st.session_state.messages[-1])
        # Display user message in chat message container
        with st.chat_message("user"):
            st.markdown(prompt)

        with st.chat_message(st.session_state.model):
            logger.info(f"Message to {st.session_state.model}: {st.session_state.messages[-1]}")
            response = st.write(hf_generator(
                st.session_state.model,
                prompt,
                data))
            st.session_state.messages.append({"role": "assistant", "content": response})
            logger.info(st.session_state.messages[-1])


def guidedMM(sysChoice:str, data):
    if sysChoice == "Tutor":
        system = tutor
    else:
        system = base
    
    if prompt := st.chat_input("How may I help you learn math today?"):
         # Add user message to chat history
        st.session_state.messages.append({"role": "user", "content": prompt,"images":[data]})
        # Display user message in chat message container
        with st.chat_message("user"):
            st.markdown(prompt)

        with st.chat_message(st.session_state.model):
            logger.info(f"Message to {st.session_state.model}: {st.session_state.messages[-1]}")
            response = st.write(hf_generator(
                st.session_state.model,
                prompt,
                data,
                system
            ))