sproducts commited on
Commit
7b6ac70
·
verified ·
1 Parent(s): d325648

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -101
app.py CHANGED
@@ -1,117 +1,69 @@
1
  import os
2
- import sys
3
  import streamlit as st
4
- from huggingface_hub import InferenceClient
5
 
6
- # -------------------------------------------------
7
- # 1. Model Selection (DeepSeek-R1-Distill-Qwen-7B)
8
- # -------------------------------------------------
9
- MODEL_NAME = "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B"
10
- client = InferenceClient(MODEL_NAME)
11
-
12
- # -------------------------------------------------
13
- # 2. Load Roadmap and Rules
14
- # -------------------------------------------------
15
- def load_file(filename):
16
- """Reads a text file and returns its content."""
17
  try:
18
- with open(filename, "r", encoding="utf-8") as file:
19
  return file.read()
20
  except FileNotFoundError:
21
- return f"⚠️ Error: {filename} not found!"
22
-
23
- # Load project guidance files
24
- roadmap_content = load_file("roadmap.txt")
25
- rules_content = load_file("rules.txt")
26
-
27
- # -------------------------------------------------
28
- # 3. Chatbot Response Function (With Project Guidance)
29
- # -------------------------------------------------
30
- def respond(
31
- message,
32
- history: list[tuple[str, str]],
33
- system_message,
34
- temperature,
35
- top_p,
36
- ):
37
- """
38
- Generates an AI response while considering project roadmap and rules.
39
- """
40
- # System message includes roadmap and rules for AI context
41
- full_system_message = f"{system_message}\n\nProject Roadmap:\n{roadmap_content}\n\nProject Rules:\n{rules_content}"
42
-
43
- messages = [{"role": "system", "content": full_system_message}]
44
-
45
- for val in history:
46
- if val[0]: # User message
47
- messages.append({"role": "user", "content": val[0]})
48
- if val[1]: # AI response
49
- messages.append({"role": "assistant", "content": val[1]})
50
-
51
- messages.append({"role": "user", "content": message})
52
-
53
- response = ""
54
 
 
 
 
55
  try:
56
- for message in client.chat_completion(
57
- messages,
58
- max_tokens=None, # 🚀 No token limit
59
- stream=True,
60
- temperature=temperature,
61
- top_p=top_p,
62
- ):
63
- token = message.choices[0].delta.content
64
-
65
- if token:
66
- response += token
67
- yield response # Stream response live
68
  except Exception as e:
69
- yield f"⚠️ Error: {str(e)}" # Show error messages
70
 
71
- # -------------------------------------------------
72
- # 4. Set up the Streamlit UI
73
- # -------------------------------------------------
74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  st.title("Project Guidance AI Chatbot")
76
- st.write("Chat with the AI to get project guidance based on the roadmap and rules.")
77
-
78
- # Display the roadmap and rules text
79
- st.subheader("Project Roadmap:")
80
- st.write(roadmap_content)
81
- st.subheader("Project Rules:")
82
- st.write(rules_content)
83
 
84
- # -----------------------------------
85
- # Create a chat history state
86
- # -----------------------------------
87
- if "history" not in st.session_state:
88
- st.session_state.history = []
89
 
90
- # -----------------------------------
91
- # Input and Chat Display
92
- # -----------------------------------
93
- user_input = st.text_input("You:", "")
94
 
 
95
  if user_input:
96
- st.session_state.history.append(("You", user_input))
97
-
98
- # Set system message (helps guide the conversation)
99
- system_message = "You are an AI assistant helping with project guidance based on a roadmap and rules."
100
-
101
- # Generate response
102
- ai_response = ""
103
- for resp in respond(user_input, st.session_state.history, system_message, temperature=0.7, top_p=0.95):
104
- ai_response = resp
105
-
106
- # Update chat history with AI response
107
- st.session_state.history.append(("AI", ai_response))
108
-
109
- # -----------------------------------
110
- # Display the chat history
111
- # -----------------------------------
112
- if st.session_state.history:
113
- for chat in st.session_state.history:
114
- if chat[0] == "You":
115
- st.write(f"**You**: {chat[1]}")
116
- else:
117
- st.write(f"**AI**: {chat[1]}")
 
1
  import os
 
2
  import streamlit as st
 
3
 
4
+ # Function to load files (roadmap and rules)
5
+ def load_file(file_name):
6
+ """Reads the content of a given text file."""
 
 
 
 
 
 
 
 
7
  try:
8
+ with open(file_name, 'r', encoding='utf-8') as file:
9
  return file.read()
10
  except FileNotFoundError:
11
+ return f"Error: {file_name} not found!"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
+ # Function to update memory files (optional feature)
14
+ def update_memory(file_name, new_content):
15
+ """Append new content to the given memory file (roadmap or rules)."""
16
  try:
17
+ with open(file_name, 'a', encoding='utf-8') as file:
18
+ file.write(new_content + "\n")
19
+ return f"Successfully updated {file_name}."
 
 
 
 
 
 
 
 
 
20
  except Exception as e:
21
+ return f"Error updating {file_name}: {e}"
22
 
23
+ # Load files into memory (roadmap and rules)
24
+ roadmap_content = load_file("roadmap.txt")
25
+ rules_content = load_file("rules.txt")
26
 
27
+ # Function to generate AI response based on user input
28
+ def generate_response(user_input):
29
+ # Check if the user asks about the roadmap
30
+ if "roadmap" in user_input.lower():
31
+ return roadmap_content
32
+ # Check if the user asks about the rules
33
+ elif "rules" in user_input.lower():
34
+ return rules_content
35
+ # Handle next step or task-based queries
36
+ elif "next step" in user_input.lower():
37
+ return "The next step is: [fetch from roadmap]"
38
+ # Handle update-related queries
39
+ elif "update" in user_input.lower():
40
+ return "Updating memory..." # Placeholder for functionality to update files
41
+ else:
42
+ return "I can help with the roadmap or rules. Just ask me!"
43
+
44
+ # Streamlit UI setup
45
  st.title("Project Guidance AI Chatbot")
 
 
 
 
 
 
 
46
 
47
+ # Displaying a brief description of how the chatbot works
48
+ st.write("""
49
+ This chatbot helps you navigate through the project using the **roadmap.txt** and **rules.txt** as its memory.
50
+ You can ask about the project steps, rules, or any guidance you need.
51
+ """)
52
 
53
+ # User input field for the chatbot
54
+ user_input = st.text_input("Ask me anything about the project:")
 
 
55
 
56
+ # Process user input and display the response from the AI
57
  if user_input:
58
+ response = generate_response(user_input)
59
+ st.write(f"**AI Response:** {response}")
60
+
61
+ # Optional: update the memory files if user wants to add something
62
+ if "add to roadmap" in user_input.lower():
63
+ new_content = user_input.replace("add to roadmap", "").strip()
64
+ update_response = update_memory("roadmap.txt", new_content)
65
+ st.write(update_response)
66
+ if "add to rules" in user_input.lower():
67
+ new_content = user_input.replace("add to rules", "").strip()
68
+ update_response = update_memory("rules.txt", new_content)
69
+ st.write(update_response)