Spaces:
Sleeping
Sleeping
jjz5463
commited on
Commit
·
df9bd09
1
Parent(s):
ceea560
initial commit
Browse files- README.md +2 -2
- app.py +93 -76
- chatbot_simulator.py +3 -3
README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
---
|
| 2 |
title: App Simulator
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
colorTo: indigo
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 4.44.1
|
|
|
|
| 1 |
---
|
| 2 |
title: App Simulator
|
| 3 |
+
emoji: 🎮
|
| 4 |
+
colorFrom: purple
|
| 5 |
colorTo: indigo
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 4.44.1
|
app.py
CHANGED
|
@@ -2,84 +2,103 @@ import gradio as gr
|
|
| 2 |
from chatbot_simulator import ChatbotSimulation
|
| 3 |
from task_specific_data_population import DataPopulation
|
| 4 |
import os
|
|
|
|
| 5 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 6 |
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
max_retries = 50 # Set the maximum number of retries
|
| 17 |
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
try:
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
global simulation
|
| 25 |
-
simulation = ChatbotSimulation(
|
| 26 |
-
site_map=sitemap_data,
|
| 27 |
-
page_details=page_details,
|
| 28 |
-
user_state=user_state,
|
| 29 |
-
task=task,
|
| 30 |
-
app_name=app_name,
|
| 31 |
-
log_location=f'conversation_log_{app_name}_human.txt',
|
| 32 |
-
openai_api_key=openai_api_key,
|
| 33 |
-
agent='human'
|
| 34 |
-
)
|
| 35 |
-
text = simulation.start_conversation()
|
| 36 |
-
global conversation
|
| 37 |
-
conversation.append({"role": "assistant", "content": text})
|
| 38 |
-
log_conversation(simulation.log_location)
|
| 39 |
-
|
| 40 |
-
global display_conversation
|
| 41 |
-
display_conversation.append(('Start Simulator', text))
|
| 42 |
-
if len(display_conversation) > 2:
|
| 43 |
-
display_conversation.pop(0)
|
| 44 |
-
return display_conversation
|
| 45 |
except Exception as e:
|
| 46 |
-
|
| 47 |
-
retry_count += 1
|
| 48 |
-
print(f"Attempt {retry_count}/{max_retries}: An error occurred: {e}. Retrying...")
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
def log_conversation(log_location):
|
| 52 |
-
"""
|
| 53 |
-
Append the conversation to the specified log file location.
|
| 54 |
-
"""
|
| 55 |
-
try:
|
| 56 |
-
with open(log_location, 'a') as f: # Use 'a' for append mode
|
| 57 |
-
for message in conversation:
|
| 58 |
-
f.write(f"{message['role']}: {message['content']}\n\n")
|
| 59 |
-
except Exception as e:
|
| 60 |
-
print(f"Error logging conversation: {e}")
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
def chatbot_interaction(user_input):
|
| 64 |
-
"""Handle the conversation."""
|
| 65 |
-
if simulation is None:
|
| 66 |
-
return "Simulation is not initialized. Please start the simulator."
|
| 67 |
-
|
| 68 |
-
try:
|
| 69 |
-
# Perform one round of conversation
|
| 70 |
-
response = simulation.one_conversation_round(user_input)
|
| 71 |
-
global conversation
|
| 72 |
-
conversation.append({"role": "user", "content": user_input})
|
| 73 |
-
conversation.append({"role": "assistant", "content": response})
|
| 74 |
-
log_conversation(simulation.log_location)
|
| 75 |
-
display_conversation.append((user_input, response))
|
| 76 |
-
return display_conversation
|
| 77 |
-
except Exception as e:
|
| 78 |
-
return f"An error occurred: {e}"
|
| 79 |
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
# Gradio Interface
|
| 82 |
-
with gr.Blocks() as demo:
|
| 83 |
gr.Markdown("## Simulator Setup")
|
| 84 |
|
| 85 |
task_input = gr.Textbox(label="Task", placeholder="Describe your task...")
|
|
@@ -87,25 +106,23 @@ with gr.Blocks() as demo:
|
|
| 87 |
sitemap_input = gr.Textbox(label="Sitemap", placeholder="Enter the Hugging Face link to sitemap...")
|
| 88 |
|
| 89 |
initialize_button = gr.Button("Initialize Simulator")
|
| 90 |
-
#setup_output = gr.Textbox(label="Setup Status", interactive=False)
|
| 91 |
-
|
| 92 |
chatbot = gr.Chatbot(label="Simulator Chat", height=800)
|
| 93 |
user_message = gr.Textbox(label="Enter your message", placeholder="Type your message here...")
|
| 94 |
submit_button = gr.Button("Send")
|
| 95 |
|
| 96 |
-
# Initialize simulator and display the welcome message
|
| 97 |
initialize_button.click(
|
| 98 |
-
initialize_simulator,
|
| 99 |
inputs=[task_input, app_name_input, sitemap_input],
|
| 100 |
-
outputs=chatbot
|
| 101 |
)
|
| 102 |
|
| 103 |
-
# Handle conversation
|
| 104 |
submit_button.click(
|
| 105 |
-
chatbot_interaction,
|
| 106 |
inputs=user_message,
|
| 107 |
outputs=chatbot
|
| 108 |
)
|
| 109 |
|
| 110 |
-
# Launch the app
|
| 111 |
demo.launch()
|
|
|
|
| 2 |
from chatbot_simulator import ChatbotSimulation
|
| 3 |
from task_specific_data_population import DataPopulation
|
| 4 |
import os
|
| 5 |
+
|
| 6 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 7 |
|
| 8 |
|
| 9 |
+
class AppSimulator:
|
| 10 |
+
def __init__(self, openai_api_key):
|
| 11 |
+
self.simulation = None
|
| 12 |
+
self.conversation = [] # Stores full conversation for logging
|
| 13 |
+
self.display_conversation = [] # Stores last 2 messages for display
|
| 14 |
+
self.openai_api_key = openai_api_key
|
| 15 |
+
|
| 16 |
+
def initialize_simulator(self, task, app_name, sitemap):
|
| 17 |
+
"""Initialize the simulator with retries in case of failure."""
|
| 18 |
+
success = False
|
| 19 |
+
retry_count = 0
|
| 20 |
+
max_retries = 50
|
| 21 |
+
|
| 22 |
+
while not success and retry_count < max_retries:
|
| 23 |
+
try:
|
| 24 |
+
# Process data
|
| 25 |
+
data_population = DataPopulation(api_key=self.openai_api_key)
|
| 26 |
+
sitemap_data, page_details, user_state = data_population.process_data(task, sitemap)
|
| 27 |
+
|
| 28 |
+
self.simulation = ChatbotSimulation(
|
| 29 |
+
site_map=sitemap_data,
|
| 30 |
+
page_details=page_details,
|
| 31 |
+
user_state=user_state,
|
| 32 |
+
task=task,
|
| 33 |
+
app_name=app_name,
|
| 34 |
+
log_location=f'conversation_log_{app_name}_human.txt',
|
| 35 |
+
openai_api_key=self.openai_api_key,
|
| 36 |
+
agent='human'
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# Start the conversation and update the logs and display
|
| 40 |
+
text = self.simulation.start_conversation()
|
| 41 |
+
self._log_message("assistant", text)
|
| 42 |
+
self.display_conversation = [('Start Simulator', text)]
|
| 43 |
+
|
| 44 |
+
return self.display_conversation
|
| 45 |
+
except Exception as e:
|
| 46 |
+
retry_count += 1
|
| 47 |
+
print(f"Attempt {retry_count}/{max_retries}: An error occurred: {e}. Retrying...")
|
| 48 |
+
|
| 49 |
+
def chatbot_interaction(self, user_input):
|
| 50 |
+
"""Handle one round of conversation."""
|
| 51 |
+
if self.simulation is None:
|
| 52 |
+
return [("system", "Simulation is not initialized. Please start the simulator.")]
|
| 53 |
+
|
| 54 |
+
try:
|
| 55 |
+
# Get the response from the simulator
|
| 56 |
+
response = self.simulation.one_conversation_round(user_input)
|
| 57 |
+
|
| 58 |
+
# Log both user input and assistant's response
|
| 59 |
+
self._log_message("user", user_input)
|
| 60 |
+
self._log_message("assistant", response)
|
| 61 |
+
|
| 62 |
+
# Update display conversation (keep last 2 entries)
|
| 63 |
+
self.display_conversation.extend([(user_input, response)])
|
| 64 |
+
while len(self.display_conversation) > 2:
|
| 65 |
+
self.display_conversation.pop(0)
|
| 66 |
+
print(len(self.display_conversation))
|
| 67 |
+
return self.display_conversation
|
| 68 |
+
except Exception as e:
|
| 69 |
+
return [("system", f"An error occurred: {e}")]
|
| 70 |
|
| 71 |
+
def _log_message(self, role, content):
|
| 72 |
+
"""Log conversation messages to both memory and file."""
|
| 73 |
+
self.conversation.append({"role": role, "content": content})
|
| 74 |
+
self._write_log_to_file(content)
|
|
|
|
| 75 |
|
| 76 |
+
def _write_log_to_file(self, content):
|
| 77 |
+
"""Append conversation to a log file."""
|
| 78 |
+
log_location = self.simulation.log_location if self.simulation else "conversation_log.txt"
|
| 79 |
try:
|
| 80 |
+
with open(log_location, 'a') as f:
|
| 81 |
+
for message in self.conversation:
|
| 82 |
+
f.write(f"{message['role']}: {message['content']}\n\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
except Exception as e:
|
| 84 |
+
print(f"Error logging conversation: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
|
| 87 |
+
# JavaScript function to refresh theme to dark mode
|
| 88 |
+
js_func = """
|
| 89 |
+
function refresh() {
|
| 90 |
+
const url = new URL(window.location);
|
| 91 |
+
if (url.searchParams.get('__theme') !== 'dark') {
|
| 92 |
+
url.searchParams.set('__theme', 'dark');
|
| 93 |
+
window.location.href = url.href;
|
| 94 |
+
}
|
| 95 |
+
}
|
| 96 |
+
"""
|
| 97 |
+
|
| 98 |
+
simulator_app = AppSimulator(openai_api_key=openai_api_key)
|
| 99 |
+
|
| 100 |
# Gradio Interface
|
| 101 |
+
with gr.Blocks(js=js_func) as demo:
|
| 102 |
gr.Markdown("## Simulator Setup")
|
| 103 |
|
| 104 |
task_input = gr.Textbox(label="Task", placeholder="Describe your task...")
|
|
|
|
| 106 |
sitemap_input = gr.Textbox(label="Sitemap", placeholder="Enter the Hugging Face link to sitemap...")
|
| 107 |
|
| 108 |
initialize_button = gr.Button("Initialize Simulator")
|
|
|
|
|
|
|
| 109 |
chatbot = gr.Chatbot(label="Simulator Chat", height=800)
|
| 110 |
user_message = gr.Textbox(label="Enter your message", placeholder="Type your message here...")
|
| 111 |
submit_button = gr.Button("Send")
|
| 112 |
|
| 113 |
+
# Initialize simulator and display the welcome message
|
| 114 |
initialize_button.click(
|
| 115 |
+
simulator_app.initialize_simulator,
|
| 116 |
inputs=[task_input, app_name_input, sitemap_input],
|
| 117 |
+
outputs=chatbot
|
| 118 |
)
|
| 119 |
|
| 120 |
+
# Handle conversation interaction
|
| 121 |
submit_button.click(
|
| 122 |
+
simulator_app.chatbot_interaction,
|
| 123 |
inputs=user_message,
|
| 124 |
outputs=chatbot
|
| 125 |
)
|
| 126 |
|
| 127 |
+
# Launch the Gradio app
|
| 128 |
demo.launch()
|
chatbot_simulator.py
CHANGED
|
@@ -5,7 +5,7 @@ import json_repair
|
|
| 5 |
class ChatbotSimulation:
|
| 6 |
def __init__(self, site_map, page_details, user_state, task,
|
| 7 |
app_name, log_location, openai_api_key, agent='human',
|
| 8 |
-
max_steps=50, max_tokens=8192, buffer_tokens=
|
| 9 |
self.sitemap = site_map
|
| 10 |
self.page_details = page_details
|
| 11 |
self.user_state = user_state
|
|
@@ -73,7 +73,7 @@ Rules:
|
|
| 73 |
response = self.client.chat.completions.create(
|
| 74 |
model="gpt-4",
|
| 75 |
messages=prompt,
|
| 76 |
-
max_tokens=
|
| 77 |
temperature=1.0,
|
| 78 |
)
|
| 79 |
return response.choices[0].message.content
|
|
@@ -84,7 +84,7 @@ Rules:
|
|
| 84 |
|
| 85 |
def _trim_conversation(self):
|
| 86 |
"""Trim the conversation to keep it within the token limit."""
|
| 87 |
-
while self._calculate_token_count(self.conversation) > self.max_tokens - self.buffer_tokens:
|
| 88 |
self.conversation.pop(0) #
|
| 89 |
|
| 90 |
def one_conversation_round(self, user_input):
|
|
|
|
| 5 |
class ChatbotSimulation:
|
| 6 |
def __init__(self, site_map, page_details, user_state, task,
|
| 7 |
app_name, log_location, openai_api_key, agent='human',
|
| 8 |
+
max_steps=50, max_tokens=8192, buffer_tokens=1000):
|
| 9 |
self.sitemap = site_map
|
| 10 |
self.page_details = page_details
|
| 11 |
self.user_state = user_state
|
|
|
|
| 73 |
response = self.client.chat.completions.create(
|
| 74 |
model="gpt-4",
|
| 75 |
messages=prompt,
|
| 76 |
+
max_tokens=self.buffer_tokens, # Adjusted max_tokens if needed
|
| 77 |
temperature=1.0,
|
| 78 |
)
|
| 79 |
return response.choices[0].message.content
|
|
|
|
| 84 |
|
| 85 |
def _trim_conversation(self):
|
| 86 |
"""Trim the conversation to keep it within the token limit."""
|
| 87 |
+
while self._calculate_token_count(self.conversation) > self.max_tokens - (self.buffer_tokens+500):
|
| 88 |
self.conversation.pop(0) #
|
| 89 |
|
| 90 |
def one_conversation_round(self, user_input):
|