acecalisto3 commited on
Commit
38e3e8b
1 Parent(s): 2c323dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +194 -58
app.py CHANGED
@@ -972,71 +972,207 @@ class GradioInterface:
972
  generate_preview,
973
  outputs=[preview_status]
974
  )
975
-
976
- def _create_ai_tab(self):
977
- """Create the AI assistant tab"""
978
- with gr.Row():
979
- with gr.Column(scale=2):
980
- chat_history = gr.Chatbot(label="Chat History")
981
- user_input = gr.Textbox(
982
- label="Your Instructions",
983
- placeholder="Describe what you want to build or modify...",
984
- lines=3
985
- )
986
- with gr.Row():
987
- send_btn = gr.Button("Send", variant="primary")
988
- clear_btn = gr.Button("Clear Chat")
989
 
990
- with gr.Column(scale=1):
991
- suggestions = gr.JSON(label="Suggested Components")
992
- preview_btn = gr.Button("Update Preview")
993
- preview_frame = gr.HTML(label="Live Preview")
994
-
995
- chat_error = gr.Markdown(visible=False)
996
-
997
- async def process_chat(message, history):
998
- if not self.app_builder.ai_builder:
999
- self.app_builder.ai_builder = GradioAIBuilder()
 
 
1000
 
1001
- try:
1002
- result = await self.app_builder.ai_builder.process_user_request(message)
1003
-
1004
- if result["status"] == "error":
1005
- return (
1006
- history + [[message, "Error: " + result["response"]]],
1007
- [],
1008
- gr.Markdown(f"⚠️ {result['error']}", visible=True)
1009
- )
1010
-
1011
- new_history = history + [[message, result["response"]]]
1012
-
1013
- return new_history, [], gr.Markdown(visible=False)
 
 
 
 
 
 
1014
 
1015
- except Exception as e:
1016
- error_msg = f"Error processing chat: {str(e)}"
1017
- logger.error(error_msg)
1018
- return (
1019
- history + [[message, "An error occurred"]],
1020
- [],
1021
- gr.Markdown(f"⚠️ {error_msg}", visible=True)
1022
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1023
 
1024
- def clear_chat_history():
1025
- if self.app_builder.ai_builder:
1026
- self.app_builder.ai_builder.chat_history = []
1027
- return [], [], gr.Markdown(visible=False)
1028
 
1029
- send_btn.click(
1030
- process_chat,
1031
- inputs=[user_input, chat_history],
1032
- outputs=[chat_history, suggestions, chat_error]
1033
- )
1034
-
1035
- clear_btn.click(
1036
- clear_chat_history,
1037
- outputs=[chat_history, suggestions, chat_error]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1038
  )
1039
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1040
  def _create_settings_tab(self):
1041
  """Create the settings tab"""
1042
  with gr.Row():
 
972
  generate_preview,
973
  outputs=[preview_status]
974
  )
975
+ def _create_ai_tab(self):
976
+ """Create AI assistant tab with streamlined yes/no interaction"""
977
+ with gr.Row():
978
+ with gr.Column(scale=2):
979
+ gr.Markdown("""### 🤖 AI Assistant - Quick Build
980
+ Start by selecting your main task, then answer with Yes/No to refine the solution.
 
 
 
 
 
 
 
 
981
 
982
+ Available Commands:
983
+ 1. create - New app
984
+ 2. component - Add/modify components
985
+ 3. layout - Change layout
986
+ 4. style - Customize appearance
987
+ 5. data - Data processing
988
+ 6. api - API integration
989
+ 7. auth - Authentication
990
+ 8. file - File operations
991
+ 9. viz - Visualization
992
+ 10. db - Database
993
+ """)
994
 
995
+ chat_history = gr.Chatbot(label="Interaction History")
996
+ user_input = gr.Radio(
997
+ choices=["Yes", "No"],
998
+ label="Your Response",
999
+ visible=False
1000
+ )
1001
+ command_input = gr.Dropdown(
1002
+ choices=["create", "component", "layout", "style", "data", "api", "auth", "file", "viz", "db"],
1003
+ label="Select Command to Start",
1004
+ )
1005
+ with gr.Row():
1006
+ send_btn = gr.Button("Send", variant="primary")
1007
+ restart_btn = gr.Button("Restart", variant="secondary")
1008
+
1009
+ class ChatState:
1010
+ def __init__(self):
1011
+ self.current_step = 0
1012
+ self.command = None
1013
+ self.context = {}
1014
 
