joermd commited on
Commit
4b8d1fe
·
verified ·
1 Parent(s): f4d2f94

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -6
app.py CHANGED
@@ -3,49 +3,74 @@ import streamlit as st
3
  import torch
4
  from transformers import AutoModelForCausalLM, AutoTokenizer
5
  import os
 
6
  # Random dog images for error messages
7
  random_dog = [
8
  "0f476473-2d8b-415e-b944-483768418a95.jpg",
9
  "1bd75c81-f1d7-4e55-9310-a27595fa8762.jpg",
10
  # Add more images as needed
11
  ]
 
12
  # Function to reset conversation
13
  def reset_conversation():
14
  '''Resets conversation'''
15
  st.session_state.conversation = []
16
  st.session_state.messages = []
17
  return None
 
18
  # Sidebar controls
19
  temp_values = st.sidebar.slider('Select a temperature value', 0.0, 1.0, 0.5)
20
  max_token_value = st.sidebar.slider('Select a max_token value', 1000, 9000, 5000)
21
  st.sidebar.button('Reset Chat', on_click=reset_conversation)
 
22
  # Initialize chat history
23
  if "messages" not in st.session_state:
24
  st.session_state.messages = []
 
25
  # Display chat messages from history on app rerun
26
  for message in st.session_state.messages:
27
  with st.chat_message(message["role"]):
28
  st.markdown(message["content"])
 
29
  # Set cache directory path to /data
30
  cache_dir = "/data" # المسار المحدد للتخزين في مساحة Hugging Face
 
31
  # Load model and tokenizer on-demand to save memory
32
  if prompt := st.chat_input(f"مرحبا انا سبيدي , كيف استطيع مساعدتك ؟"):
33
  with st.chat_message("user"):
34
  st.markdown(prompt)
35
  st.session_state.messages.append({"role": "user", "content": prompt})
 
36
  # Load model only when user submits a prompt
37
  try:
38
  # Load the tokenizer and model with caching in the specified directory
39
- tokenizer = AutoTokenizer.from_pretrained("sambanovasystems/SambaLingo-Arabic-Chat", cache_dir=cache_dir)
40
- model = AutoModelForCausalLM.from_pretrained("sambanovasystems/SambaLingo-Arabic-Chat", cache_dir=cache_dir)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  # Generate response
43
- inputs = tokenizer(prompt, return_tensors="pt")
44
  outputs = model.generate(
45
  inputs.input_ids,
46
  max_new_tokens=max_token_value,
47
  temperature=temp_values,
48
- do_sample=True
 
 
49
  )
50
  assistant_response = tokenizer.decode(outputs[0], skip_special_tokens=True)
51
 
@@ -55,10 +80,11 @@ if prompt := st.chat_input(f"مرحبا انا سبيدي , كيف استطيع
55
  del model
56
 
57
  except Exception as e:
58
- assistant_response = "😵‍💫 Connection issue! Try again later. Here's a 🐶:"
59
  st.image(f'https://random.dog/{random_dog[np.random.randint(len(random_dog))]}')
60
- st.write("Error message:")
61
  st.write(e)
 
62
  # Display assistant response
63
  with st.chat_message("assistant"):
64
  st.markdown(assistant_response)
 
3
  import torch
4
  from transformers import AutoModelForCausalLM, AutoTokenizer
5
  import os
6
+
7
  # Random dog images for error messages
8
  random_dog = [
9
  "0f476473-2d8b-415e-b944-483768418a95.jpg",
10
  "1bd75c81-f1d7-4e55-9310-a27595fa8762.jpg",
11
  # Add more images as needed
12
  ]
13
+
14
  # Function to reset conversation
15
  def reset_conversation():
16
  '''Resets conversation'''
17
  st.session_state.conversation = []
18
  st.session_state.messages = []
19
  return None
20
+
21
  # Sidebar controls
22
  temp_values = st.sidebar.slider('Select a temperature value', 0.0, 1.0, 0.5)
23
  max_token_value = st.sidebar.slider('Select a max_token value', 1000, 9000, 5000)
24
  st.sidebar.button('Reset Chat', on_click=reset_conversation)
25
+
26
  # Initialize chat history
27
  if "messages" not in st.session_state:
28
  st.session_state.messages = []
29
+
30
  # Display chat messages from history on app rerun
31
  for message in st.session_state.messages:
32
  with st.chat_message(message["role"]):
33
  st.markdown(message["content"])
34
+
35
  # Set cache directory path to /data
36
  cache_dir = "/data" # المسار المحدد للتخزين في مساحة Hugging Face
37
+
38
  # Load model and tokenizer on-demand to save memory
39
  if prompt := st.chat_input(f"مرحبا انا سبيدي , كيف استطيع مساعدتك ؟"):
40
  with st.chat_message("user"):
41
  st.markdown(prompt)
42
  st.session_state.messages.append({"role": "user", "content": prompt})
43
+
44
  # Load model only when user submits a prompt
45
  try:
46
  # Load the tokenizer and model with caching in the specified directory
47
+ tokenizer = AutoTokenizer.from_pretrained("joermd/speedy-llama2", cache_dir=cache_dir)
48
+ model = AutoModelForCausalLM.from_pretrained(
49
+ "joermd/speedy-llama2",
50
+ cache_dir=cache_dir,
51
+ torch_dtype=torch.bfloat16,
52
+ device_map="auto"
53
+ )
54
+
55
+ # Prepare the system message and conversation
56
+ system_message = {
57
+ "role": "system",
58
+ "content": "You are a friendly chatbot who answers questions in Arabic."
59
+ }
60
+ messages = [system_message, {"role": "user", "content": prompt}]
61
+
62
+ # Create conversation prompt using chat template
63
+ conversation = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
64
 
65
  # Generate response
66
+ inputs = tokenizer(conversation, return_tensors="pt")
67
  outputs = model.generate(
68
  inputs.input_ids,
69
  max_new_tokens=max_token_value,
70
  temperature=temp_values,
71
+ do_sample=True,
72
+ top_k=50,
73
+ top_p=0.95
74
  )
75
  assistant_response = tokenizer.decode(outputs[0], skip_special_tokens=True)
76
 
 
80
  del model
81
 
82
  except Exception as e:
83
+ assistant_response = "😵‍💫 عذراً، حدث خطأ في الاتصال! حاول مرة أخرى لاحقاً. إليك صورة كلب 🐶:"
84
  st.image(f'https://random.dog/{random_dog[np.random.randint(len(random_dog))]}')
85
+ st.write("رسالة الخطأ:")
86
  st.write(e)
87
+
88
  # Display assistant response
89
  with st.chat_message("assistant"):
90
  st.markdown(assistant_response)