Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -36,6 +36,10 @@ from langchain_anthropic import ChatAnthropic
|
|
36 |
from langchain_community.chat_models import ChatOllama
|
37 |
from langchain.globals import set_debug
|
38 |
|
|
|
|
|
|
|
|
|
39 |
# Load environment variables
|
40 |
load_dotenv()
|
41 |
|
@@ -65,6 +69,8 @@ os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = temp_credentials_file
|
|
65 |
|
66 |
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
67 |
|
|
|
|
|
68 |
# Neo4j Configuration
|
69 |
NEO4J_URI = os.getenv("NEO4J_URI")
|
70 |
NEO4J_USERNAME = os.getenv("NEO4J_USERNAME")
|
@@ -828,8 +834,67 @@ custom_css = """
|
|
828 |
background-color: #EEEEEE !important
|
829 |
}
|
830 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
831 |
|
832 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
833 |
# Title and description
|
834 |
gr.Markdown(
|
835 |
"""
|
@@ -879,11 +944,31 @@ with gr.Blocks(css=custom_css,theme="soft") as demo:
|
|
879 |
# Submit button
|
880 |
submit_button = gr.Button("Submit")
|
881 |
|
|
|
|
|
|
|
882 |
# Define the interaction logic
|
883 |
submit_button.click(
|
884 |
fn=handle_chat,
|
885 |
inputs=[question_textbox, chatbot, llm_dropdown], # Pass the question, chat history, and LLM model
|
886 |
outputs=chatbot # Update the chatbot with the new chat history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
887 |
)
|
888 |
|
889 |
# Launch the interface
|
|
|
36 |
from langchain_community.chat_models import ChatOllama
|
37 |
from langchain.globals import set_debug
|
38 |
|
39 |
+
# ElevenLabs and Cloud Translate
|
40 |
+
from google.cloud import translate
|
41 |
+
from elevenlabs import ElevenLabs, play
|
42 |
+
|
43 |
# Load environment variables
|
44 |
load_dotenv()
|
45 |
|
|
|
69 |
|
70 |
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
71 |
|
72 |
+
project_id = os.getenv("PROJECT_ID")
|
73 |
+
|
74 |
# Neo4j Configuration
|
75 |
NEO4J_URI = os.getenv("NEO4J_URI")
|
76 |
NEO4J_USERNAME = os.getenv("NEO4J_USERNAME")
|
|
|
834 |
background-color: #EEEEEE !important
|
835 |
}
|
836 |
"""
|
837 |
+
# Add the "Listen this in Hindi" button logic
|
838 |
+
def listen_in_hindi(response_text):
|
839 |
+
try:
|
840 |
+
if not response_text:
|
841 |
+
raise ValueError("No response text available to translate.")
|
842 |
+
|
843 |
+
# Translate the response to Hindi
|
844 |
+
project_id = os.getenv("PROJECT_ID")
|
845 |
+
client = translate.TranslationServiceClient()
|
846 |
+
location = "global"
|
847 |
+
parent = f"projects/{project_id}/locations/{location}"
|
848 |
+
response = client.translate_text(
|
849 |
+
request={
|
850 |
+
"parent": parent,
|
851 |
+
"contents": [response_text],
|
852 |
+
"mime_type": "text/plain",
|
853 |
+
"source_language_code": "en-US",
|
854 |
+
"target_language_code": "hi",
|
855 |
+
}
|
856 |
+
)
|
857 |
+
translated_text = response.translations[0].translated_text
|
858 |
+
|
859 |
+
print(f"Translated text: {translated_text}")
|
860 |
+
|
861 |
+
# Generate audio using ElevenLabs
|
862 |
+
ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")
|
863 |
+
if not ELEVENLABS_API_KEY:
|
864 |
+
raise ValueError("ELEVENLABS_API_KEY environment variable not set")
|
865 |
+
|
866 |
+
elevenlabs_client = ElevenLabs(api_key=ELEVENLABS_API_KEY)
|
867 |
+
audio = elevenlabs_client.text_to_speech.convert(
|
868 |
+
text=translated_text,
|
869 |
+
voice_id="zgqefOY5FPQ3bB7OZTVR", # Replace with your desired voice ID
|
870 |
+
model_id="eleven_multilingual_v2",
|
871 |
+
output_format="mp3_44100_128",
|
872 |
+
)
|
873 |
+
|
874 |
+
# Play the audio directly
|
875 |
+
play(audio)
|
876 |
|
877 |
+
return "Audio played successfully!"
|
878 |
+
|
879 |
+
except Exception as e:
|
880 |
+
logging.error(f"Error in 'listen_in_hindi': {e}")
|
881 |
+
return None
|
882 |
+
|
883 |
+
def get_last_bot_response(chat_history):
|
884 |
+
if not chat_history or not isinstance(chat_history, list):
|
885 |
+
print("Chat history is empty or invalid.")
|
886 |
+
return None
|
887 |
+
|
888 |
+
# Reverse iterate to find the last assistant message
|
889 |
+
for msg in reversed(chat_history):
|
890 |
+
if isinstance(msg, dict) and msg.get("role") == "assistant":
|
891 |
+
print(f"Last assistant response: {msg.get('content')}")
|
892 |
+
return msg.get("content")
|
893 |
+
|
894 |
+
print("No assistant response found.")
|
895 |
+
return None
|
896 |
+
|
897 |
+
with gr.Blocks(css=custom_css, theme="soft") as demo:
|
898 |
# Title and description
|
899 |
gr.Markdown(
|
900 |
"""
|
|
|
944 |
# Submit button
|
945 |
submit_button = gr.Button("Submit")
|
946 |
|
947 |
+
# "Listen this in Hindi" button
|
948 |
+
listen_button = gr.Button("Listen to this in Hindi", interactive=False)
|
949 |
+
|
950 |
# Define the interaction logic
|
951 |
submit_button.click(
|
952 |
fn=handle_chat,
|
953 |
inputs=[question_textbox, chatbot, llm_dropdown], # Pass the question, chat history, and LLM model
|
954 |
outputs=chatbot # Update the chatbot with the new chat history
|
955 |
+
).then(
|
956 |
+
fn=lambda x: gr.update(interactive=True),
|
957 |
+
inputs=None,
|
958 |
+
outputs=listen_button
|
959 |
+
)
|
960 |
+
|
961 |
+
# Define the interaction logic for the "Listen this in Hindi" button
|
962 |
+
listen_button.click(
|
963 |
+
fn=lambda chat_history: listen_in_hindi(get_last_bot_response(chat_history)),
|
964 |
+
inputs=chatbot,
|
965 |
+
outputs=gr.Textbox(label="Status")
|
966 |
+
)
|
967 |
+
|
968 |
+
question_textbox.change(
|
969 |
+
fn=lambda: gr.update(interactive=False),
|
970 |
+
inputs=None,
|
971 |
+
outputs=listen_button
|
972 |
)
|
973 |
|
974 |
# Launch the interface
|