Spaces:
Running
Running
aheedsajid
commited on
Commit
•
8c762cf
1
Parent(s):
e800e86
Upload 2 files
Browse files- app.py +130 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
import google.generativeai as genai
|
4 |
+
from gradio_client import Client, file
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
|
7 |
+
# Load environment variables from .env file
|
8 |
+
load_dotenv()
|
9 |
+
|
10 |
+
# Retrieve API key from environment variable
|
11 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
12 |
+
|
13 |
+
# Retrieve system content from environment variable
|
14 |
+
SYSTEM_CONTENT = os.getenv("SYSTEM_CONTENT")
|
15 |
+
|
16 |
+
# Configure Google Gemini API
|
17 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
18 |
+
|
19 |
+
# Create the model
|
20 |
+
generation_config = {
|
21 |
+
"temperature": 0.7,
|
22 |
+
"top_p": 0.95,
|
23 |
+
"top_k": 64,
|
24 |
+
"max_output_tokens": 512, # Adjust as needed
|
25 |
+
"response_mime_type": "text/plain",
|
26 |
+
}
|
27 |
+
|
28 |
+
# Define safety settings for the model
|
29 |
+
safety_settings = [
|
30 |
+
{
|
31 |
+
"category": "HARM_CATEGORY_HARASSMENT",
|
32 |
+
"threshold": "BLOCK_NONE"
|
33 |
+
},
|
34 |
+
{
|
35 |
+
"category": "HARM_CATEGORY_HATE_SPEECH",
|
36 |
+
"threshold": "BLOCK_NONE"
|
37 |
+
},
|
38 |
+
{
|
39 |
+
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
40 |
+
"threshold": "BLOCK_NONE"
|
41 |
+
},
|
42 |
+
{
|
43 |
+
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
|
44 |
+
"threshold": "BLOCK_NONE"
|
45 |
+
}
|
46 |
+
]
|
47 |
+
|
48 |
+
# Create the generative model (outside the function)
|
49 |
+
model = genai.GenerativeModel(
|
50 |
+
model_name="gemini-1.5-pro",
|
51 |
+
generation_config=generation_config,
|
52 |
+
safety_settings=safety_settings,
|
53 |
+
system_instruction=SYSTEM_CONTENT,
|
54 |
+
)
|
55 |
+
|
56 |
+
# Initialize Gradio client for new TTS API (outside the function)
|
57 |
+
try:
|
58 |
+
tts_client = Client("tonyassi/voice-clone")
|
59 |
+
except ValueError as e:
|
60 |
+
print(f"Error initializing TTS client: {e}")
|
61 |
+
tts_client = None
|
62 |
+
|
63 |
+
def generate_response(user_input, chat_history):
|
64 |
+
"""Generates a response based on user input and chat history."""
|
65 |
+
|
66 |
+
# Add user input to history
|
67 |
+
chat_history.append(user_input)
|
68 |
+
|
69 |
+
# Limit history length
|
70 |
+
if len(chat_history) > 10:
|
71 |
+
chat_history = chat_history[-10:]
|
72 |
+
|
73 |
+
# Start a new chat session
|
74 |
+
chat_session = model.start_chat()
|
75 |
+
|
76 |
+
# Send the entire chat history as the first message
|
77 |
+
response = chat_session.send_message("\n".join(chat_history))
|
78 |
+
|
79 |
+
if tts_client:
|
80 |
+
# Use the new Gradio TTS API
|
81 |
+
tts_result = tts_client.predict(
|
82 |
+
text=response.text,
|
83 |
+
audio=file('audio.wav'), # Use local audio file
|
84 |
+
api_name="/predict"
|
85 |
+
)
|
86 |
+
else:
|
87 |
+
tts_result = None
|
88 |
+
|
89 |
+
# Return response and audio, and update chat history
|
90 |
+
return response.text, tts_result, chat_history
|
91 |
+
|
92 |
+
|
93 |
+
# HTML code for the ad banner
|
94 |
+
ad_code = """
|
95 |
+
<div style="text-align: center;">
|
96 |
+
<iframe src="https://www.fiverr.com/gig_widgets?id=U2FsdGVkX1+9vQaosK0mq7xGXzIb/5hdFYIQsmHlpeMwFm5gingveDZKXBz2oJwV7ZwvToxseOx/KzKtr77ryB3qTb83/+GvCyI8OKpZcSgM0sDHzEPlZnqNeu8Db4y/IXlAceA1UcYT5Z3cW5vBLrgJ/WOpyMWSiyfwwMmGngjltQBexcivW7ukRd+/0yos7GCbfGjGdnLPHm2LB/CB9w2mA1i8cVv9LU+UUdj/O5KLRXtnychl9wAMkPTpmwMiyuAfUZy0nbK/Xa5O2UecmCvn7wicTr0TCTpLmIxaEh3YftXOVr9e36OGVYTmy5nlsgujkqJPI7wL0dXfon0ru4kvoycC7UD6m/whMoxrWZOf386qV2eEeXzk3vKO/emXUUfSavtofzSBtjLcyAOVhnHJg27PvVPgvFTQdy0o3F1M0DaHYTVW3Ln45MPYvMSH&affiliate_id=36184&strip_google_tagmanager=true" loading="lazy" data-with-title="false" class="fiverr_nga_frame" frameborder="0" height="350" width="100%" referrerpolicy="no-referrer-when-downgrade" data-mode="random_gigs" onload=" var frame = this; var script = document.createElement('script'); script.addEventListener('load', function() { window.FW_SDK.register(frame); }); script.setAttribute('src', 'https://www.fiverr.com/gig_widgets/sdk'); document.body.appendChild(script); " ></iframe>
|
97 |
+
</div>
|
98 |
+
"""
|
99 |
+
|
100 |
+
# HTML code for the ad banner
|
101 |
+
ad_code2 = """
|
102 |
+
<div style="text-align: center;">
|
103 |
+
<a href="https://beta.publishers.adsterra.com/referral/UNXJYTziBP" target="_blank" style="display: inline-block;">
|
104 |
+
<img decoding="async" alt="banner" src="https://landings-cdn.adsterratech.com/referralBanners/gif/468x120_adsterra_reff.gif">
|
105 |
+
</a>
|
106 |
+
<a href="https://go.fiverr.com/visit/?bta=36184&brand=fiverrcpa&landingPage=https%253A%252F%252Fwww.fiverr.com%252Fcategories%252Fprogramming-tech%252Fai-coding%252Fai-applications%253Fsource%253Dcategory_tree" target="_blank" style="display: inline-block;">
|
107 |
+
<img fetchpriority="high" decoding="async" width="468" height="120" src="https://ziverr.xyz/wp-content/uploads/2024/06/PASSIVE-1.gif" class="attachment-large size-large wp-image-1266" alt="">
|
108 |
+
</a>
|
109 |
+
</div>
|
110 |
+
"""
|
111 |
+
|
112 |
+
with gr.Blocks() as iface:
|
113 |
+
gr.HTML(ad_code)
|
114 |
+
gr.Interface(
|
115 |
+
fn=generate_response,
|
116 |
+
inputs=[
|
117 |
+
gr.Textbox(lines=2, label="Talk to AI Girlfriend", placeholder="Enter your message here..."),
|
118 |
+
gr.State([]) # State input for chat history
|
119 |
+
],
|
120 |
+
outputs=[
|
121 |
+
gr.Textbox(label="Response"),
|
122 |
+
gr.Audio(label="Voice Output", interactive=False, autoplay=True) if tts_client else gr.Textbox(label="Voice Output not available"),
|
123 |
+
gr.State([]) # State output to update chat history
|
124 |
+
],
|
125 |
+
title="AI Girlfriend",
|
126 |
+
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!"
|
127 |
+
)
|
128 |
+
gr.HTML(ad_code2)
|
129 |
+
|
130 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gtts
|
2 |
+
gradio
|
3 |
+
requests
|
4 |
+
python-dotenv
|
5 |
+
google-generativeai
|