Spaces:
Sleeping
Sleeping
Ashhar
commited on
Commit
·
596ae2b
1
Parent(s):
6eb357f
my changes
Browse files- .gitignore +6 -0
- README.md +3 -3
- app.py +100 -27
- requirements.txt +2 -1
.gitignore
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.env
|
| 2 |
+
.venv
|
| 3 |
+
__pycache__/
|
| 4 |
+
.gitattributes
|
| 5 |
+
gradio_cached_examples/
|
| 6 |
+
app_*.py
|
README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: gray
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 4.
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Interesting Sentences Generator
|
| 3 |
+
emoji: ✈
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: gray
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 4.36.1
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
app.py
CHANGED
|
@@ -5,62 +5,135 @@ import os
|
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
from groq import Groq
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
client = Groq(api_key=os.getenv('GROQ_API_KEY'))
|
| 10 |
|
| 11 |
SYSTEM_PROMPT = """
|
| 12 |
-
You complete
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
"""
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
def autocomplete(text):
|
| 18 |
-
global
|
| 19 |
-
|
| 20 |
if text != "":
|
| 21 |
-
if text[-1]
|
| 22 |
-
|
| 23 |
-
else:
|
| 24 |
return
|
| 25 |
|
|
|
|
|
|
|
| 26 |
response = client.chat.completions.create(
|
| 27 |
-
model='
|
| 28 |
messages=[
|
| 29 |
{
|
| 30 |
-
"role": "
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
"content": text
|
| 32 |
}
|
| 33 |
],
|
|
|
|
|
|
|
| 34 |
stream=True
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
for chunk in response:
|
| 39 |
if chunk.choices[0].delta.content is not None:
|
| 40 |
-
|
| 41 |
-
yield
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
css = """
|
| 44 |
.generating {
|
| 45 |
display: none
|
| 46 |
}
|
| 47 |
"""
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
fn=autocomplete,
|
| 52 |
-
inputs=
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
title="Catch Me If You Can 🐰",
|
| 57 |
-
description="Powered by Groq & Gemma",
|
| 58 |
live=True, # Set live to True for real-time feedback
|
| 59 |
allow_flagging="never", # Disable flagging
|
| 60 |
css=css
|
| 61 |
-
)
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
# Launch the app
|
| 66 |
-
|
|
|
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
from groq import Groq
|
| 8 |
+
import datetime as DT
|
| 9 |
+
import pytz
|
| 10 |
+
from dotenv import load_dotenv
|
| 11 |
+
load_dotenv()
|
| 12 |
|
| 13 |
client = Groq(api_key=os.getenv('GROQ_API_KEY'))
|
| 14 |
|
| 15 |
SYSTEM_PROMPT = """
|
| 16 |
+
You complete every sentence in a very interesting way. Use emojis wherever possible.
|
| 17 |
+
Preserve everything in the user prompt and append to it to complete the sentence
|
| 18 |
+
|
| 19 |
+
eg:
|
| 20 |
+
Q. What's my
|
| 21 |
+
A. What's my name? 🤔
|
| 22 |
+
|
| 23 |
+
Q. I love to eat
|
| 24 |
+
A. I love to eat eggs for breakfast! 🧦🍳
|
| 25 |
+
|
| 26 |
+
Q. The weather is
|
| 27 |
+
A. The weather is perfect for surfing! 🏄♀️
|
| 28 |
+
|
| 29 |
+
Q. My favorite hobby is
|
| 30 |
+
A. My favorite hobby is collecting playing cards! 🐰💨
|
| 31 |
+
|
| 32 |
+
Q. The meaning of life is
|
| 33 |
+
A. The meaning of life is to find what impact you can create in the life of others! 🍕🕵️♂️
|
| 34 |
+
|
| 35 |
+
Q. The weather is perfect for surfing! 🏄♀️ My favorite hobby is
|
| 36 |
+
A. The weather is perfect for surfing! 🏄♀️ My favorite hobby is collecting playing cards! 🐰💨
|
| 37 |
+
|
| 38 |
+
Q. I love to eat eggs for breakfast! 🧦🍳 Eggs are
|
| 39 |
+
A. I love to eat eggs for breakfast! 🧦🍳 Eggs are healthy as well as tasty 😋
|
| 40 |
+
|
| 41 |
"""
|
| 42 |
|
| 43 |
+
lastResponse = None
|
| 44 |
+
ipAddress = None
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def __nowInIST():
|
| 48 |
+
return DT.datetime.now(pytz.timezone("Asia/Kolkata"))
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def __attachIp(request: gr.Request):
|
| 52 |
+
global ipAddress
|
| 53 |
+
x_forwarded_for = request.headers.get('x-forwarded-for')
|
| 54 |
+
if x_forwarded_for:
|
| 55 |
+
ipAddress = x_forwarded_for
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def pprint(log: str):
|
| 59 |
+
now = __nowInIST()
|
| 60 |
+
now = now.strftime("%Y-%m-%d %H:%M:%S")
|
| 61 |
+
print(f"[{now}] [{ipAddress}] {log}")
|
| 62 |
+
|
| 63 |
|
| 64 |
def autocomplete(text):
|
| 65 |
+
global lastResponse
|
| 66 |
+
|
| 67 |
if text != "":
|
| 68 |
+
if text[-1] != " ":
|
| 69 |
+
yield lastResponse
|
|
|
|
| 70 |
return
|
| 71 |
|
| 72 |
+
pprint(f"{text=}")
|
| 73 |
+
|
| 74 |
response = client.chat.completions.create(
|
| 75 |
+
model='llama-3.1-8b-instant',
|
| 76 |
messages=[
|
| 77 |
{
|
| 78 |
+
"role": "system",
|
| 79 |
+
"content": SYSTEM_PROMPT
|
| 80 |
+
},
|
| 81 |
+
{
|
| 82 |
+
"role": "user",
|
| 83 |
"content": text
|
| 84 |
}
|
| 85 |
],
|
| 86 |
+
temperature=0.8,
|
| 87 |
+
max_tokens=100,
|
| 88 |
stream=True
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
partialMessage = ""
|
| 92 |
for chunk in response:
|
| 93 |
if chunk.choices[0].delta.content is not None:
|
| 94 |
+
partialMessage = partialMessage + chunk.choices[0].delta.content
|
| 95 |
+
yield partialMessage
|
| 96 |
+
pprint(f"{partialMessage=}")
|
| 97 |
+
lastResponse = partialMessage
|
| 98 |
+
else:
|
| 99 |
+
lastResponse = None
|
| 100 |
+
|
| 101 |
+
|
| 102 |
css = """
|
| 103 |
.generating {
|
| 104 |
display: none
|
| 105 |
}
|
| 106 |
"""
|
| 107 |
|
| 108 |
+
inputBox = gr.Textbox(
|
| 109 |
+
lines=2,
|
| 110 |
+
placeholder="Start typing ...",
|
| 111 |
+
label="Input Sentence",
|
| 112 |
+
)
|
| 113 |
+
outputBox = gr.Markdown(
|
| 114 |
+
label="Output Sentence"
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
with gr.Interface(
|
| 118 |
fn=autocomplete,
|
| 119 |
+
inputs=inputBox,
|
| 120 |
+
outputs=outputBox,
|
| 121 |
+
title="Create interesting sentences on the fly ✈",
|
| 122 |
+
description="Powered by Groq & Llama 3.1",
|
|
|
|
|
|
|
| 123 |
live=True, # Set live to True for real-time feedback
|
| 124 |
allow_flagging="never", # Disable flagging
|
| 125 |
css=css
|
| 126 |
+
) as demo:
|
| 127 |
+
demo.load(__attachIp, None, None)
|
| 128 |
+
copyButton = gr.Button(
|
| 129 |
+
"Copy",
|
| 130 |
+
elem_id="copy-button",
|
| 131 |
+
variant="primary"
|
| 132 |
+
).click(
|
| 133 |
+
fn=lambda x: x,
|
| 134 |
+
inputs=outputBox,
|
| 135 |
+
outputs=inputBox,
|
| 136 |
+
)
|
| 137 |
|
| 138 |
# Launch the app
|
| 139 |
+
demo.launch(debug=True)
|
requirements.txt
CHANGED
|
@@ -1 +1,2 @@
|
|
| 1 |
-
groq
|
|
|
|
|
|
| 1 |
+
groq
|
| 2 |
+
python-dotenv
|