Update app.js
Browse files
app.js
CHANGED
|
@@ -1,57 +1,53 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 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 |
-
generate_btn.click(generate_react, inputs=prompt, outputs=output)
|
| 56 |
-
|
| 57 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 3 |
+
|
| 4 |
+
# Choose a lightweight, open model
|
| 5 |
+
model_name = "mistralai/Mistral-7B-Instruct-v0.2"
|
| 6 |
+
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
+
model_name,
|
| 10 |
+
torch_dtype="auto",
|
| 11 |
+
device_map="auto"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
pipe = pipeline(
|
| 15 |
+
"text-generation",
|
| 16 |
+
model=model,
|
| 17 |
+
tokenizer=tokenizer,
|
| 18 |
+
max_new_tokens=256,
|
| 19 |
+
do_sample=True,
|
| 20 |
+
temperature=0.7,
|
| 21 |
+
top_p=0.9
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
def chat(history, message):
|
| 25 |
+
# Build conversation text
|
| 26 |
+
prompt = ""
|
| 27 |
+
for user, bot in history:
|
| 28 |
+
prompt += f"User: {user}\nAssistant: {bot}\n"
|
| 29 |
+
prompt += f"User: {message}\nAssistant:"
|
| 30 |
+
|
| 31 |
+
output = pipe(prompt)[0]["generated_text"]
|
| 32 |
+
reply = output.split("Assistant:")[-1].strip()
|
| 33 |
+
|
| 34 |
+
history.append((message, reply))
|
| 35 |
+
return history, ""
|
| 36 |
+
|
| 37 |
+
with gr.Blocks() as demo:
|
| 38 |
+
gr.Markdown("# 🔥 My Chatbot")
|
| 39 |
+
chatbot = gr.Chatbot()
|
| 40 |
+
msg = gr.Textbox(label="Say something")
|
| 41 |
+
clear = gr.Button("Clear chat")
|
| 42 |
+
|
| 43 |
+
state = gr.State([])
|
| 44 |
+
|
| 45 |
+
def respond(message, history):
|
| 46 |
+
if history is None:
|
| 47 |
+
history = []
|
| 48 |
+
return chat(history, message)
|
| 49 |
+
|
| 50 |
+
msg.submit(respond, [msg, chatbot], [chatbot, msg])
|
| 51 |
+
clear.click(lambda: ([], ""), None, [chatbot, msg])
|
| 52 |
+
|
| 53 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|