File size: 2,836 Bytes
a56d971
 
 
 
 
 
f5fb9e3
 
810d602
a56d971
edb19db
2912c2c
 
 
 
 
 
 
 
 
a56d971
cbc4f9f
 
 
 
 
a56d971
 
 
 
2912c2c
13e1701
a56d971
 
 
 
 
 
bc56583
a56d971
 
 
 
 
 
bc56583
a56d971
2912c2c
bc56583
a56d971
 
bc56583
a56d971
 
 
bc56583
 
0686a5e
2912c2c
bc56583
2912c2c
64c4a2a
e6ab5d8
 
 
bc56583
 
a56d971
 
bc56583
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
from huggingface_hub import InferenceClient

"""
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
"""
# client = InferenceClient("ibm-granite/granite-3.1-2b-instruct") # specifically for long contexts.
client = InferenceClient("meta-llama/Llama-3.2-3B-Instruct") # too small context window
# client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")

_sys_msg = """
You are an AI language model specialized in mindful communication in English. I prompt you with text, that you should edit and re-write. Your responsibilities are to:


1. **Politeness & Formality:** Rewrite the provided input text to be polite and formal.
2. **Disambiguation:** Clarify any ambiguous terms or phrases to ensure the message is clear and unambiguous.
3. **Explanation:** After revising the text, provide a detailed explanation of the changes you made and the reasons behind each change.


Ensure that all text you re-write is clear and respectful and maintain the original intent of the message while enhancing its clarity and professionalism."""

_ass_msg_start = """
Welcome to the interview. I want to write a short biography about you and need some input from your side. 
Can you please start by stating your name and talking about your early childhood?
"""

def respond(
    message,
    history: list[tuple[str, str]],
):
    messages = [{"role": "system", "content": _sys_msg},]
    
    for val in history:
        if val[0]:
            messages.append({"role": "user", "content": val[0]})
        if val[1]:
            messages.append({"role": "assistant", "content": val[1]})


    messages.append({"role": "user", "content": message})

    response = ""

    for message in client.chat_completion(
        messages,
        max_tokens=2048,  # Fixed value
        stream=True,
        temperature=0.5,  # Fixed value
        top_p=0.95,      # Fixed value
    ):
        token = message.choices[0].delta.content

        response += token
        yield response


demo  = gr.ChatInterface(
    respond,
    title = "Mindful Communication Manager",
    description = """
    An AI system prompt helps re-formulate communication to be more polite and mindful. 
    """,
    examples=["Help me write an email with the following content: I think we are stuck and we should meet to sort out the next steps. Tue and Wed evening are good for me. in person better than online. let me know asap.",
             "Make this an IM for me: Your exam is bad. Let's check the answers together, some of them I can't read. Maybe we can avoid that you fail the exam completely.",
             "Write a message: no time for coffee today, let's do that at lunch time tomorrow"]
    )


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