Spaces:
Sleeping
Sleeping
File size: 3,317 Bytes
a20cb09 596ae2b a20cb09 6eb357f 596ae2b 6eb357f 596ae2b 6eb357f 596ae2b a20cb09 596ae2b 6eb357f 596ae2b a20cb09 596ae2b adcde1c 596ae2b adcde1c 6eb357f 596ae2b a20cb09 596ae2b a20cb09 596ae2b 2a0bd98 178ec6e 596ae2b a20cb09 596ae2b a20cb09 2a0bd98 596ae2b a20cb09 596ae2b |
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 |
"""
app.py
"""
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 sentence
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 😋
"""
lastResponse = None
ipAddress = None
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 = None
css = """
.generating {
display: none
}
"""
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(
"Copy",
elem_id="copy-button",
variant="primary"
).click(
fn=lambda x: x,
inputs=outputBox,
outputs=inputBox,
)
# Launch the app
demo.launch(debug=True)
|