Spaces:
Runtime error
Runtime error
jordigonzm
commited on
Commit
•
0067ed6
1
Parent(s):
976d049
Update app.py
Browse files
app.py
CHANGED
@@ -1,86 +1,59 @@
|
|
1 |
-
import os
|
2 |
import gradio as gr
|
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 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
with gr.Row():
|
62 |
-
with gr.Column(scale=3):
|
63 |
-
system_input = gr.Textbox(value=default_system, lines=1, label='System')
|
64 |
-
with gr.Column(scale=1):
|
65 |
-
modify_system = gr.Button("🛠️ Set system prompt and clear history", scale=2)
|
66 |
-
system_state = gr.Textbox(value=default_system, visible=False)
|
67 |
-
chatbot = gr.Chatbot(label='Chat with AI')
|
68 |
-
textbox = gr.Textbox(lines=2, label='Input')
|
69 |
-
|
70 |
-
with gr.Row():
|
71 |
-
clear_history = gr.Button("🧹 Clear history")
|
72 |
-
submit = gr.Button("🚀 Send")
|
73 |
-
|
74 |
-
submit.click(model_chat,
|
75 |
-
inputs=[textbox, chatbot, system_state],
|
76 |
-
outputs=[textbox, chatbot, system_input],
|
77 |
-
concurrency_limit=100)
|
78 |
-
clear_history.click(fn=clear_session,
|
79 |
-
inputs=[],
|
80 |
-
outputs=[textbox, chatbot])
|
81 |
-
modify_system.click(fn=modify_system_session,
|
82 |
-
inputs=[system_input],
|
83 |
-
outputs=[system_state, system_input, chatbot])
|
84 |
-
|
85 |
-
demo.queue(api_open=False)
|
86 |
-
demo.launch(max_threads=30)
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import spaces
|
3 |
+
import torch
|
4 |
+
|
5 |
+
import transformers
|
6 |
+
import torch
|
7 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
8 |
+
|
9 |
+
model_name = "microsoft/Phi-3-mini-128k-instruct"
|
10 |
+
|
11 |
+
pipeline = transformers.pipeline(
|
12 |
+
"text-generation",
|
13 |
+
model=model_name,
|
14 |
+
model_kwargs={"torch_dtype": torch.bfloat16},
|
15 |
+
device="cuda",
|
16 |
+
)
|
17 |
+
|
18 |
+
@spaces.GPU
|
19 |
+
def chat_function(message, history, system_prompt,max_new_tokens,temperature):
|
20 |
+
messages = [
|
21 |
+
{"role": "system", "content": system_prompt},
|
22 |
+
{"role": "user", "content": message},
|
23 |
+
]
|
24 |
+
prompt = pipeline.tokenizer.apply_chat_template(
|
25 |
+
messages,
|
26 |
+
tokenize=False,
|
27 |
+
add_generation_prompt=True
|
28 |
+
)
|
29 |
+
terminators = [
|
30 |
+
pipeline.tokenizer.eos_token_id,
|
31 |
+
pipeline.tokenizer.convert_tokens_to_ids("<|eot_id|>")
|
32 |
+
]
|
33 |
+
temp = temperature + 0.1
|
34 |
+
outputs = pipeline(
|
35 |
+
prompt,
|
36 |
+
max_new_tokens=max_new_tokens,
|
37 |
+
eos_token_id=terminators,
|
38 |
+
do_sample=True,
|
39 |
+
temperature=temp,
|
40 |
+
top_p=0.9,
|
41 |
+
)
|
42 |
+
return outputs[0]["generated_text"][len(prompt):]
|
43 |
+
|
44 |
+
gr.ChatInterface(
|
45 |
+
chat_function,
|
46 |
+
chatbot=gr.Chatbot(height=400),
|
47 |
+
textbox=gr.Textbox(placeholder="Enter message here", container=False, scale=7),
|
48 |
+
title="microsoft/Phi-3-mini-128k-instruct",
|
49 |
+
description="""
|
50 |
+
This space is dedicated for chatting with Meta's Latest LLM - Llama 8b Instruct. Find this model here: https://huggingface.co/meta-llama/Meta-Llama-3-8B-Instruct
|
51 |
+
Feel free to play with customization in the "Additional Inputs".
|
52 |
+
""",
|
53 |
+
theme="soft",
|
54 |
+
additional_inputs=[
|
55 |
+
gr.Textbox("You are helpful AI.", label="System Prompt"),
|
56 |
+
gr.Slider(512, 4096, label="Max New Tokens"),
|
57 |
+
gr.Slider(0, 1, label="Temperature")
|
58 |
+
]
|
59 |
+
).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|