Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,63 +1,50 @@
|
|
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 |
-
|
28 |
-
response =
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
""
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
gr.Slider(
|
52 |
-
minimum=0.1,
|
53 |
-
maximum=1.0,
|
54 |
-
value=0.95,
|
55 |
-
step=0.05,
|
56 |
-
label="Top-p (nucleus sampling)",
|
57 |
-
),
|
58 |
-
],
|
59 |
-
)
|
60 |
-
|
61 |
-
|
62 |
-
if __name__ == "__main__":
|
63 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Optimized Loading: Load in half precision if CUDA is available
|
6 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
7 |
+
|
8 |
+
# Load the model and tokenizer
|
9 |
+
model_name = "Sephfox/A.I.R.R"
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
11 |
+
model = AutoModelForCausalLM.from_pretrained(
|
12 |
+
model_name,
|
13 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
14 |
+
device_map="auto"
|
15 |
+
).to(device)
|
16 |
+
|
17 |
+
def generate_response(prompt):
|
18 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
19 |
+
outputs = model.generate(
|
20 |
+
**inputs,
|
21 |
+
max_length=200,
|
22 |
+
num_return_sequences=1,
|
23 |
+
pad_token_id=tokenizer.eos_token_id,
|
24 |
+
do_sample=True,
|
25 |
+
temperature=0.7,
|
26 |
+
top_p=0.9
|
27 |
+
)
|
28 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
29 |
+
return response
|
30 |
+
|
31 |
+
# Create Gradio chat interface
|
32 |
+
def chat_bot(user_input, history=[]):
|
33 |
+
bot_response = generate_response(user_input)
|
34 |
+
history.append((user_input, bot_response))
|
35 |
+
return history, history
|
36 |
+
|
37 |
+
with gr.Blocks() as demo:
|
38 |
+
gr.Markdown("# A.I.R.R Chatbot (Optimized)")
|
39 |
+
chatbot = gr.Chatbot(label="Chat with A.I.R.R")
|
40 |
+
user_input = gr.Textbox(show_label=False, placeholder="Type your message here...")
|
41 |
+
state = gr.State([])
|
42 |
+
submit_button = gr.Button("Send")
|
43 |
+
|
44 |
+
submit_button.click(
|
45 |
+
fn=chat_bot,
|
46 |
+
inputs=[user_input, state],
|
47 |
+
outputs=[chatbot, state]
|
48 |
+
)
|
49 |
+
|
50 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|