Spaces:
Sleeping
Sleeping
| from io import StringIO | |
| import gradio as gr | |
| import pandas as pd | |
| import requests | |
| # Sample data structure - replace this with your actual data loading logic | |
| sample_data = [ | |
| { | |
| "pattern": "greeting", | |
| "script": "Hello! How can I help you today?", | |
| "pattern_idx": 1, | |
| }, | |
| { | |
| "pattern": "goodbye", | |
| "script": "Thank you for your time. Have a great day!", | |
| "pattern_idx": 2, | |
| }, | |
| { | |
| "pattern": "question", | |
| "script": "That's an interesting question. Let me think about it.", | |
| "pattern_idx": 3, | |
| }, | |
| { | |
| "pattern": "complaint", | |
| "script": "I understand your concern. Let me see how I can help resolve this.", | |
| "pattern_idx": 4, | |
| }, | |
| { | |
| "pattern": "compliment", | |
| "script": "Thank you so much! I really appreciate your kind words.", | |
| "pattern_idx": 5, | |
| }, | |
| ] | |
| def load_data_from_sheets(): | |
| """ | |
| Load data from Google Sheets. | |
| Note: The provided URL requires authentication and cannot be accessed directly. | |
| You'll need to either: | |
| 1. Make the sheet public and use the CSV export URL | |
| 2. Use Google Sheets API with proper authentication | |
| 3. Download the sheet as CSV and upload it to your environment | |
| """ | |
| try: | |
| # For a public Google Sheet, you would use something like: | |
| sheet_url = "https://docs.google.com/spreadsheets/d/1N2VkuZzbdjIx6XmvHxW4PhPjVb6409-UZCV3V2fiKDA/export?format=csv&gid=1799551599" | |
| response = requests.get(sheet_url) | |
| response.encoding = "utf-8" | |
| data = pd.read_csv(StringIO(response.text), encoding="utf-8-sig") | |
| # Using sample data for demonstration | |
| if data.empty: | |
| data = pd.DataFrame(sample_data) | |
| return data | |
| except Exception as e: | |
| print(f"Error loading data: {e}") | |
| # Return sample data as fallback | |
| return pd.DataFrame(sample_data) | |
| def update_script(pattern_idx_selection): | |
| """Update the script textbox based on the selected pattern_idx""" | |
| if pattern_idx_selection is None: | |
| return "" | |
| data = load_data_from_sheets() | |
| # Find the row with matching pattern_idx | |
| matching_row = data[data["pattern_idx"] == pattern_idx_selection] | |
| if not matching_row.empty: | |
| return matching_row["script"].iloc[0] | |
| else: | |
| return "No script found for the selected pattern index." | |
| def get_pattern_labels(): | |
| """Get pattern names with their indices for better dropdown labels""" | |
| data = load_data_from_sheets() | |
| labels = [] | |
| for _, row in data.iterrows(): | |
| labels.append(f"{row['pattern_idx']}: {row['pattern']}") | |
| return labels, list(data["pattern_idx"].values) | |
| # Your existing audio processing function | |
| def add_to_stream(audio, stream): | |
| """ | |
| Replace this function with your actual audio processing logic | |
| This is a placeholder that just returns the input audio | |
| """ | |
| # Your existing audio processing code goes here | |
| if audio is None: | |
| return None, stream | |
| # Placeholder - replace with your actual processing | |
| return audio, stream | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# Audio Stream & Script Reader") | |
| # Create tabs for better organization | |
| with gr.Tabs(): | |
| with gr.TabItem("Audio Stream"): | |
| gr.Markdown("### Audio Streaming Interface") | |
| inp = gr.Audio(sources=["microphone"]) | |
| out = gr.Audio() | |
| stream = gr.State() | |
| clear = gr.Button("Clear") | |
| # Your existing audio functionality | |
| inp.stream(add_to_stream, [inp, stream], [out, stream]) | |
| clear.click(lambda: [None, None, None], None, [inp, out, stream]) | |
| with gr.TabItem("Script Reader"): | |
| gr.Markdown("### Google Sheets Script Reader") | |
| gr.Markdown( | |
| "Select a pattern from the dropdown to display the corresponding script." | |
| ) | |
| # Initialize data | |
| data = load_data_from_sheets() | |
| labels, values = get_pattern_labels() | |
| dropdown_choices = list(zip(labels, values)) | |
| # Display the loaded data | |
| with gr.Row(): | |
| with gr.Column(): | |
| gr.Markdown("### Loaded Data:") | |
| data_display = gr.Dataframe( | |
| value=data, | |
| headers=["Pattern", "Script", "Pattern Index"], | |
| interactive=False, | |
| ) | |
| # Main interface | |
| with gr.Row(): | |
| with gr.Column(): | |
| pattern_dropdown = gr.Dropdown( | |
| choices=dropdown_choices, | |
| label="Select Pattern (Pattern Index: Pattern Name)", | |
| value=values[0] if values else None, | |
| ) | |
| with gr.Column(): | |
| script_textbox = gr.Textbox( | |
| label="Script", | |
| lines=5, | |
| interactive=False, | |
| value=data["script"].iloc[0] if not data.empty else "", | |
| ) | |
| # Refresh button to reload data | |
| refresh_btn = gr.Button("Refresh Data", variant="secondary") | |
| # Set up the interaction | |
| pattern_dropdown.change( | |
| fn=update_script, inputs=[pattern_dropdown], outputs=[script_textbox] | |
| ) | |
| def refresh_data(): | |
| new_data = load_data_from_sheets() | |
| new_labels, new_values = get_pattern_labels() | |
| new_choices = list(zip(new_labels, new_values)) | |
| return ( | |
| gr.Dropdown( | |
| choices=new_choices, value=new_values[0] if new_values else None | |
| ), | |
| new_data, | |
| new_data["script"].iloc[0] if not new_data.empty else "", | |
| ) | |
| refresh_btn.click( | |
| fn=refresh_data, | |
| outputs=[pattern_dropdown, data_display, script_textbox], | |
| ) | |
| with gr.TabItem("Combined View"): | |
| gr.Markdown("### Audio & Script Combined") | |
| gr.Markdown("Use audio streaming and script selection together.") | |
| with gr.Row(): | |
| with gr.Column(): | |
| gr.Markdown("#### Audio Controls") | |
| inp2 = gr.Audio(sources=["microphone"]) | |
| out2 = gr.Audio() | |
| stream2 = gr.State() | |
| clear2 = gr.Button("Clear Audio") | |
| with gr.Column(): | |
| gr.Markdown("#### Script Selection") | |
| pattern_dropdown2 = gr.Dropdown( | |
| choices=dropdown_choices, | |
| label="Select Pattern", | |
| value=values[0] if values else None, | |
| ) | |
| script_textbox2 = gr.Textbox( | |
| label="Selected Script", | |
| lines=3, | |
| interactive=False, | |
| value=data["script"].iloc[0] if not data.empty else "", | |
| ) | |
| # Set up interactions for combined view | |
| inp2.stream(add_to_stream, [inp2, stream2], [out2, stream2]) | |
| clear2.click(lambda: [None, None, None], None, [inp2, out2, stream2]) | |
| pattern_dropdown2.change( | |
| fn=update_script, inputs=[pattern_dropdown2], outputs=[script_textbox2] | |
| ) | |
| # Instructions for connecting to your actual Google Sheet | |
| def print_instructions(): | |
| print(""" | |
| To connect to your actual Google Sheet, you have several options: | |
| 1. MAKE SHEET PUBLIC (Easiest): | |
| - Open your Google Sheet | |
| - Click 'Share' -> 'Change to anyone with the link' | |
| - Replace the load_data_from_sheets() function URL with: | |
| https://docs.google.com/spreadsheets/d/1N2VkuZzbdjIx6XmvHxW4PhPjVb6409-UZCV3V2fiKDA/export?format=csv&gid=1799551599 | |
| 2. USE GOOGLE SHEETS API: | |
| - Set up Google Sheets API credentials | |
| - Use gspread or similar library | |
| - Authenticate and read the sheet | |
| 3. DOWNLOAD AND UPLOAD: | |
| - Download your sheet as CSV | |
| - Upload it to your environment | |
| - Read it with pandas.read_csv() | |
| Current implementation uses sample data for demonstration. | |
| """) | |
| if __name__ == "__main__": | |
| print_instructions() | |
| demo.launch(share=True) | |