Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
import random
|
4 |
+
|
5 |
+
# Replace with your desired model ID from Hugging Face Hub
|
6 |
+
client = InferenceClient("unsloth/Llama-3.2-1B-Instruct")
|
7 |
+
|
8 |
+
words = [
|
9 |
+
# ... Add your list of nouns, adjectives, verbs, and adverbs here ...
|
10 |
+
]
|
11 |
+
|
12 |
+
|
13 |
+
class WordGame:
|
14 |
+
def __init__(self):
|
15 |
+
self.points = 0
|
16 |
+
self.target_word = ""
|
17 |
+
self.attempts = 3
|
18 |
+
self.generate_task()
|
19 |
+
|
20 |
+
def generate_task(self):
|
21 |
+
self.attempts = 3
|
22 |
+
self.target_word = random.choice(words)
|
23 |
+
st.session_state["target_word"] = self.target_word # Store in session state
|
24 |
+
st.title(f'The Game - Current score: {self.points}, remaining attempts: {self.attempts}')
|
25 |
+
|
26 |
+
def check_input_for_word(self, user_input):
|
27 |
+
if self.target_word in user_input.lower():
|
28 |
+
self.generate_task()
|
29 |
+
self.points -= 1
|
30 |
+
st.write(f"You input the target word yourself, so you lost one point and the game reset.")
|
31 |
+
return True
|
32 |
+
else:
|
33 |
+
return False
|
34 |
+
|
35 |
+
def check_output_for_word(self, response):
|
36 |
+
if self.target_word in response.lower():
|
37 |
+
self.points += self.attempts
|
38 |
+
score_gained = self.attempts
|
39 |
+
self.generate_task()
|
40 |
+
return f"Success! You earned {score_gained} points!"
|
41 |
+
else:
|
42 |
+
self.attempts -= 1
|
43 |
+
if self.attempts <= 0:
|
44 |
+
self.generate_task()
|
45 |
+
return f"You did not win in three attempts. Generating new target word."
|
46 |
+
else:
|
47 |
+
return "That didn't quite hit the mark. Try again!"
|
48 |
+
|
49 |
+
def respond(self, user_message):
|
50 |
+
messages = [{"role": "system", "content": st.session_state["system_message"]}]
|
51 |
+
|
52 |
+
for val in st.session_state["history"]:
|
53 |
+
if val[0]:
|
54 |
+
messages.append({"role": "user", "content": val[0]})
|
55 |
+
if val[1]:
|
56 |
+
messages.append({"role": "assistant", "content": val[1]})
|
57 |
+
|
58 |
+
messages.append({"role": "user", "content": user_message})
|
59 |
+
response = ""
|
60 |
+
for message in client.chat_completion(
|
61 |
+
messages, max_tokens=st.session_state["max_tokens"], stream=True,
|
62 |
+
temperature=st.session_state["temperature"], top_p=st.session_state["top_p"]
|
63 |
+
):
|
64 |
+
token = message.choices[0].delta.content
|
65 |
+
response += token
|
66 |
+
|
67 |
+
output_check_result = self.check_output_for_word(response)
|
68 |
+
st.write(response + f"\n\n---\n\n{output_check_result}")
|
69 |
+
st.write(f'Current score: {self.points}, remaining attempts: {self.attempts}')
|
70 |
+
|
71 |
+
# Update session state with history and system message
|
72 |
+
st.session_state["history"].append((user_message, response))
|
73 |
+
st.session_state["system_message"] = response
|
74 |
+
|
75 |
+
|
76 |
+
game = WordGame()
|
77 |
+
|
78 |
+
# Session state variables to store dynamic information
|
79 |
+
st.session_state["history"] = [] # List of tuples (user message, response)
|
80 |
+
st.session_state["system_message"] = "You are a friendly Chatbot." # Initial system message
|
81 |
+
|
82 |
+
# Streamlit interface elements
|
83 |
+
system_message_input = st.text_input("System message", value=st.session_state["system_message"])
|
84 |
+
max_tokens_slider = st.slider("Max new tokens", minimum=1, maximum=2048, value=512, step=1)
|
85 |
+
temperature_slider = st.slider("Temperature", minimum=0.1, maximum=4.0, value=0.7, step=0.1)
|
86 |
+
top_p_slider = st.slider("Top-p (nucleus sampling)", minimum=0.1, maximum=1.0, value=0.95, step=0.05)
|
87 |
+
|
88 |
+
# Update session state with user input for system message
|
89 |
+
st.session_state["system_message"] = system_message_input
|
90 |
+
|
91 |
+
user_message = st.text_
|