Sumayyea commited on
Commit
e0a377a
·
verified ·
1 Parent(s): b497157

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -25
app.py CHANGED
@@ -1,26 +1,50 @@
1
- import streamlit as st
2
- from langchain.llms import HuggingFaceHub
3
- import os
4
-
5
- # Replace with your actual Hugging Face token
6
- hf_token = os.getenv('HF_TOKEN2')
7
-
8
- # Initialize the HuggingFace model
9
- repo_id = "gpt2" # Make sure this is the correct repository ID
10
- llm = HuggingFaceHub(repo_id=repo_id, huggingfacehub_api_token=hf_token, model_kwargs={"max_length": 128, "temperature": 0.7})
11
-
12
- # Streamlit interface
13
- st.title("Chat with GPT-2")
14
- st.write("This chatbot is powered by a GPT-2 model hosted on Hugging Face.")
15
-
16
- user_input = st.text_input("You:", "Type your message here...")
17
-
18
- if st.button("Send"):
19
- if user_input:
20
- try:
21
- response = llm(user_input)
22
- st.write(f"Bot: {response}")
23
- except Exception as e:
24
- st.error(f"Error: {str(e)}")
25
- else:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  st.write("Please enter a message.")
 
1
+ import streamlit as st
2
+ from transformers import pipeline, GPT2Tokenizer, GPT2LMHeadModel
3
+ import torch
4
+
5
+ # Streamlit interface
6
+ st.title("Chat with GPT-2")
7
+ st.write("This chatbot is powered by a GPT-2 model from Hugging Face.")
8
+
9
+ # Initialize the GPT-2 model and tokenizer
10
+ @st.cache_resource
11
+ def load_model():
12
+ model_name = "gpt2"
13
+ tokenizer = GPT2Tokenizer.from_pretrained(model_name)
14
+ model = GPT2LMHeadModel.from_pretrained(model_name)
15
+ return tokenizer, model
16
+
17
+ tokenizer, model = load_model()
18
+
19
+ # Create text generation pipeline
20
+ generator = pipeline(
21
+ "text-generation",
22
+ model=model,
23
+ tokenizer=tokenizer,
24
+ device=0 if torch.cuda.is_available() else -1
25
+ )
26
+
27
+ user_input = st.text_input("You:", "Type your message here...")
28
+
29
+ if st.button("Send"):
30
+ if user_input:
31
+ try:
32
+ # Generate response
33
+ response = generator(
34
+ user_input,
35
+ max_length=128,
36
+ num_return_sequences=1,
37
+ temperature=0.7,
38
+ pad_token_id=tokenizer.eos_token_id
39
+ )
40
+ bot_response = response[0]['generated_text']
41
+
42
+ # Clean up the response by removing the input text if it appears
43
+ if user_input in bot_response:
44
+ bot_response = bot_response.replace(user_input, "").strip()
45
+
46
+ st.write(f"Bot: {bot_response}")
47
+ except Exception as e:
48
+ st.error(f"Error: {str(e)}")
49
+ else:
50
  st.write("Please enter a message.")