YAMITEK commited on
Commit
c5117ff
ยท
verified ยท
1 Parent(s): 7f638f3

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +94 -0
  2. requirements.txt +7 -0
app.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM
4
+ import os
5
+ from huggingface_hub import login
6
+
7
+ # Hugging Face Authentication
8
+ hf_token = os.getenv("HUGGINGFACE_TOKEN", "").strip()
9
+
10
+ if not hf_token:
11
+ st.error("HUGGINGFACE_TOKEN not found. Please set your Hugging Face token.")
12
+ st.stop()
13
+
14
+ login(token=hf_token)
15
+
16
+ # Load Model & Tokenizer
17
+ model_name = "meta-llama/Llama-2-7b-chat-hf" # Use the chat model
18
+
19
+ @st.cache_resource
20
+ def load_model():
21
+ # Load tokenizer
22
+ tokenizer = AutoTokenizer.from_pretrained(model_name, token=hf_token)
23
+
24
+ # Load model with FP16 (half-precision) on CPU
25
+ model = AutoModelForCausalLM.from_pretrained(
26
+ model_name,
27
+ device_map="cpu", # Force CPU usage
28
+ torch_dtype=torch.float16, # Use FP16 to reduce memory usage
29
+ token=hf_token
30
+ )
31
+
32
+ return tokenizer, model
33
+
34
+ tokenizer, model = load_model()
35
+
36
+ # Function to classify text using a prompt-based approach
37
+ def classify_text(text, classes):
38
+ # Create a prompt for classification
39
+ prompt = f"""
40
+ Classify the following text into one of these categories: {", ".join(classes)}.
41
+ Text: {text}
42
+ Category:
43
+ """
44
+
45
+ # Tokenize the prompt
46
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
47
+
48
+ # Generate the output
49
+ with torch.no_grad():
50
+ outputs = model.generate(**inputs, max_length=100, num_return_sequences=1)
51
+
52
+ # Decode the output
53
+ decoded_output = tokenizer.decode(outputs[0], skip_special_tokens=True)
54
+
55
+ # Extract the predicted class
56
+ predicted_class = decoded_output.split("Category:")[-1].strip()
57
+ return predicted_class
58
+
59
+ # Custom CSS to make all text red
60
+ st.markdown(
61
+ """
62
+ <style>
63
+ /* Target all text elements */
64
+ body, h1, h2, h3, h4, h5, h6, p, div, span, input, textarea, button, label {
65
+ color: #E25822 !important;
66
+ }
67
+ </style>
68
+ """,
69
+ unsafe_allow_html=True
70
+ )
71
+
72
+ # Streamlit UI
73
+ st.title("๐Ÿ“ Text Classification with LLaMA 2 Chat (FP16)")
74
+ st.write("Powered by LLaMA 2 Chat & Hugging Face")
75
+
76
+ # User Input
77
+ user_input = st.text_area("Enter the text to classify:")
78
+
79
+ # Define classes for classification
80
+ classes = ["Positive", "Negative", "Neutral"]
81
+
82
+ if st.button("Classify"):
83
+ if user_input:
84
+ # Perform classification
85
+ predicted_class = classify_text(user_input, classes)
86
+
87
+ # Display result
88
+ st.subheader("Predicted Class:")
89
+ st.write(predicted_class)
90
+ else:
91
+ st.warning("Please enter some text to classify.")
92
+
93
+ st.markdown("---")
94
+ st.write("๐Ÿ” This app classifies text using the LLaMA 2 Chat model with FP16.")
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ streamlit
2
+ torch
3
+ transformers
4
+ huggingface-hub
5
+ sentencepiece
6
+ accelerate>=0.26.0
7
+ bitsandbytes