1015
+ def reset(self):
1016
+ self.current_step = 0
1017
+ self.command = None
1018
+ self.context = {}
1019
+
1020
+ chat_state = ChatState()
1021
+
1022
+ def get_next_question(command, step, previous_answer=None):
1023
+ """Generate next question based on command and current step"""
1024
+ questions = {
1025
+ "create": [
1026
+ "Do you need user input components?",
1027
+ "Do you need data processing functionality?",
1028
+ "Would you like to add styling/themes?"
1029
+ ],
1030
+ "component": [
1031
+ "Is this for user input?",
1032
+ "Do you need media handling (image/audio/video)?",
1033
+ "Should the component have real-time updates?"
1034
+ ],
1035
+ "layout": [
1036
+ "Do you want a multi-tab layout?",
1037
+ "Do you need responsive design?",
1038
+ "Should components be arranged horizontally?"
1039
+ ],
1040
+ "style": [
1041
+ "Do you want a dark theme?",
1042
+ "Do you need custom CSS?",
1043
+ "Should components have rounded corners?"
1044
+ ],
1045
+ "data": [
1046
+ "Will you be handling file uploads?",
1047
+ "Do you need data visualization?",
1048
+ "Should data be stored persistently?"
1049
+ ],
1050
+ "api": [
1051
+ "Do you need authentication for API?",
1052
+ "Will you be handling JSON data?",
1053
+ "Do you need error handling?"
1054
+ ],
1055
+ "auth": [
1056
+ "Do you need user registration?",
1057
+ "Should sessions be persistent?",
1058
+ "Do you need role-based access?"
1059
+ ],
1060
+ "file": [
1061
+ "Will you handle multiple file types?",
1062
+ "Do you need file preprocessing?",
1063
+ "Should files be stored locally?"
1064
+ ],
1065
+ "viz": [
1066
+ "Do you need interactive plots?",
1067
+ "Will you use real-time data?",
1068
+ "Do you need multiple chart types?"
1069
+ ],
1070
+ "db": [
1071
+ "Do you need real-time updates?",
1072
+ "Will you use SQL database?",
1073
+ "Do you need data caching?"
1074
+ ]
1075
+ }
1076
+
1077
+ if step < len(questions[command]):
1078
+ return questions[command][step]
1079
+ return None
1080
+
1081
+ def generate_code(command, context):
1082
+ """Generate code based on collected context"""
1083
+ base_templates = {
1084
+ "create": """
1085
+ ```python
1086
+ import gradio as gr
1087
 
1088
+ def process_input({input_params}):
1089
+ {processing_logic}
1090
+ return result
 
1091
 
1092
+ with gr.Blocks({style_params}) as demo:
1093
+ {components}
1094
+ {layout}
1095
+ {event_handlers}
1096
+
1097
+ demo.launch()
1098
+ ```""",
1099
+ "component": """
1100
+ ```python
1101
+ {import_statements}
1102
+
1103
+ {component_definition}
1104
+
1105
+ {event_handling}
1106
+ ```""",
1107
+ # Add other base templates for each command...
1108
+ }
1109
+
1110
+ # Customize template based on context
1111
+ if command == "create":
1112
+ input_params = "user_input"
1113
+ processing_logic = "result = user_input"
1114
+ style_params = "theme=gr.themes.Default()" if not context.get("dark_theme") else "theme=gr.themes.Monochrome()"
1115
+ components = "input_component = gr.Textbox(label='Input')\noutput_component = gr.Textbox(label='Output')"
1116
+ layout = "gr.Button('Process')"
1117
+ event_handlers = "input_component.change(process_input, inputs=input_component, outputs=output_component)"
1118
+
1119
+ return base_templates[command].format(
1120
+ input_params=input_params,
1121
+ processing_logic=processing_logic,
1122
+ style_params=style_params,
1123
+ components=components,
1124
+ layout=layout,
1125
+ event_handlers=event_handlers
1126
  )
1127
 
1128
+ def process_chat(command, answer, history):
1129
+ """Process chat interaction"""
1130
+ try:
1131
+ if command and chat_state.command != command:
1132
+ # New command started
1133
+ chat_state.reset()
1134
+ chat_state.command = command
1135
+ question = get_next_question(command, 0)
1136
+ return history + [[None, question]], True, question
1137
+
1138
+ if answer:
1139
+ # Store answer and get next question
1140
+ chat_state.context[f"step_{chat_state.current_step}"] = answer == "Yes"
1141
+ chat_state.current_step += 1
1142
+
1143
+ next_question = get_next_question(chat_state.command, chat_state.current_step)
1144
+
1145
+ if next_question:
1146
+ # More questions to ask
1147
+ return history + [[answer, next_question]], True, next_question
1148
+ else:
1149
+ # Generate final code
1150
+ code = generate_code(chat_state.command, chat_state.context)
1151
+ return history + [[answer, f"Here's your code:\n{code}"]], False, None
1152
+
1153
+ return history, True, None
1154
+
1155
+ except Exception as e:
1156
+ error_msg = f"Error: {str(e)}"
1157
+ logger.error(error_msg)
1158
+ return history + [[None, error_msg]], False, None
1159
+
1160
+ def reset_chat():
1161
+ """Reset chat state and history"""
1162
+ chat_state.reset()
1163
+ return None, None, False, None
1164
+
1165
+ # Set up event handlers
1166
+ send_btn.click(
1167
+ process_chat,
1168
+ inputs=[command_input, user_input, chat_history],
1169
+ outputs=[chat_history, user_input, user_input]
1170
+ )
1171
+
1172
+ restart_btn.click(
1173
+ reset_chat,
1174
+ outputs=[chat_history, command_input, user_input, user_input]
1175
+ )
1176
  def _create_settings_tab(self):
1177
  """Create the settings tab"""
1178
  with gr.Row():