Alexander Hux commited on
Commit
3fa5046
·
verified ·
1 Parent(s): 6b55222

Create chatbot_app.py

Browse files
Files changed (1) hide show
  1. chatbot_app.py +55 -0
chatbot_app.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ import torch
4
+
5
+ # Load pre-trained model and tokenizer
6
+ model_name = "gpt2" # You can replace this with any other suitable model
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForCausalLM.from_pretrained(model_name)
9
+
10
+ # Streamlit app
11
+ st.title("Interactive Chatbot with Games and Apps")
12
+
13
+ # Chat interface
14
+ user_input = st.text_input("You:", "")
15
+ if st.button("Send"):
16
+ # Generate response
17
+ input_ids = tokenizer.encode(user_input, return_tensors="pt")
18
+ output = model.generate(input_ids, max_length=100, num_return_sequences=1)
19
+ response = tokenizer.decode(output[0], skip_special_tokens=True)
20
+
21
+ st.text_area("Chatbot:", value=response, height=200)
22
+
23
+ # Check for special commands
24
+ if "play game" in user_input.lower():
25
+ st.subheader("Interactive Game")
26
+ st.write("Here's a simple game:")
27
+ game_choice = st.selectbox("Choose your move:", ["Rock", "Paper", "Scissors"])
28
+ if st.button("Play"):
29
+ import random
30
+ computer_choice = random.choice(["Rock", "Paper", "Scissors"])
31
+ st.write(f"Computer chose: {computer_choice}")
32
+ if game_choice == computer_choice:
33
+ st.write("It's a tie!")
34
+ elif (game_choice == "Rock" and computer_choice == "Scissors") or \
35
+ (game_choice == "Paper" and computer_choice == "Rock") or \
36
+ (game_choice == "Scissors" and computer_choice == "Paper"):
37
+ st.write("You win!")
38
+ else:
39
+ st.write("Computer wins!")
40
+
41
+ elif "show presentation" in user_input.lower():
42
+ st.subheader("Interactive Presentation")
43
+ st.write("Here's a simple presentation:")
44
+ slide_number = st.slider("Select slide", 1, 5)
45
+ st.write(f"Slide {slide_number} content goes here.")
46
+
47
+ elif "launch web app" in user_input.lower():
48
+ st.subheader("Interactive Web App")
49
+ st.write("Here's a simple web app:")
50
+ name = st.text_input("Enter your name:")
51
+ age = st.number_input("Enter your age:", min_value=0, max_value=120)
52
+ if st.button("Submit"):
53
+ st.write(f"Hello, {name}! You are {age} years old.")
54
+
55
+ # Run the app using: streamlit run chatbot_app.py