Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,28 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
def format_chat_prompt(message, chat_history, instruction):
|
| 8 |
prompt = f"System:{instruction}"
|
|
@@ -15,25 +35,14 @@ def format_chat_prompt(message, chat_history, instruction):
|
|
| 15 |
def respond(message, chat_history, instruction, temperature=0.7):
|
| 16 |
prompt = format_chat_prompt(message, chat_history, instruction)
|
| 17 |
chat_history = chat_history + [[message, ""]]
|
| 18 |
-
stream = client.generate_stream(prompt,
|
| 19 |
-
max_new_tokens=1024,
|
| 20 |
-
stop_sequences=["\nUser:", "<|endoftext|>"],
|
| 21 |
-
temperature=temperature)
|
| 22 |
-
#stop_sequences to not generate the user answer
|
| 23 |
acc_text = ""
|
| 24 |
-
#Streaming the tokens
|
| 25 |
for idx, response in enumerate(stream):
|
| 26 |
text_token = response.token.text
|
| 27 |
-
|
| 28 |
if response.details:
|
| 29 |
return
|
| 30 |
-
|
| 31 |
if idx == 0 and text_token.startswith(" "):
|
| 32 |
text_token = text_token[1:]
|
| 33 |
-
|
| 34 |
-
if any(word in text_token for word in SAFETY_GUIDELINES):
|
| 35 |
-
continue
|
| 36 |
-
|
| 37 |
acc_text += text_token
|
| 38 |
last_turn = list(chat_history.pop(-1))
|
| 39 |
last_turn[-1] += acc_text
|
|
@@ -41,16 +50,7 @@ def respond(message, chat_history, instruction, temperature=0.7):
|
|
| 41 |
yield "", chat_history
|
| 42 |
acc_text = ""
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
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.")
|
| 49 |
-
temperature = gr.Slider(label="temperature", minimum=0.1, maximum=1, value=0.7, step=0.1)
|
| 50 |
-
btn = gr.Button("Submit")
|
| 51 |
-
clear = gr.ClearButton(components=[msg, chatbot], value="Clear console")
|
| 52 |
-
|
| 53 |
-
btn.click(respond, inputs=[msg, chatbot, system], outputs=[msg, chatbot])
|
| 54 |
-
msg.submit(respond, inputs=[msg, chatbot, system], outputs=[msg, chatbot]) #Press enter to submit
|
| 55 |
-
gr.close_all()
|
| 56 |
-
demo.queue().launch(share=True, server_port=int(os.environ['PORT4']))
|
|
|
|
| 1 |
import os
|
| 2 |
+
import requests
|
| 3 |
import gradio as gr
|
| 4 |
+
from dotenv import load_dotenv, find_dotenv
|
| 5 |
from transformers import pipeline
|
| 6 |
|
| 7 |
+
# Load environment variables
|
| 8 |
+
_ = load_dotenv(find_dotenv())
|
| 9 |
+
hf_api_key = os.environ['HF_API_KEY']
|
| 10 |
+
|
| 11 |
+
class Client:
|
| 12 |
+
def __init__(self, base_url, headers, timeout):
|
| 13 |
+
self.base_url = base_url
|
| 14 |
+
self.headers = headers
|
| 15 |
+
self.timeout = timeout
|
| 16 |
+
|
| 17 |
+
def generate(self, prompt, max_new_tokens):
|
| 18 |
+
# Your text generation code here.
|
| 19 |
+
return {'generated_text': 'Placeholder text'}
|
| 20 |
+
|
| 21 |
+
def generate_stream(self, prompt, max_new_tokens, stop_sequences, temperature):
|
| 22 |
+
# Your streaming text generation code here.
|
| 23 |
+
pass
|
| 24 |
+
|
| 25 |
+
client = Client(os.environ['HF_API_FALCOM_BASE'], headers={"Authorization": f"Basic {hf_api_key}"}, timeout=120)
|
| 26 |
|
| 27 |
def format_chat_prompt(message, chat_history, instruction):
|
| 28 |
prompt = f"System:{instruction}"
|
|
|
|
| 35 |
def respond(message, chat_history, instruction, temperature=0.7):
|
| 36 |
prompt = format_chat_prompt(message, chat_history, instruction)
|
| 37 |
chat_history = chat_history + [[message, ""]]
|
| 38 |
+
stream = client.generate_stream(prompt, max_new_tokens=1024, stop_sequences=["\nUser:", ""], temperature=temperature)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
acc_text = ""
|
|
|
|
| 40 |
for idx, response in enumerate(stream):
|
| 41 |
text_token = response.token.text
|
|
|
|
| 42 |
if response.details:
|
| 43 |
return
|
|
|
|
| 44 |
if idx == 0 and text_token.startswith(" "):
|
| 45 |
text_token = text_token[1:]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
acc_text += text_token
|
| 47 |
last_turn = list(chat_history.pop(-1))
|
| 48 |
last_turn[-1] += acc_text
|
|
|
|
| 50 |
yield "", chat_history
|
| 51 |
acc_text = ""
|
| 52 |
|
| 53 |
+
iface = gr.Interface(fn=respond, inputs=[gr.Textbox(label="Prompt"), gr.Chatbox(label="Chat History", height=240), 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."), gr.Slider(label="temperature", minimum=0.1, maximum=1, value=0.7, step=0.1)], outputs=[gr.Textbox(label="Prompt"), gr.Chatbox(label="Chat History", height=240)])
|
| 54 |
+
|
| 55 |
+
if __name__ == "__main__":
|
| 56 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|