mudassir032 commited on
Commit
6f65240
Β·
verified Β·
1 Parent(s): af712e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -7
app.py CHANGED
@@ -1,9 +1,10 @@
1
  from langchain_groq import ChatGroq
2
  import streamlit as st
3
  from crewai import Agent
 
4
 
5
  # Initialize LLM
6
- llm = ChatGroq(temperature=0.4, model_name="llama-3.3-70b-versatile", api_key="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")
7
 
8
  # Define Agents
9
  class NarratorAgent(Agent):
@@ -12,7 +13,11 @@ class NarratorAgent(Agent):
12
 
13
  class QuestionAgent(Agent):
14
  def act(self, level, history):
15
- return llm.invoke(f"Generate a general knowledge question for a Q and A game. The player is at level {level}. Only generate the question, not the answer, and avoid repeating questions from history: {history}").content
 
 
 
 
16
 
17
  class HintAgent(Agent):
18
  def act(self, level, question):
@@ -32,14 +37,30 @@ validator = AnswerCheckerAgent(role="Validator", goal="Check if the answer is co
32
  st.title("🏰 Knowledge Dungeon 🏰")
33
  st.subheader("Answer correctly to defeat enemies and escape!")
34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  # Sidebar for Level & Points Display
36
  st.sidebar.header("πŸ“œ Game Stats")
37
  if "level" not in st.session_state:
38
  st.session_state.level = 1
39
  st.session_state.points = 0
40
- st.session_state.history = []
41
  st.session_state.questions_answered = 0
42
- st.session_state.current_question = None # Stores the current question
43
  st.session_state.hint = None
44
 
45
  st.sidebar.write(f"**πŸ† Level:** {st.session_state.level}")
@@ -50,12 +71,15 @@ QUESTIONS_PER_LEVEL = 3
50
  POINTS_PER_CORRECT = 10
51
 
52
  def generate_new_question():
53
- """Generates a new question and stores it in session state."""
54
  question = questioner.act(st.session_state.level, st.session_state.history)
55
  st.session_state.current_question = question
56
  st.session_state.history.append(question)
57
  st.session_state.hint = hinter.act(st.session_state.level, question)
58
 
 
 
 
59
  def main():
60
  level = st.session_state.level
61
 
@@ -99,8 +123,8 @@ def main():
99
 
100
  # Generate new question and force UI refresh
101
  generate_new_question()
102
- st.rerun() # Forces Streamlit to refresh immediately
103
  else:
104
  st.error("❌ Wrong answer! Try again.")
105
 
106
- main()
 
1
  from langchain_groq import ChatGroq
2
  import streamlit as st
3
  from crewai import Agent
4
+ import os
5
 
6
  # Initialize LLM
7
+ llm = ChatGroq(temperature=0.4, model_name="llama-3.3-70b-versatile")
8
 
9
  # Define Agents
10
  class NarratorAgent(Agent):
 
13
 
14
  class QuestionAgent(Agent):
15
  def act(self, level, history):
16
+ """Generate a unique question that is not in history"""
17
+ while True:
18
+ question = llm.invoke(f"Generate a general knowledge question for a Q and A game. The player is at level {level}. Avoid repeating questions from history: {history}").content
19
+ if question not in history:
20
+ return question
21
 
22
  class HintAgent(Agent):
23
  def act(self, level, question):
 
37
  st.title("🏰 Knowledge Dungeon 🏰")
38
  st.subheader("Answer correctly to defeat enemies and escape!")
39
 
40
+ # File to store history
41
+ HISTORY_FILE = "history.txt"
42
+
43
+ # Load question history from file
44
+ def load_history():
45
+ if os.path.exists(HISTORY_FILE):
46
+ with open(HISTORY_FILE, "r") as f:
47
+ return [line.strip() for line in f.readlines()]
48
+ return []
49
+
50
+ # Save question history to file
51
+ def save_history(history):
52
+ with open(HISTORY_FILE, "w") as f:
53
+ for question in history:
54
+ f.write(question + "\n")
55
+
56
  # Sidebar for Level & Points Display
57
  st.sidebar.header("πŸ“œ Game Stats")
58
  if "level" not in st.session_state:
59
  st.session_state.level = 1
60
  st.session_state.points = 0
61
+ st.session_state.history = load_history()
62
  st.session_state.questions_answered = 0
63
+ st.session_state.current_question = None
64
  st.session_state.hint = None
65
 
66
  st.sidebar.write(f"**πŸ† Level:** {st.session_state.level}")
 
71
  POINTS_PER_CORRECT = 10
72
 
73
  def generate_new_question():
74
+ """Generates a unique new question and stores it in session state."""
75
  question = questioner.act(st.session_state.level, st.session_state.history)
76
  st.session_state.current_question = question
77
  st.session_state.history.append(question)
78
  st.session_state.hint = hinter.act(st.session_state.level, question)
79
 
80
+ # Save updated history to file
81
+ save_history(st.session_state.history)
82
+
83
  def main():
84
  level = st.session_state.level
85
 
 
123
 
124
  # Generate new question and force UI refresh
125
  generate_new_question()
126
+ st.rerun()
127
  else:
128
  st.error("❌ Wrong answer! Try again.")
129
 
130
+ main()