Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,65 +1,47 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
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 |
-
messages.append({"role": "user", "content": message})
|
28 |
-
|
29 |
-
response = ""
|
30 |
-
|
31 |
-
for message in client.chat_completion(
|
32 |
-
messages,
|
33 |
-
max_tokens=max_tokens,
|
34 |
-
stream=True,
|
35 |
temperature=temperature,
|
36 |
top_p=top_p,
|
37 |
-
|
38 |
-
|
|
|
39 |
|
40 |
-
|
41 |
-
yield response
|
42 |
|
43 |
-
|
44 |
-
"""
|
45 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
46 |
-
"""
|
47 |
demo = gr.ChatInterface(
|
48 |
respond,
|
49 |
additional_inputs=[
|
50 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
51 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
52 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
53 |
-
gr.Slider(
|
54 |
-
minimum=0.1,
|
55 |
-
maximum=1.0,
|
56 |
-
value=0.95,
|
57 |
-
step=0.05,
|
58 |
-
label="Top-p (nucleus sampling)",
|
59 |
-
),
|
60 |
],
|
61 |
)
|
62 |
|
63 |
-
|
64 |
if __name__ == "__main__":
|
65 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
3 |
+
from peft import PeftModel
|
4 |
+
|
5 |
+
# Load base + LoRA model
|
6 |
+
base_model = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
7 |
+
lora_model = "Futuresony/future_12_10_2024"
|
8 |
+
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model)
|
10 |
+
base = AutoModelForCausalLM.from_pretrained(base_model)
|
11 |
+
model = PeftModel.from_pretrained(base, lora_model)
|
12 |
+
model.eval()
|
13 |
+
|
14 |
+
# Create generation pipeline
|
15 |
+
generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
16 |
+
|
17 |
+
# Define the chat function
|
18 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
19 |
+
prompt = system_message + "\n"
|
20 |
+
for user, bot in history:
|
21 |
+
prompt += f"User: {user}\nAssistant: {bot}\n"
|
22 |
+
prompt += f"User: {message}\nAssistant:"
|
23 |
+
|
24 |
+
response = generator(
|
25 |
+
prompt,
|
26 |
+
max_new_tokens=max_tokens,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
temperature=temperature,
|
28 |
top_p=top_p,
|
29 |
+
do_sample=True,
|
30 |
+
return_full_text=False,
|
31 |
+
)[0]["generated_text"]
|
32 |
|
33 |
+
yield response.strip()
|
|
|
34 |
|
35 |
+
# Set up Gradio UI
|
|
|
|
|
|
|
36 |
demo = gr.ChatInterface(
|
37 |
respond,
|
38 |
additional_inputs=[
|
39 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
40 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
41 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
42 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
],
|
44 |
)
|
45 |
|
|
|
46 |
if __name__ == "__main__":
|
47 |
demo.launch()
|