Spaces:
Sleeping
Sleeping
OmParkashPandeY
commited on
Commit
•
83ad74b
1
Parent(s):
f319229
Upload 6 files
Browse files- .gitignore +2 -0
- app.py +83 -0
- images/helicopter.jpg +0 -0
- images/maxresdefault.jpg +0 -0
- images/police-heli.jpg +0 -0
- requirements.txt +76 -0
.gitignore
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
.venv/
|
2 |
+
.env
|
app.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
+
requests.adapters.DEFAULT_TIMEOUT = 60
|
4 |
+
|
5 |
+
from dotenv import load_dotenv, find_dotenv
|
6 |
+
_ = load_dotenv(find_dotenv()) # read local .env file
|
7 |
+
hf_api_key = os.environ['HF_API_KEY']
|
8 |
+
|
9 |
+
# Helper function
|
10 |
+
import requests, json
|
11 |
+
from text_generation import Client
|
12 |
+
|
13 |
+
|
14 |
+
#FalcomLM-instruct endpoint on the text_generation library
|
15 |
+
URL = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta"
|
16 |
+
client = Client(URL, timeout=120)
|
17 |
+
|
18 |
+
#Back to Lesson 2, time flies!
|
19 |
+
import gradio as gr
|
20 |
+
def generate(input, slider):
|
21 |
+
output = client.generate(input, max_new_tokens=slider).generated_text
|
22 |
+
return output
|
23 |
+
|
24 |
+
def format_chat_prompt(message, chat_history, instruction):
|
25 |
+
prompt = f"System:{instruction}"
|
26 |
+
for turn in chat_history:
|
27 |
+
user_message, bot_message = turn
|
28 |
+
prompt = f"{prompt}\nUser: {user_message}\nAssistant: {bot_message}"
|
29 |
+
prompt = f"{prompt}\nUser: {message}\nAssistant:"
|
30 |
+
return prompt
|
31 |
+
|
32 |
+
def respond(message, chat_history, instruction, temperature=0.7):
|
33 |
+
prompt = format_chat_prompt(message, chat_history, instruction)
|
34 |
+
chat_history = chat_history + [[message, ""]]
|
35 |
+
stream = client.generate_stream(prompt,
|
36 |
+
max_new_tokens=1024,
|
37 |
+
stop_sequences=["\nUser:", "<|endoftext|>"],
|
38 |
+
temperature=temperature)
|
39 |
+
#stop_sequences to not generate the user answer
|
40 |
+
acc_text = ""
|
41 |
+
#Streaming the tokens
|
42 |
+
for idx, response in enumerate(stream):
|
43 |
+
text_token = response.token.text
|
44 |
+
|
45 |
+
if response.details:
|
46 |
+
return
|
47 |
+
|
48 |
+
if idx == 0 and text_token.startswith(" "):
|
49 |
+
text_token = text_token[1:]
|
50 |
+
|
51 |
+
acc_text += text_token
|
52 |
+
last_turn = list(chat_history.pop(-1))
|
53 |
+
last_turn[-1] += acc_text
|
54 |
+
chat_history = chat_history + [last_turn]
|
55 |
+
yield "", chat_history
|
56 |
+
acc_text = ""
|
57 |
+
|
58 |
+
|
59 |
+
|
60 |
+
def loadGUI():
|
61 |
+
with gr.Blocks() as demo:
|
62 |
+
chatbot = gr.Chatbot(height=240) #just to fit the notebook
|
63 |
+
msg = gr.Textbox(label="Prompt")
|
64 |
+
with gr.Accordion(label="Advanced options",open=False):
|
65 |
+
system = gr.Textbox(label="System message", lines=2, value="A conversation between a user and an LLM-based AI assistant. The assistant gives helpful and honest answers.")
|
66 |
+
temperature = gr.Slider(label="temperature", minimum=0.1, maximum=1, value=0.7, step=0.1)
|
67 |
+
btn = gr.Button("Submit")
|
68 |
+
clear = gr.ClearButton(components=[msg, chatbot], value="Clear console")
|
69 |
+
|
70 |
+
btn.click(respond, inputs=[msg, chatbot, system], outputs=[msg, chatbot])
|
71 |
+
msg.submit(respond, inputs=[msg, chatbot, system], outputs=[msg, chatbot]) #Press enter to submit
|
72 |
+
|
73 |
+
gr.close_all()
|
74 |
+
demo.queue().launch(share=True)
|
75 |
+
|
76 |
+
|
77 |
+
def main():
|
78 |
+
loadGUI()
|
79 |
+
|
80 |
+
|
81 |
+
if __name__ == "__main__":
|
82 |
+
main()
|
83 |
+
|
images/helicopter.jpg
ADDED
images/maxresdefault.jpg
ADDED
images/police-heli.jpg
ADDED
requirements.txt
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
accelerate==0.25.0
|
2 |
+
aiofiles==23.2.1
|
3 |
+
altair==5.2.0
|
4 |
+
annotated-types==0.6.0
|
5 |
+
anyio==3.7.1
|
6 |
+
attrs==23.1.0
|
7 |
+
certifi==2023.11.17
|
8 |
+
charset-normalizer==3.3.2
|
9 |
+
click==8.1.7
|
10 |
+
colorama==0.4.6
|
11 |
+
contourpy==1.2.0
|
12 |
+
cycler==0.12.1
|
13 |
+
fastapi==0.104.1
|
14 |
+
ffmpy==0.3.1
|
15 |
+
filelock==3.13.1
|
16 |
+
fonttools==4.46.0
|
17 |
+
fsspec==2023.12.1
|
18 |
+
gradio==4.8.0
|
19 |
+
gradio_client==0.7.1
|
20 |
+
h11==0.14.0
|
21 |
+
httpcore==1.0.2
|
22 |
+
httpx==0.25.2
|
23 |
+
huggingface-hub==0.19.4
|
24 |
+
idna==3.6
|
25 |
+
importlib-resources==6.1.1
|
26 |
+
Jinja2==3.1.2
|
27 |
+
jsonschema==4.20.0
|
28 |
+
jsonschema-specifications==2023.11.2
|
29 |
+
kiwisolver==1.4.5
|
30 |
+
markdown-it-py==3.0.0
|
31 |
+
MarkupSafe==2.1.3
|
32 |
+
matplotlib==3.8.2
|
33 |
+
mdurl==0.1.2
|
34 |
+
mpmath==1.3.0
|
35 |
+
networkx==3.2.1
|
36 |
+
numpy==1.26.2
|
37 |
+
orjson==3.9.10
|
38 |
+
packaging==23.2
|
39 |
+
pandas==2.1.4
|
40 |
+
Pillow==10.1.0
|
41 |
+
psutil==5.9.6
|
42 |
+
pydantic==2.5.2
|
43 |
+
pydantic_core==2.14.5
|
44 |
+
pydub==0.25.1
|
45 |
+
Pygments==2.17.2
|
46 |
+
pyparsing==3.1.1
|
47 |
+
python-dateutil==2.8.2
|
48 |
+
python-dotenv==1.0.0
|
49 |
+
python-multipart==0.0.6
|
50 |
+
pytz==2023.3.post1
|
51 |
+
PyYAML==6.0.1
|
52 |
+
referencing==0.32.0
|
53 |
+
regex==2023.10.3
|
54 |
+
requests==2.31.0
|
55 |
+
rich==13.7.0
|
56 |
+
rpds-py==0.13.2
|
57 |
+
safetensors==0.4.1
|
58 |
+
semantic-version==2.10.0
|
59 |
+
shellingham==1.5.4
|
60 |
+
six==1.16.0
|
61 |
+
sniffio==1.3.0
|
62 |
+
starlette==0.27.0
|
63 |
+
sympy==1.12
|
64 |
+
tokenizers==0.15.0
|
65 |
+
tomlkit==0.12.0
|
66 |
+
toolz==0.12.0
|
67 |
+
torch==2.1.1
|
68 |
+
tqdm==4.66.1
|
69 |
+
transformers==4.35.2
|
70 |
+
typer==0.9.0
|
71 |
+
typing_extensions==4.8.0
|
72 |
+
tzdata==2023.3
|
73 |
+
urllib3==2.1.0
|
74 |
+
uvicorn==0.24.0.post1
|
75 |
+
websockets==11.0.3
|
76 |
+
text-generation==0.6.1
|