Update app.py
Browse files
app.py
CHANGED
|
@@ -10,43 +10,83 @@ 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 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 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 |
################ STATEMENT SUMMARIZATION #################
|
| 52 |
|
|
|
|
| 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 |
|