Ath commited on
Create app,py
Browse files
app,py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Install the necessary libraries
|
| 2 |
+
!pip install google-generativeai requests gradio
|
| 3 |
+
|
| 4 |
+
import os
|
| 5 |
+
import requests
|
| 6 |
+
import google.generativeai as genai
|
| 7 |
+
import gradio as gr
|
| 8 |
+
|
| 9 |
+
# Configure your Google Generative AI API key
|
| 10 |
+
genai.configure(api_key="AIzaSyDFjkgDAcnSAXDpsQJAMOaNCKY_SNJ-sD4")
|
| 11 |
+
|
| 12 |
+
# Create the model
|
| 13 |
+
generation_config = {
|
| 14 |
+
"temperature": 1,
|
| 15 |
+
"top_p": 0.95,
|
| 16 |
+
"top_k": 64,
|
| 17 |
+
"max_output_tokens": 8192,
|
| 18 |
+
"response_mime_type": "text/plain",
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
model = genai.GenerativeModel(
|
| 22 |
+
model_name="gemini-1.5-pro",
|
| 23 |
+
generation_config=generation_config,
|
| 24 |
+
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.",
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
chat_session = model.start_chat(history=[])
|
| 28 |
+
|
| 29 |
+
# Eleven Labs API configuration
|
| 30 |
+
eleven_labs_api_key = "sk_9ac3d5449624c92f16ca187955853621355a5f0ac52572e3"
|
| 31 |
+
eleven_labs_url = "https://api.elevenlabs.io/v1/text-to-speech/aEO01A4wXwd1O8GPgGlF"
|
| 32 |
+
|
| 33 |
+
def chat_and_tts(user_input):
|
| 34 |
+
# Send the user's input to the chat session
|
| 35 |
+
response = chat_session.send_message(user_input)
|
| 36 |
+
response_text = response.text
|
| 37 |
+
|
| 38 |
+
# Eleven Labs text-to-speech request payload
|
| 39 |
+
payload = {
|
| 40 |
+
"text": response_text,
|
| 41 |
+
"voice_settings": {
|
| 42 |
+
"stability": 0,
|
| 43 |
+
"similarity_boost": 0
|
| 44 |
+
}
|
| 45 |
+
}
|
| 46 |
+
headers = {
|
| 47 |
+
"xi-api-key": eleven_labs_api_key,
|
| 48 |
+
"Content-Type": "application/json"
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
# Make the request to Eleven Labs API
|
| 52 |
+
tts_response = requests.post(eleven_labs_url, json=payload, headers=headers)
|
| 53 |
+
|
| 54 |
+
# Check if the response is successful and save the audio content to a file
|
| 55 |
+
if tts_response.status_code == 200:
|
| 56 |
+
audio_path = '/content/response_audio.mp3'
|
| 57 |
+
with open(audio_path, 'wb') as file:
|
| 58 |
+
file.write(tts_response.content)
|
| 59 |
+
return response_text, audio_path
|
| 60 |
+
else:
|
| 61 |
+
return response_text, None
|
| 62 |
+
|
| 63 |
+
# Gradio interface
|
| 64 |
+
def chat_interface(user_input):
|
| 65 |
+
response_text, audio_path = chat_and_tts(user_input)
|
| 66 |
+
return response_text, audio_path
|
| 67 |
+
|
| 68 |
+
# Create the Gradio UI
|
| 69 |
+
iface = gr.Interface(
|
| 70 |
+
fn=chat_interface,
|
| 71 |
+
inputs="text",
|
| 72 |
+
outputs=["text", "audio"],
|
| 73 |
+
title="Chat with Ath",
|
| 74 |
+
description="Ask any question and get a friendly response from Ath. The response will also be converted to speech.",
|
| 75 |
+
theme="huggingface"
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
# Launch the Gradio app
|
| 79 |
+
iface.launch()
|