shikata4622 commited on
Commit
2031b4c
·
verified ·
1 Parent(s): 5ee472d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -87
app.py CHANGED
@@ -1,61 +1,3 @@
1
- import gradio as gr
2
- import os
3
- import yaml
4
-
5
- # Function to update the NLU file with new intents
6
- def update_nlu_file(intent_name, training_examples, nlu_file_path="data/nlu.yml"):
7
- # Create the 'data' directory if it doesn't exist
8
- if not os.path.exists("data"):
9
- os.makedirs("data")
10
-
11
- # If the NLU file doesn't exist, create it with a default structure
12
- if os.path.exists(nlu_file_path):
13
- with open(nlu_file_path, "r") as file:
14
- nlu_data = yaml.safe_load(file)
15
- else:
16
- nlu_data = {"rasa_nlu_data": {"common_examples": []}}
17
-
18
- # Add the new intent and examples
19
- new_intent = {
20
- "intent": intent_name,
21
- "examples": training_examples.splitlines()
22
- }
23
-
24
- nlu_data["rasa_nlu_data"]["common_examples"].append(new_intent)
25
-
26
- # Write back the updated NLU file
27
- with open(nlu_file_path, "w") as file:
28
- yaml.dump(nlu_data, file)
29
-
30
- return f"NLU file updated with intent: {intent_name}"
31
-
32
- # Function to update the Story file with new stories
33
- def update_stories_file(story_name, steps, stories_file_path="data/stories.yml"):
34
- # Create the 'data' directory if it doesn't exist
35
- if not os.path.exists("data"):
36
- os.makedirs("data")
37
-
38
- # If the Stories file doesn't exist, create it with a default structure
39
- if os.path.exists(stories_file_path):
40
- with open(stories_file_path, "r") as file:
41
- stories_data = yaml.safe_load(file)
42
- else:
43
- stories_data = {"stories": []}
44
-
45
- # Add the new story
46
- new_story = {
47
- "story": story_name,
48
- "steps": [{"intent": step} for step in steps.splitlines()]
49
- }
50
-
51
- stories_data["stories"].append(new_story)
52
-
53
- # Write back the updated Stories file
54
- with open(stories_file_path, "w") as file:
55
- yaml.dump(stories_data, file)
56
-
57
- return f"Stories file updated with story: {story_name}"
58
-
59
  # Function to update the Domain file with new intents and responses
60
  def update_domain_file(intent_name, domain_file_path="domain.yml"):
61
  # Create the 'data' directory if it doesn't exist
@@ -66,6 +8,9 @@ def update_domain_file(intent_name, domain_file_path="domain.yml"):
66
  if os.path.exists(domain_file_path):
67
  with open(domain_file_path, "r") as file:
68
  domain_data = yaml.safe_load(file)
 
 
 
69
  else:
70
  domain_data = {"intents": [], "responses": {}}
71
 
@@ -84,32 +29,3 @@ def update_domain_file(intent_name, domain_file_path="domain.yml"):
84
  yaml.dump(domain_data, file)
85
 
86
  return f"Domain file updated with intent: {intent_name}"
87
-
88
- # Gradio interface function
89
- def rasa_creator(intent_name, training_examples, story_name, steps, add_button):
90
- if add_button:
91
- nlu_response = update_nlu_file(intent_name, training_examples)
92
- stories_response = update_stories_file(story_name, steps)
93
- domain_response = update_domain_file(intent_name)
94
- return nlu_response, stories_response, domain_response
95
- return "Press Add to update files."
96
-
97
- # Gradio interface
98
- iface = gr.Interface(
99
- fn=rasa_creator,
100
- inputs=[
101
- gr.Textbox(label="Intent Name"),
102
- gr.Textbox(label="Training Examples", placeholder="Add your training examples here", lines=4),
103
- gr.Textbox(label="Story Name"),
104
- gr.Textbox(label="Story Steps", placeholder="Add your story steps here", lines=4),
105
- gr.Button("Add") # Button to trigger the file update
106
- ],
107
- outputs=[
108
- gr.Textbox(label="NLU Update Status"),
109
- gr.Textbox(label="Stories Update Status"),
110
- gr.Textbox(label="Domain Update Status")
111
- ],
112
- live=False, # Disable live mode to prevent auto-updating
113
- )
114
-
115
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # Function to update the Domain file with new intents and responses
2
  def update_domain_file(intent_name, domain_file_path="domain.yml"):
3
  # Create the 'data' directory if it doesn't exist
 
8
  if os.path.exists(domain_file_path):
9
  with open(domain_file_path, "r") as file:
10
  domain_data = yaml.safe_load(file)
11
+ # If domain_data is None, initialize it with a default structure
12
+ if domain_data is None:
13
+ domain_data = {"intents": [], "responses": {}}
14
  else:
15
  domain_data = {"intents": [], "responses": {}}
16
 
 
29
  yaml.dump(domain_data, file)
30
 
31
  return f"Domain file updated with intent: {intent_name}"