File size: 2,411 Bytes
fe12fb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# Install the necessary libraries
!pip install google-generativeai requests gradio

import os
import requests
import google.generativeai as genai
import gradio as gr

# Configure your Google Generative AI API key
genai.configure(api_key="AIzaSyDFjkgDAcnSAXDpsQJAMOaNCKY_SNJ-sD4")

# Create the model
generation_config = {
  "temperature": 1,
  "top_p": 0.95,
  "top_k": 64,
  "max_output_tokens": 8192,
  "response_mime_type": "text/plain",
}

model = genai.GenerativeModel(
  model_name="gemini-1.5-pro",
  generation_config=generation_config,
  system_instruction="You are a human, living on Earth. You speak friendly and use casual language. You never say you're an AI or program. Your name is Ath, and you always speak like a 100% human.",
)

chat_session = model.start_chat(history=[])

# Eleven Labs API configuration
eleven_labs_api_key = "sk_9ac3d5449624c92f16ca187955853621355a5f0ac52572e3"
eleven_labs_url = "https://api.elevenlabs.io/v1/text-to-speech/aEO01A4wXwd1O8GPgGlF"

def chat_and_tts(user_input):
    # Send the user's input to the chat session
    response = chat_session.send_message(user_input)
    response_text = response.text

    # Eleven Labs text-to-speech request payload
    payload = {
        "text": response_text,
        "voice_settings": {
            "stability": 0,
            "similarity_boost": 0
        }
    }
    headers = {
        "xi-api-key": eleven_labs_api_key,
        "Content-Type": "application/json"
    }

    # Make the request to Eleven Labs API
    tts_response = requests.post(eleven_labs_url, json=payload, headers=headers)

    # Check if the response is successful and save the audio content to a file
    if tts_response.status_code == 200:
        audio_path = '/content/response_audio.mp3'
        with open(audio_path, 'wb') as file:
            file.write(tts_response.content)
        return response_text, audio_path
    else:
        return response_text, None

# Gradio interface
def chat_interface(user_input):
    response_text, audio_path = chat_and_tts(user_input)
    return response_text, audio_path

# Create the Gradio UI
iface = gr.Interface(
    fn=chat_interface,
    inputs="text",
    outputs=["text", "audio"],
    title="Chat with Ath",
    description="Ask any question and get a friendly response from Ath. The response will also be converted to speech.",
    theme="huggingface"
)

# Launch the Gradio app
iface.launch()