Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,29 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
|
|
|
| 3 |
|
| 4 |
"""
|
| 5 |
-
|
| 6 |
"""
|
| 7 |
-
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def respond(
|
| 11 |
message,
|
|
@@ -26,19 +44,29 @@ def respond(
|
|
| 26 |
|
| 27 |
prompt += f"[INST] {message} [/INST]\n"
|
| 28 |
|
| 29 |
-
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
max_new_tokens=max_tokens,
|
| 34 |
-
|
| 35 |
temperature=temperature,
|
| 36 |
top_p=top_p,
|
| 37 |
-
|
| 38 |
-
)
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
| 42 |
|
| 43 |
"""
|
| 44 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
|
@@ -46,19 +74,20 @@ For information on how to customize the ChatInterface, peruse the gradio docs: h
|
|
| 46 |
demo = gr.ChatInterface(
|
| 47 |
respond,
|
| 48 |
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a Microsoft 365 data management assistant.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=
|
| 52 |
gr.Slider(
|
| 53 |
minimum=0.1,
|
| 54 |
maximum=1.0,
|
| 55 |
-
value=0.
|
| 56 |
step=0.05,
|
| 57 |
label="Top-p (nucleus sampling)",
|
| 58 |
),
|
| 59 |
],
|
|
|
|
|
|
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
if __name__ == "__main__":
|
| 64 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
| 5 |
|
| 6 |
"""
|
| 7 |
+
Load model and tokenizer directly using transformers
|
| 8 |
"""
|
| 9 |
+
model_name = "PlantWisdom/Data_Management_Mistral"
|
| 10 |
|
| 11 |
+
# Configure quantization for lower memory usage
|
| 12 |
+
quantization_config = BitsAndBytesConfig(
|
| 13 |
+
load_in_4bit=True,
|
| 14 |
+
bnb_4bit_compute_dtype=torch.float16
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
# Load tokenizer and model
|
| 18 |
+
print("Loading tokenizer...")
|
| 19 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 20 |
+
|
| 21 |
+
print("Loading model...")
|
| 22 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 23 |
+
model_name,
|
| 24 |
+
quantization_config=quantization_config,
|
| 25 |
+
device_map="auto",
|
| 26 |
+
)
|
| 27 |
|
| 28 |
def respond(
|
| 29 |
message,
|
|
|
|
| 44 |
|
| 45 |
prompt += f"[INST] {message} [/INST]\n"
|
| 46 |
|
| 47 |
+
# Print the prompt for debugging
|
| 48 |
+
print(f"Prompt: {prompt}")
|
| 49 |
|
| 50 |
+
# Encode the prompt
|
| 51 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 52 |
+
|
| 53 |
+
# Generate tokens
|
| 54 |
+
print("Generating response...")
|
| 55 |
+
|
| 56 |
+
# Generate without streaming for simplicity
|
| 57 |
+
generated_ids = model.generate(
|
| 58 |
+
inputs.input_ids,
|
| 59 |
max_new_tokens=max_tokens,
|
| 60 |
+
do_sample=True,
|
| 61 |
temperature=temperature,
|
| 62 |
top_p=top_p,
|
| 63 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
# Decode the response
|
| 67 |
+
full_response = tokenizer.decode(generated_ids[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
|
| 68 |
+
print(f"Response generated: {full_response[:50]}...")
|
| 69 |
+
return full_response
|
| 70 |
|
| 71 |
"""
|
| 72 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
|
|
|
| 74 |
demo = gr.ChatInterface(
|
| 75 |
respond,
|
| 76 |
additional_inputs=[
|
| 77 |
+
gr.Textbox(value="You are a Microsoft 365 data management assistant specialized in SharePoint and OneDrive. Answer questions concisely and accurately.", label="System message"),
|
| 78 |
+
gr.Slider(minimum=1, maximum=1024, value=256, step=1, label="Max new tokens"),
|
| 79 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Temperature"),
|
| 80 |
gr.Slider(
|
| 81 |
minimum=0.1,
|
| 82 |
maximum=1.0,
|
| 83 |
+
value=0.9,
|
| 84 |
step=0.05,
|
| 85 |
label="Top-p (nucleus sampling)",
|
| 86 |
),
|
| 87 |
],
|
| 88 |
+
title="Microsoft 365 Data Management Assistant",
|
| 89 |
+
description="Ask questions about SharePoint, OneDrive, and other Microsoft 365 data management topics."
|
| 90 |
)
|
| 91 |
|
|
|
|
| 92 |
if __name__ == "__main__":
|
| 93 |
demo.launch()
|