Allahbux commited on
Commit
ac00ffa
·
verified ·
1 Parent(s): b472547

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -12
app.py CHANGED
@@ -1,43 +1,53 @@
1
  import streamlit as st
2
- from transformers import pipeline
3
 
4
- # Set Streamlit page configuration
5
  st.set_page_config(page_title="AI Chatbot", layout="centered")
6
 
7
- # Load the model pipeline (cached to avoid reloading on each run)
8
  @st.cache_resource
9
  def load_pipeline():
10
  model_name = "Orenguteng/Llama-3.1-8B-Lexi-Uncensored-V2"
11
- return pipeline("text-generation", model=model_name)
 
 
 
 
 
 
 
 
12
 
13
  pipe = load_pipeline()
14
 
15
- # App UI
16
  st.title("🤖 AI Chatbot")
17
  st.markdown(
18
  """
19
  Welcome to the **AI Chatbot** powered by Hugging Face's **Llama-3.1-8B-Lexi-Uncensored-V2** model.
20
- Enter your message below, and the AI will respond!
21
  """
22
  )
23
 
24
- # Input Textbox
25
  user_input = st.text_area(
26
  "Your Message",
27
  placeholder="Type your message here...",
28
  height=100
29
  )
30
 
31
- # Generate Button and Response
32
  if st.button("Generate Response"):
33
  if user_input.strip():
34
  with st.spinner("Generating response..."):
35
- # Generate a response
36
- response = pipe(user_input, max_length=150, num_return_sequences=1)
37
- st.text_area("Response", value=response[0]['generated_text'], height=200)
 
 
38
  else:
39
  st.warning("Please enter a message before clicking the button.")
40
 
41
  # Footer
42
  st.markdown("---")
43
- st.markdown("Made with ❤️ using [Streamlit](https://streamlit.io) and [Hugging Face](https://huggingface.co).")
 
1
  import streamlit as st
2
+ from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
3
 
4
+ # Streamlit app configuration
5
  st.set_page_config(page_title="AI Chatbot", layout="centered")
6
 
7
+ # Load the model pipeline
8
  @st.cache_resource
9
  def load_pipeline():
10
  model_name = "Orenguteng/Llama-3.1-8B-Lexi-Uncensored-V2"
11
+
12
+ # Load tokenizer and model
13
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
14
+ model = AutoModelForCausalLM.from_pretrained(
15
+ model_name,
16
+ device_map="auto", # Use GPU if available
17
+ rope_scaling=None # Avoid issues with rope_scaling
18
+ )
19
+ return pipeline("text-generation", model=model, tokenizer=tokenizer)
20
 
21
  pipe = load_pipeline()
22
 
23
+ # Streamlit App UI
24
  st.title("🤖 AI Chatbot")
25
  st.markdown(
26
  """
27
  Welcome to the **AI Chatbot** powered by Hugging Face's **Llama-3.1-8B-Lexi-Uncensored-V2** model.
28
+ Type your message below and interact with the AI!
29
  """
30
  )
31
 
32
+ # User input area
33
  user_input = st.text_area(
34
  "Your Message",
35
  placeholder="Type your message here...",
36
  height=100
37
  )
38
 
39
+ # Button to generate response
40
  if st.button("Generate Response"):
41
  if user_input.strip():
42
  with st.spinner("Generating response..."):
43
+ try:
44
+ response = pipe(user_input, max_length=150, num_return_sequences=1)
45
+ st.text_area("Response", value=response[0]["generated_text"], height=200)
46
+ except Exception as e:
47
+ st.error(f"An error occurred: {e}")
48
  else:
49
  st.warning("Please enter a message before clicking the button.")
50
 
51
  # Footer
52
  st.markdown("---")
53
+ st.markdown("Made with ❤️ using [Streamlit](https://streamlit.io) and [Hugging Face](https://huggingface.co).")