richardorama
commited on
Commit
β’
f5cd439
1
Parent(s):
61944f0
Update app.py
Browse files
app.py
CHANGED
@@ -5,88 +5,11 @@
|
|
5 |
#st.write(x, 'squared is', x * x)
|
6 |
|
7 |
import streamlit as st
|
8 |
-
from transformers import pipeline
|
9 |
import ast
|
10 |
|
11 |
st.title("Assorted Language Tools - AI Craze")
|
12 |
|
13 |
-
# ################ CHAT BOT #################
|
14 |
-
|
15 |
-
# # Load the GPT model
|
16 |
-
# generator = pipeline("text-generation", model="EleutherAI/gpt-neo-2.7B")
|
17 |
-
|
18 |
-
# # Streamlit chat UI
|
19 |
-
# #st.title("GPT-3 Chatbox")
|
20 |
-
|
21 |
-
# # user_input = st.text_input("You: ", "Hello, how are you?")
|
22 |
-
|
23 |
-
# # if user_input:
|
24 |
-
# # response = generator(user_input, max_length=100, num_return_sequences=1)[0]['generated_text']
|
25 |
-
# # st.write(f"GPT-3: {response}")
|
26 |
-
|
27 |
-
# # Define the summarization function
|
28 |
-
# def chat(txt):
|
29 |
-
# st.write('\n\n')
|
30 |
-
# #st.write(txt[:100]) # Display the first 100 characters of the article
|
31 |
-
# #st.write('--------------------------------------------------------------')
|
32 |
-
# #summary = summarizer(txt, max_length=500, min_length=30, do_sample=False)
|
33 |
-
# #st.write(summary[0]['summary_text'])
|
34 |
-
# response = generator(txt, max_length=500, num_return_sequences=1)[0]['generated_text']
|
35 |
-
# st.write(f"GPT-3: {response}")
|
36 |
-
|
37 |
-
# DEFAULT_CHAT = ""
|
38 |
-
# # Create a text area for user input
|
39 |
-
# CHAT = st.sidebar.text_area('Enter Chat (String)', DEFAULT_CHAT, height=150)
|
40 |
-
|
41 |
-
# # Enable the button only if there is text in the CHAT variable
|
42 |
-
# if CHAT:
|
43 |
-
# if st.sidebar.button('Chat Statement'):
|
44 |
-
# # Call your Summarize function here
|
45 |
-
# chat(CHAT) # Directly pass the your
|
46 |
-
# else:
|
47 |
-
# st.sidebar.button('Chat Statement', disabled=True)
|
48 |
-
# st.warning('π Please enter Chat!')
|
49 |
-
|
50 |
-
|
51 |
-
import streamlit as st
|
52 |
-
from transformers import pipeline, GPT2Tokenizer, GPT2LMHeadModel
|
53 |
-
|
54 |
-
# Load pre-trained GPT-2 model and tokenizer
|
55 |
-
model_name = "gpt2" # Use "gpt-3.5-turbo" or another model from Hugging Face if needed
|
56 |
-
model = GPT2LMHeadModel.from_pretrained(model_name)
|
57 |
-
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
|
58 |
-
|
59 |
-
# Initialize the text generation pipeline
|
60 |
-
gpt_pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
61 |
-
|
62 |
-
# Streamlit UI
|
63 |
-
st.title("Chat with GPT-2")
|
64 |
-
|
65 |
-
if 'conversation' not in st.session_state:
|
66 |
-
st.session_state.conversation = ""
|
67 |
-
|
68 |
-
def chat_with_gpt(user_input):
|
69 |
-
# Append user input to the conversation
|
70 |
-
st.session_state.conversation += f"User: {user_input}\n"
|
71 |
-
|
72 |
-
# Generate response
|
73 |
-
response = gpt_pipeline(user_input, max_length=100, num_return_sequences=1)[0]['generated_text']
|
74 |
-
response_text = response.replace(user_input, '') # Strip the user input part from response
|
75 |
-
|
76 |
-
# Append GPT's response to the conversation
|
77 |
-
st.session_state.conversation += f"GPT: {response_text}\n"
|
78 |
-
return response_text
|
79 |
-
|
80 |
-
# Text input for user query
|
81 |
-
user_input = st.text_input("You:", "")
|
82 |
-
|
83 |
-
if st.button("Send"):
|
84 |
-
if user_input:
|
85 |
-
chat_with_gpt(user_input)
|
86 |
-
|
87 |
-
# Display conversation history
|
88 |
-
st.text_area("Conversation", value=st.session_state.conversation, height=400)
|
89 |
-
|
90 |
|
91 |
################ STATEMENT SUMMARIZATION #################
|
92 |
|
@@ -164,6 +87,85 @@ else:
|
|
164 |
st.warning('π Please enter Sentiment!')
|
165 |
|
166 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
# Add a footnote at the bottom
|
168 |
st.markdown("---") # Horizontal line to separate content from footnote
|
169 |
st.markdown("Orama's AI Craze")
|
|
|
5 |
#st.write(x, 'squared is', x * x)
|
6 |
|
7 |
import streamlit as st
|
8 |
+
from transformers import pipeline, GPT2Tokenizer, GPT2LMHeadModel
|
9 |
import ast
|
10 |
|
11 |
st.title("Assorted Language Tools - AI Craze")
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
################ STATEMENT SUMMARIZATION #################
|
15 |
|
|
|
87 |
st.warning('π Please enter Sentiment!')
|
88 |
|
89 |
|
90 |
+
# ################ CHAT BOT #################
|
91 |
+
|
92 |
+
# # Load the GPT model
|
93 |
+
# generator = pipeline("text-generation", model="EleutherAI/gpt-neo-2.7B")
|
94 |
+
|
95 |
+
# # Streamlit chat UI
|
96 |
+
# #st.title("GPT-3 Chatbox")
|
97 |
+
|
98 |
+
# # user_input = st.text_input("You: ", "Hello, how are you?")
|
99 |
+
|
100 |
+
# # if user_input:
|
101 |
+
# # response = generator(user_input, max_length=100, num_return_sequences=1)[0]['generated_text']
|
102 |
+
# # st.write(f"GPT-3: {response}")
|
103 |
+
|
104 |
+
# # Define the summarization function
|
105 |
+
# def chat(txt):
|
106 |
+
# st.write('\n\n')
|
107 |
+
# #st.write(txt[:100]) # Display the first 100 characters of the article
|
108 |
+
# #st.write('--------------------------------------------------------------')
|
109 |
+
# #summary = summarizer(txt, max_length=500, min_length=30, do_sample=False)
|
110 |
+
# #st.write(summary[0]['summary_text'])
|
111 |
+
# response = generator(txt, max_length=500, num_return_sequences=1)[0]['generated_text']
|
112 |
+
# st.write(f"GPT-3: {response}")
|
113 |
+
|
114 |
+
# DEFAULT_CHAT = ""
|
115 |
+
# # Create a text area for user input
|
116 |
+
# CHAT = st.sidebar.text_area('Enter Chat (String)', DEFAULT_CHAT, height=150)
|
117 |
+
|
118 |
+
# # Enable the button only if there is text in the CHAT variable
|
119 |
+
# if CHAT:
|
120 |
+
# if st.sidebar.button('Chat Statement'):
|
121 |
+
# # Call your Summarize function here
|
122 |
+
# chat(CHAT) # Directly pass the your
|
123 |
+
# else:
|
124 |
+
# st.sidebar.button('Chat Statement', disabled=True)
|
125 |
+
# st.warning('π Please enter Chat!')
|
126 |
+
|
127 |
+
|
128 |
+
|
129 |
+
|
130 |
+
# Load pre-trained GPT-2 model and tokenizer
|
131 |
+
model_name = "gpt-3.5-turbo" # "gpt2" # Use "gpt-3.5-turbo" or another model from Hugging Face if needed
|
132 |
+
model = GPT2LMHeadModel.from_pretrained(model_name)
|
133 |
+
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
|
134 |
+
|
135 |
+
# Initialize the text generation pipeline
|
136 |
+
gpt_pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
137 |
+
|
138 |
+
# Streamlit UI
|
139 |
+
st.title("Chat with GPT-2")
|
140 |
+
|
141 |
+
if 'conversation' not in st.session_state:
|
142 |
+
st.session_state.conversation = ""
|
143 |
+
|
144 |
+
def chat_with_gpt(user_input):
|
145 |
+
# Append user input to the conversation
|
146 |
+
st.session_state.conversation += f"User: {user_input}\n"
|
147 |
+
|
148 |
+
# Generate response
|
149 |
+
response = gpt_pipeline(user_input, max_length=100, num_return_sequences=1)[0]['generated_text']
|
150 |
+
response_text = response.replace(user_input, '') # Strip the user input part from response
|
151 |
+
|
152 |
+
# Append GPT's response to the conversation
|
153 |
+
st.session_state.conversation += f"GPT: {response_text}\n"
|
154 |
+
return response_text
|
155 |
+
|
156 |
+
# Text input for user query
|
157 |
+
user_input = st.text_input("You:", "")
|
158 |
+
|
159 |
+
if st.button("Send"):
|
160 |
+
if user_input:
|
161 |
+
chat_with_gpt(user_input)
|
162 |
+
|
163 |
+
# Display conversation history
|
164 |
+
st.text_area("Conversation", value=st.session_state.conversation, height=400)
|
165 |
+
|
166 |
+
# ################ END #################
|
167 |
+
|
168 |
+
|
169 |
# Add a footnote at the bottom
|
170 |
st.markdown("---") # Horizontal line to separate content from footnote
|
171 |
st.markdown("Orama's AI Craze")
|