Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
+
|
| 4 |
+
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
| 5 |
+
|
| 6 |
+
def format_prompt(message, history):
|
| 7 |
+
system_prompt = ("I’m an AI chatbot named ChatSherman designed by a super-intelligent student named ShermanAI at the Department of Electronic and Information Engineering at The Hong Kong Polytechnic University to help you with your engineering questions. Also, I can assist with a wide range of topics and questions.")
|
| 8 |
+
prompt = f"<s>{system_prompt}"
|
| 9 |
+
for user_prompt, bot_response in history:
|
| 10 |
+
prompt += f"[USER] {user_prompt} [/USER]"
|
| 11 |
+
prompt += f"{bot_response}</s> "
|
| 12 |
+
prompt += f"[USER] {message} [/USER]</s>"
|
| 13 |
+
return prompt
|
| 14 |
+
|
| 15 |
+
def generate(prompt, history, temperature=0.6, max_new_tokens=256, top_p=0.95, repetition_penalty=1.2):
|
| 16 |
+
temperature = float(temperature)
|
| 17 |
+
top_p = float(top_p)
|
| 18 |
+
|
| 19 |
+
generate_kwargs = dict(
|
| 20 |
+
temperature=temperature,
|
| 21 |
+
max_new_tokens=max_new_tokens,
|
| 22 |
+
top_p=top_p,
|
| 23 |
+
repetition_penalty=repetition_penalty,
|
| 24 |
+
do_sample=True,
|
| 25 |
+
seed=42,
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
formatted_prompt = format_prompt(prompt, history)
|
| 29 |
+
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
| 30 |
+
output = ""
|
| 31 |
+
for response in stream:
|
| 32 |
+
output += response.token.text
|
| 33 |
+
yield output
|
| 34 |
+
return output
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
examples = [
|
| 38 |
+
["What is ChatSherman, and how does it work?", []],
|
| 39 |
+
["Is my personal information and data safe when I use the ChatSherman chatbot?", []],
|
| 40 |
+
["What are some common applications of deep learning in engineering?", []]
|
| 41 |
+
]
|
| 42 |
+
|
| 43 |
+
gr.ChatInterface(
|
| 44 |
+
fn=generate,
|
| 45 |
+
chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
|
| 46 |
+
title="ChatSherman",
|
| 47 |
+
description = "This is an AI chatbot powered by ShermanAI. Enter your question below to get started. ",
|
| 48 |
+
examples=examples,
|
| 49 |
+
concurrency_limit=20,
|
| 50 |
+
).launch(show_api=False)
|