Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
import logging
|
|
|
|
| 4 |
|
| 5 |
# Logging configuration
|
| 6 |
logging.basicConfig(level=logging.INFO,
|
|
@@ -455,7 +456,6 @@ def optimize_user_prompt_with_zephyr(user_idea, scene_info=None):
|
|
| 455 |
logger.info("Preparing prompt for optimization...")
|
| 456 |
|
| 457 |
# Detect language and adjust system prompt accordingly
|
| 458 |
-
import re
|
| 459 |
non_english_pattern = re.compile(r'[^\x00-\x7F]+')
|
| 460 |
has_non_english = bool(non_english_pattern.search(user_idea))
|
| 461 |
|
|
@@ -557,7 +557,6 @@ def refine_prompt_with_zephyr(current_prompt, feedback, chat_history, scene_info
|
|
| 557 |
|
| 558 |
try:
|
| 559 |
# Detect language of current prompt and feedback
|
| 560 |
-
import re
|
| 561 |
non_english_pattern = re.compile(r'[^\x00-\x7F]+')
|
| 562 |
has_non_english_prompt = bool(non_english_pattern.search(current_prompt))
|
| 563 |
has_non_english_feedback = bool(non_english_pattern.search(feedback))
|
|
@@ -727,6 +726,72 @@ def get_debug_info():
|
|
| 727 |
logger.error(f"Error generating debug info: {str(e)}")
|
| 728 |
return f"Error: {str(e)}"
|
| 729 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 730 |
# Create the Gradio interface
|
| 731 |
def create_interface():
|
| 732 |
"""Create the Gradio interface"""
|
|
@@ -748,6 +813,7 @@ def create_interface():
|
|
| 748 |
# State variables
|
| 749 |
scene_state = gr.State({})
|
| 750 |
chat_history_state = gr.State([])
|
|
|
|
| 751 |
|
| 752 |
with gr.Tabs():
|
| 753 |
# Tab 1: Learning Guide
|
|
@@ -793,57 +859,46 @@ def create_interface():
|
|
| 793 |
for i in range(3)
|
| 794 |
]
|
| 795 |
|
| 796 |
-
# Tab 3: AI Prompt Generator
|
| 797 |
with gr.Tab("🤖 AI Prompt Generator"):
|
| 798 |
with gr.Row():
|
| 799 |
-
|
| 800 |
-
|
| 801 |
-
|
| 802 |
-
|
| 803 |
-
|
| 804 |
-
|
| 805 |
-
|
| 806 |
-
|
| 807 |
-
|
| 808 |
-
|
| 809 |
-
|
| 810 |
-
|
| 811 |
-
|
| 812 |
-
|
| 813 |
-
|
| 814 |
-
|
| 815 |
-
|
| 816 |
-
|
| 817 |
-
|
| 818 |
-
|
| 819 |
-
|
| 820 |
-
|
| 821 |
-
|
| 822 |
-
|
| 823 |
-
|
| 824 |
-
|
| 825 |
-
|
| 826 |
-
|
| 827 |
-
|
| 828 |
-
|
| 829 |
-
|
| 830 |
-
|
| 831 |
-
|
| 832 |
-
|
| 833 |
-
|
| 834 |
-
|
| 835 |
-
|
| 836 |
-
chat_display = gr.Chatbot(height=300, type='messages')
|
| 837 |
-
|
| 838 |
-
# Model status and debug info
|
| 839 |
-
with gr.Accordion("🔧 Debug Info", open=False):
|
| 840 |
-
debug_info = gr.Textbox(
|
| 841 |
-
label="Debug Information",
|
| 842 |
-
value="Click 'Get Debug Info' to see model status",
|
| 843 |
-
lines=8,
|
| 844 |
-
interactive=False
|
| 845 |
-
)
|
| 846 |
-
debug_btn = gr.Button("Get Debug Info")
|
| 847 |
|
| 848 |
# Tab 4: Custom Builder
|
| 849 |
with gr.Tab("🛠️ Custom Builder"):
|
|
@@ -905,7 +960,7 @@ def create_interface():
|
|
| 905 |
show_copy_button=True
|
| 906 |
)
|
| 907 |
|
| 908 |
-
# Event handlers
|
| 909 |
analyze_btn.click(
|
| 910 |
fn=analyze_image_with_zephyr,
|
| 911 |
inputs=[image_input],
|
|
@@ -916,42 +971,86 @@ def create_interface():
|
|
| 916 |
inputs=[scene_state],
|
| 917 |
outputs=sample_prompts
|
| 918 |
)
|
| 919 |
-
|
| 920 |
-
|
| 921 |
-
|
| 922 |
-
|
|
|
|
|
|
|
| 923 |
)
|
| 924 |
-
|
| 925 |
-
|
| 926 |
-
|
| 927 |
-
|
|
|
|
|
|
|
| 928 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 929 |
test_btn.click(
|
| 930 |
fn=test_basic_generation,
|
| 931 |
inputs=[],
|
| 932 |
outputs=[test_output]
|
| 933 |
)
|
|
|
|
| 934 |
debug_btn.click(
|
| 935 |
fn=get_debug_info,
|
| 936 |
inputs=[],
|
| 937 |
outputs=[debug_info]
|
| 938 |
)
|
| 939 |
-
|
| 940 |
-
fn=refine_prompt_with_zephyr,
|
| 941 |
-
inputs=[optimized_prompt, feedback_input, chat_history_state, scene_state],
|
| 942 |
-
outputs=[optimized_prompt, chat_history_state]
|
| 943 |
-
)
|
| 944 |
# Update chat display when history changes
|
| 945 |
chat_history_state.change(
|
| 946 |
fn=lambda history: history,
|
| 947 |
inputs=[chat_history_state],
|
| 948 |
outputs=[chat_display]
|
| 949 |
)
|
|
|
|
|
|
|
| 950 |
build_custom_btn.click(
|
| 951 |
fn=build_custom_prompt,
|
| 952 |
inputs=[custom_foundation, subject_motion, scene_motion, camera_motion, style_motion],
|
| 953 |
outputs=[custom_output]
|
| 954 |
)
|
|
|
|
| 955 |
return demo
|
| 956 |
|
| 957 |
# Launch the app
|
|
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
import logging
|
| 4 |
+
import re
|
| 5 |
|
| 6 |
# Logging configuration
|
| 7 |
logging.basicConfig(level=logging.INFO,
|
|
|
|
| 456 |
logger.info("Preparing prompt for optimization...")
|
| 457 |
|
| 458 |
# Detect language and adjust system prompt accordingly
|
|
|
|
| 459 |
non_english_pattern = re.compile(r'[^\x00-\x7F]+')
|
| 460 |
has_non_english = bool(non_english_pattern.search(user_idea))
|
| 461 |
|
|
|
|
| 557 |
|
| 558 |
try:
|
| 559 |
# Detect language of current prompt and feedback
|
|
|
|
| 560 |
non_english_pattern = re.compile(r'[^\x00-\x7F]+')
|
| 561 |
has_non_english_prompt = bool(non_english_pattern.search(current_prompt))
|
| 562 |
has_non_english_feedback = bool(non_english_pattern.search(feedback))
|
|
|
|
| 726 |
logger.error(f"Error generating debug info: {str(e)}")
|
| 727 |
return f"Error: {str(e)}"
|
| 728 |
|
| 729 |
+
# Nueva función para el chat conversacional
|
| 730 |
+
def chat_with_ai(user_input, chat_history, last_prompt, scene_info=None):
|
| 731 |
+
"""Process user message for chat-based prompt creation or refinement"""
|
| 732 |
+
logger.info(f"Processing chat message: {user_input}")
|
| 733 |
+
|
| 734 |
+
if not user_input.strip():
|
| 735 |
+
return "", chat_history, last_prompt
|
| 736 |
+
|
| 737 |
+
try:
|
| 738 |
+
# Check if this is refinement of an existing prompt or a new prompt
|
| 739 |
+
is_refinement = False
|
| 740 |
+
|
| 741 |
+
if last_prompt:
|
| 742 |
+
# Check if input seems like feedback rather than a new prompt idea
|
| 743 |
+
# Common words used in feedback
|
| 744 |
+
feedback_keywords = ['more', 'less', 'add', 'change', 'make', 'remove',
|
| 745 |
+
'modify', 'update', 'adjust',
|
| 746 |
+
# Spanish equivalents
|
| 747 |
+
'más', 'menos', 'añadir', 'cambiar', 'hacer', 'quitar',
|
| 748 |
+
'modificar', 'actualizar', 'ajustar']
|
| 749 |
+
|
| 750 |
+
# Check for feedback keywords and short inputs which are likely feedback
|
| 751 |
+
first_word = user_input.strip().split(' ')[0].lower() if user_input.strip() else ""
|
| 752 |
+
if (first_word in feedback_keywords or
|
| 753 |
+
len(user_input.split()) <= 4 or
|
| 754 |
+
not user_input[0].isupper()):
|
| 755 |
+
is_refinement = True
|
| 756 |
+
|
| 757 |
+
# Detect language
|
| 758 |
+
non_english_pattern = re.compile(r'[^\x00-\x7F]+')
|
| 759 |
+
has_non_english = bool(non_english_pattern.search(user_input))
|
| 760 |
+
|
| 761 |
+
if is_refinement and last_prompt:
|
| 762 |
+
# Process as refinement to previous prompt
|
| 763 |
+
logger.info("Processing as refinement feedback")
|
| 764 |
+
refined_prompt, _ = refine_prompt_with_zephyr(last_prompt, user_input, [], scene_info)
|
| 765 |
+
|
| 766 |
+
# Prepare response based on language
|
| 767 |
+
if has_non_english:
|
| 768 |
+
response_text = f"Aquí está tu prompt refinado:\n\n**{refined_prompt}**\n\n¿Quieres hacer más cambios?"
|
| 769 |
+
else:
|
| 770 |
+
response_text = f"Here's your refined prompt:\n\n**{refined_prompt}**\n\nAny other changes you'd like to make?"
|
| 771 |
+
|
| 772 |
+
chat_history.append([user_input, response_text])
|
| 773 |
+
return "", chat_history, refined_prompt
|
| 774 |
+
|
| 775 |
+
else:
|
| 776 |
+
# Process as new prompt creation
|
| 777 |
+
logger.info("Processing as new prompt idea")
|
| 778 |
+
optimized, _ = optimize_user_prompt_with_zephyr(user_input, scene_info)
|
| 779 |
+
|
| 780 |
+
# Prepare response based on language
|
| 781 |
+
if has_non_english:
|
| 782 |
+
response_text = f"Aquí está tu prompt optimizado:\n\n**{optimized}**\n\nPuedes pedir cambios específicos o mejoras."
|
| 783 |
+
else:
|
| 784 |
+
response_text = f"Here's your optimized video prompt:\n\n**{optimized}**\n\nYou can ask for specific changes or improvements."
|
| 785 |
+
|
| 786 |
+
chat_history.append([user_input, response_text])
|
| 787 |
+
return "", chat_history, optimized
|
| 788 |
+
|
| 789 |
+
except Exception as e:
|
| 790 |
+
logger.error(f"Error in chat processing: {str(e)}")
|
| 791 |
+
error_message = f"Error processing your message: {str(e)}. Please try again with a simpler request."
|
| 792 |
+
chat_history.append([user_input, error_message])
|
| 793 |
+
return "", chat_history, last_prompt
|
| 794 |
+
|
| 795 |
# Create the Gradio interface
|
| 796 |
def create_interface():
|
| 797 |
"""Create the Gradio interface"""
|
|
|
|
| 813 |
# State variables
|
| 814 |
scene_state = gr.State({})
|
| 815 |
chat_history_state = gr.State([])
|
| 816 |
+
last_prompt_state = gr.State("")
|
| 817 |
|
| 818 |
with gr.Tabs():
|
| 819 |
# Tab 1: Learning Guide
|
|
|
|
| 859 |
for i in range(3)
|
| 860 |
]
|
| 861 |
|
| 862 |
+
# Tab 3: AI Prompt Generator (Conversational)
|
| 863 |
with gr.Tab("🤖 AI Prompt Generator"):
|
| 864 |
with gr.Row():
|
| 865 |
+
# Conversation history
|
| 866 |
+
chat_display = gr.Chatbot(
|
| 867 |
+
height=450,
|
| 868 |
+
show_copy_button=True,
|
| 869 |
+
label="AI Conversation"
|
| 870 |
+
)
|
| 871 |
+
|
| 872 |
+
with gr.Row():
|
| 873 |
+
# Message input
|
| 874 |
+
user_message = gr.Textbox(
|
| 875 |
+
placeholder="Type your video idea or feedback here (any language)...",
|
| 876 |
+
lines=2,
|
| 877 |
+
label="Your message"
|
| 878 |
+
)
|
| 879 |
+
send_btn = gr.Button("💬 Send", variant="primary")
|
| 880 |
+
|
| 881 |
+
with gr.Row():
|
| 882 |
+
# Action buttons
|
| 883 |
+
regenerate_btn = gr.Button("🔄 Regenerate")
|
| 884 |
+
clear_btn = gr.Button("🗑️ Clear Chat")
|
| 885 |
+
copy_last_btn = gr.Button("📋 Copy Last Prompt")
|
| 886 |
+
|
| 887 |
+
# Debug info in collapsible section
|
| 888 |
+
with gr.Accordion("🔧 Debug Info", open=False):
|
| 889 |
+
test_btn = gr.Button("🔬 Test Basic Generation", variant="secondary")
|
| 890 |
+
test_output = gr.Textbox(
|
| 891 |
+
label="Basic Generation Test",
|
| 892 |
+
lines=2,
|
| 893 |
+
interactive=False
|
| 894 |
+
)
|
| 895 |
+
debug_btn = gr.Button("Get Debug Info")
|
| 896 |
+
debug_info = gr.Textbox(
|
| 897 |
+
label="Debug Information",
|
| 898 |
+
value="Click 'Get Debug Info' to see model status",
|
| 899 |
+
lines=8,
|
| 900 |
+
interactive=False
|
| 901 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 902 |
|
| 903 |
# Tab 4: Custom Builder
|
| 904 |
with gr.Tab("🛠️ Custom Builder"):
|
|
|
|
| 960 |
show_copy_button=True
|
| 961 |
)
|
| 962 |
|
| 963 |
+
# Event handlers for Image Analysis tab
|
| 964 |
analyze_btn.click(
|
| 965 |
fn=analyze_image_with_zephyr,
|
| 966 |
inputs=[image_input],
|
|
|
|
| 971 |
inputs=[scene_state],
|
| 972 |
outputs=sample_prompts
|
| 973 |
)
|
| 974 |
+
|
| 975 |
+
# Event handlers for AI Prompt Generator tab (Chat)
|
| 976 |
+
send_btn.click(
|
| 977 |
+
fn=chat_with_ai,
|
| 978 |
+
inputs=[user_message, chat_history_state, last_prompt_state, scene_state],
|
| 979 |
+
outputs=[user_message, chat_history_state, last_prompt_state]
|
| 980 |
)
|
| 981 |
+
|
| 982 |
+
# Also trigger on Enter key
|
| 983 |
+
user_message.submit(
|
| 984 |
+
fn=chat_with_ai,
|
| 985 |
+
inputs=[user_message, chat_history_state, last_prompt_state, scene_state],
|
| 986 |
+
outputs=[user_message, chat_history_state, last_prompt_state]
|
| 987 |
)
|
| 988 |
+
|
| 989 |
+
# Function to regenerate last prompt
|
| 990 |
+
def regenerate_last_prompt(chat_history, scene_info):
|
| 991 |
+
if not chat_history:
|
| 992 |
+
return chat_history, ""
|
| 993 |
+
|
| 994 |
+
# Get the last user message
|
| 995 |
+
last_user_msg = chat_history[-1][0]
|
| 996 |
+
new_prompt, _ = optimize_user_prompt_with_zephyr(last_user_msg, scene_info)
|
| 997 |
+
|
| 998 |
+
# Add regenerated response to chat
|
| 999 |
+
chat_history.append(["🔄 Regenerate", f"Here's an alternative version:\n\n**{new_prompt}**"])
|
| 1000 |
+
return chat_history, new_prompt
|
| 1001 |
+
|
| 1002 |
+
regenerate_btn.click(
|
| 1003 |
+
fn=regenerate_last_prompt,
|
| 1004 |
+
inputs=[chat_history_state, scene_state],
|
| 1005 |
+
outputs=[chat_history_state, last_prompt_state]
|
| 1006 |
+
)
|
| 1007 |
+
|
| 1008 |
+
# Clear chat button
|
| 1009 |
+
clear_btn.click(
|
| 1010 |
+
fn=lambda: ([], ""),
|
| 1011 |
+
inputs=[],
|
| 1012 |
+
outputs=[chat_history_state, last_prompt_state]
|
| 1013 |
+
)
|
| 1014 |
+
|
| 1015 |
+
# Copy last prompt button
|
| 1016 |
+
def copy_last_prompt(last_prompt):
|
| 1017 |
+
if last_prompt:
|
| 1018 |
+
return gr.update(value=f"Copied: {last_prompt[:20]}...")
|
| 1019 |
+
return gr.update(value="No prompt to copy")
|
| 1020 |
+
|
| 1021 |
+
copy_last_btn.click(
|
| 1022 |
+
fn=copy_last_prompt,
|
| 1023 |
+
inputs=[last_prompt_state],
|
| 1024 |
+
outputs=[test_output]
|
| 1025 |
+
)
|
| 1026 |
+
|
| 1027 |
+
# Diagnostic buttons
|
| 1028 |
test_btn.click(
|
| 1029 |
fn=test_basic_generation,
|
| 1030 |
inputs=[],
|
| 1031 |
outputs=[test_output]
|
| 1032 |
)
|
| 1033 |
+
|
| 1034 |
debug_btn.click(
|
| 1035 |
fn=get_debug_info,
|
| 1036 |
inputs=[],
|
| 1037 |
outputs=[debug_info]
|
| 1038 |
)
|
| 1039 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1040 |
# Update chat display when history changes
|
| 1041 |
chat_history_state.change(
|
| 1042 |
fn=lambda history: history,
|
| 1043 |
inputs=[chat_history_state],
|
| 1044 |
outputs=[chat_display]
|
| 1045 |
)
|
| 1046 |
+
|
| 1047 |
+
# Event handlers for Custom Builder tab
|
| 1048 |
build_custom_btn.click(
|
| 1049 |
fn=build_custom_prompt,
|
| 1050 |
inputs=[custom_foundation, subject_motion, scene_motion, camera_motion, style_motion],
|
| 1051 |
outputs=[custom_output]
|
| 1052 |
)
|
| 1053 |
+
|
| 1054 |
return demo
|
| 1055 |
|
| 1056 |
# Launch the app
|