Spaces:
Sleeping
Sleeping
import os | |
import gradio as gr | |
import google.generativeai as genai | |
from gradio_client import Client, file | |
from dotenv import load_dotenv | |
# Load environment variables from .env file | |
load_dotenv() | |
# Retrieve API key from environment variable | |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY") | |
# Retrieve system content from environment variable | |
SYSTEM_CONTENT = os.getenv("SYSTEM_CONTENT") | |
# Configure Google Gemini API | |
genai.configure(api_key=GEMINI_API_KEY) | |
# Create the model | |
generation_config = { | |
"temperature": 0.7, | |
"top_p": 0.95, | |
"top_k": 64, | |
"max_output_tokens": 512, # Adjust as needed | |
"response_mime_type": "text/plain", | |
} | |
# Define safety settings for the model | |
safety_settings = [ | |
{ | |
"category": "HARM_CATEGORY_HARASSMENT", | |
"threshold": "BLOCK_NONE" | |
}, | |
{ | |
"category": "HARM_CATEGORY_HATE_SPEECH", | |
"threshold": "BLOCK_NONE" | |
}, | |
{ | |
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", | |
"threshold": "BLOCK_NONE" | |
}, | |
{ | |
"category": "HARM_CATEGORY_DANGEROUS_CONTENT", | |
"threshold": "BLOCK_NONE" | |
} | |
] | |
# Create the generative model (outside the function) | |
model = genai.GenerativeModel( | |
model_name="gemini-1.5-pro", | |
generation_config=generation_config, | |
safety_settings=safety_settings, | |
system_instruction=SYSTEM_CONTENT, | |
) | |
# Initialize Gradio client for new TTS API (outside the function) | |
try: | |
tts_client = Client("tonyassi/voice-clone") | |
except ValueError as e: | |
print(f"Error initializing TTS client: {e}") | |
tts_client = None | |
def generate_response(user_input, chat_history): | |
"""Generates a response based on user input and chat history.""" | |
# Add user input to history | |
chat_history.append(user_input) | |
# Limit history length | |
if len(chat_history) > 10: | |
chat_history = chat_history[-10:] | |
# Start a new chat session | |
chat_session = model.start_chat() | |
# Send the entire chat history as the first message | |
response = chat_session.send_message("\n".join(chat_history)) | |
if tts_client: | |
# Use the new Gradio TTS API | |
tts_result = tts_client.predict( | |
text=response.text, | |
audio=file('audio.wav'), # Use local audio file | |
api_name="/predict" | |
) | |
else: | |
tts_result = None | |
# Return response and audio, and update chat history | |
return response.text, tts_result, chat_history | |
with gr.Blocks() as iface: | |
gr.Interface( | |
fn=generate_response, | |
inputs=[ | |
gr.Textbox(lines=2, label="Talk to AI Indian Girlfriend", placeholder="Enter your message here..."), | |
gr.State([]) # State input for chat history | |
], | |
outputs=[ | |
gr.Textbox(label="Response"), | |
gr.Audio(label="Voice Output", interactive=False, autoplay=True) if tts_client else gr.Textbox(label="Voice Output not available"), | |
gr.State([]) # State output to update chat history | |
], | |
title="AI Indian Girlfriend", | |
description="Contact me if you want another character/voice<br>WhatsApp me: +92-332-4399819<br> Email me: aheedsajid@gmail.com<br><b>Donate something to increase GPU power</b><br>[Click here to Donate](https://nowpayments.io/donation/aheed)<br>Please duplicate the space if you get an error!" | |
) | |
iface.launch() | |