File size: 4,276 Bytes
a20cb09
 
 
 
596ae2b
 
e4f4c98
596ae2b
 
a20cb09
 
 
6eb357f
596ae2b
b24e527
 
596ae2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6eb357f
 
e4f4c98
c7f1ed1
596ae2b
 
 
 
 
 
e7b6c01
 
 
 
 
596ae2b
 
 
 
 
 
 
6eb357f
 
596ae2b
 
a20cb09
596ae2b
c7f1ed1
6eb357f
 
596ae2b
 
a20cb09
596ae2b
adcde1c
 
596ae2b
 
 
 
 
adcde1c
6eb357f
 
596ae2b
 
a20cb09
596ae2b
 
 
a20cb09
 
596ae2b
 
 
c7f1ed1
 
 
596ae2b
 
2a0bd98
e4f4c98
 
 
2a0bd98
178ec6e
b24e527
e7b6c01
b24e527
 
 
 
 
c7f1ed1
b24e527
 
 
 
 
4ae6860
b24e527
 
 
e7b6c01
 
 
 
 
 
 
 
b24e527
 
e4f4c98
b24e527
 
e4f4c98
a20cb09
b24e527
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a20cb09
4f7e8f2
 
 
c7f1ed1
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import os

import gradio as gr
from groq import Groq
import datetime as DT
import pytz

from dotenv import load_dotenv
load_dotenv()

client = Groq(api_key=os.getenv('GROQ_API_KEY'))

SYSTEM_PROMPT = """
You complete every sentence in a very interesting way. Use emojis wherever possible.
Preserve everything in the user prompt and append to it to complete the ongoing sentence.
Don't add additional sentences.

eg:
Q. What's my
A. What's my name? 🤔

Q. I love to eat
A. I love to eat eggs for breakfast! 🧦🍳

Q. The weather is
A. The weather is perfect for surfing! 🏄‍♀️

Q. My favorite hobby is
A. My favorite hobby is collecting playing cards! 🐰💨

Q. The meaning of life is
A. The meaning of life is to find what impact you can create in the life of others! 🍕🕵️‍♂️

Q. The weather is perfect for surfing! 🏄‍♀️ My favorite hobby is
A. The weather is perfect for surfing! 🏄‍♀️ My favorite hobby is collecting playing cards! 🐰💨

Q. I love to eat eggs for breakfast! 🧦🍳 Eggs are
A. I love to eat eggs for breakfast! 🧦🍳 Eggs are healthy as well as tasty 😋

"""

ipAddress = ""
lastResponse = ""


def __nowInIST():
    return DT.datetime.now(pytz.timezone("Asia/Kolkata"))


def __attachIp(request: gr.Request):
    global ipAddress
    x_forwarded_for = request.headers.get('x-forwarded-for')
    if x_forwarded_for:
        ipAddress = x_forwarded_for


def pprint(log: str):
    now = __nowInIST()
    now = now.strftime("%Y-%m-%d %H:%M:%S")
    print(f"[{now}] [{ipAddress}] {log}")


def autocomplete(text):
    global lastResponse

    if text != "":
        if text[-1] != " ":
            yield lastResponse
            return

        pprint(f"{text=}")

        response = client.chat.completions.create(
            model='llama-3.1-8b-instant',
            messages=[
                {
                    "role": "system",
                    "content": SYSTEM_PROMPT
                },
                {
                    "role": "user",
                    "content": text
                }
            ],
            temperature=0.8,
            max_tokens=100,
            stream=True
        )

        partialMessage = ""
        for chunk in response:
            if chunk.choices[0].delta.content is not None:
                partialMessage = partialMessage + chunk.choices[0].delta.content
                yield partialMessage
        pprint(f"{partialMessage=}")
        lastResponse = partialMessage
    else:
        lastResponse = ""


css = """
# .generating {
#     display: none
# }
"""

with gr.Blocks(css=css, title="Create interesting sentences on the fly ✈") as demo:
    demo.load(__attachIp, None, None)
    gr.Markdown("# Create interesting sentences on the fly ✈")
    gr.Markdown("Powered by Groq & Llama 3.1")

    with gr.Row():
        input_box = gr.Textbox(
            lines=5,
            placeholder="Start typing ...",
            label="Input Sentence",
        )
        output_box = gr.Markdown(
            label="Output Sentence"
        )

    copy_button = gr.Button("Use Output", variant="primary")

    input_box.change(
        fn=autocomplete,
        inputs=input_box,
        outputs=output_box,
        show_progress="minimal",
        api_name=None,
        queue=True,
    )

    copy_button.click(
        fn=lambda x: x,
        inputs=output_box,
        outputs=input_box,
    )


# inputBox = gr.Textbox(
#     lines=2,
#     placeholder="Start typing ...",
#     label="Input Sentence",
# )
# outputBox = gr.Markdown(
#     label="Output Sentence"
# )

# with gr.Interface(
#     fn=autocomplete,
#     inputs=inputBox,
#     outputs=outputBox,
#     title="Create interesting sentences on the fly ✈",
#     description="Powered by Groq & Llama 3.1",
#     live=True,  # Set live to True for real-time feedback
#     allow_flagging="never",  # Disable flagging
#     css=css
# ) as demo:
#     # demo.load(__attachIp, None, None)
#     copyButton = gr.Button(
#         "Use Output",
#         elem_id="copy-button",
#         variant="primary"
#     ).click(
#         fn=lambda x: x,
#         inputs=outputBox,
#         outputs=inputBox,
#     )

# Launch the app
demo.launch(
    debug=True,
    server_name="0.0.0.0",
)