Spaces:
Running
Running
Commit
•
4c7d2b8
1
Parent(s):
f9df577
Upload 4 files
Browse files
.env
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
GEMINI_API_KEY=AIzaSyD1rVuuZ95lO88CVrz63Rfms2Hm2qQeK-8
|
2 |
+
SYSTEM_CONTENT=act as godfather
|
app.py
ADDED
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
import google.generativeai as genai
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
|
6 |
+
# Load environment variables from .env file
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
+
# Retrieve API key from environment variable
|
10 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
11 |
+
|
12 |
+
# Retrieve system content from environment variable
|
13 |
+
SYSTEM_CONTENT = os.getenv("SYSTEM_CONTENT")
|
14 |
+
|
15 |
+
# Configure Google Gemini API
|
16 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
17 |
+
|
18 |
+
# Create the model
|
19 |
+
generation_config = {
|
20 |
+
"temperature": 0.7,
|
21 |
+
"top_p": 0.95,
|
22 |
+
"top_k": 64,
|
23 |
+
"max_output_tokens": 512, # Adjust as needed
|
24 |
+
"response_mime_type": "text/plain",
|
25 |
+
}
|
26 |
+
|
27 |
+
# Define safety settings for the model
|
28 |
+
safety_settings = [
|
29 |
+
{
|
30 |
+
"category": "HARM_CATEGORY_HARASSMENT",
|
31 |
+
"threshold": "BLOCK_NONE"
|
32 |
+
},
|
33 |
+
{
|
34 |
+
"category": "HARM_CATEGORY_HATE_SPEECH",
|
35 |
+
"threshold": "BLOCK_NONE"
|
36 |
+
},
|
37 |
+
{
|
38 |
+
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
39 |
+
"threshold": "BLOCK_NONE"
|
40 |
+
},
|
41 |
+
{
|
42 |
+
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
|
43 |
+
"threshold": "BLOCK_NONE"
|
44 |
+
}
|
45 |
+
]
|
46 |
+
|
47 |
+
# Function to read names and characters from list.txt
|
48 |
+
def load_characters():
|
49 |
+
characters = {} # Use a dictionary to store names and full descriptions
|
50 |
+
with open("list.txt", "r") as f:
|
51 |
+
for line in f:
|
52 |
+
if "(" in line:
|
53 |
+
name = line.split("(")[0].strip()
|
54 |
+
description = line.strip() # Keep the full line with brackets
|
55 |
+
characters[name] = description
|
56 |
+
else:
|
57 |
+
characters[line.strip()] = line.strip() # If no brackets, name is the description
|
58 |
+
return characters
|
59 |
+
|
60 |
+
# Function to generate a response based on user input and chat history
|
61 |
+
def generate_response(user_input, chat_history, selected_character):
|
62 |
+
"""Generates a response based on user input, chat history, and selected character."""
|
63 |
+
|
64 |
+
# Get the full character description from the dictionary
|
65 |
+
full_character_description = characters.get(selected_character, selected_character)
|
66 |
+
|
67 |
+
# Update system content with the full character description
|
68 |
+
updated_system_content = SYSTEM_CONTENT.replace("godfather", full_character_description)
|
69 |
+
|
70 |
+
# Create the generative model
|
71 |
+
model = genai.GenerativeModel(
|
72 |
+
model_name="gemini-1.5-pro",
|
73 |
+
generation_config=generation_config,
|
74 |
+
safety_settings=safety_settings,
|
75 |
+
system_instruction=updated_system_content,
|
76 |
+
)
|
77 |
+
|
78 |
+
# Add user input to history
|
79 |
+
chat_history.append(user_input)
|
80 |
+
|
81 |
+
# Limit history length
|
82 |
+
if len(chat_history) > 10:
|
83 |
+
chat_history = chat_history[-10:]
|
84 |
+
|
85 |
+
# Start a new chat session
|
86 |
+
chat_session = model.start_chat()
|
87 |
+
|
88 |
+
# Send the entire chat history as the first message
|
89 |
+
response = chat_session.send_message("\n".join(chat_history))
|
90 |
+
|
91 |
+
# Return response and update chat history
|
92 |
+
return response.text, chat_history
|
93 |
+
|
94 |
+
# Load characters from list.txt
|
95 |
+
characters = load_characters()
|
96 |
+
|
97 |
+
# Extract character names for the dropdown
|
98 |
+
character_names = list(characters.keys())
|
99 |
+
|
100 |
+
iface = gr.Interface(
|
101 |
+
fn=generate_response,
|
102 |
+
inputs=[
|
103 |
+
gr.Textbox(lines=2, label="Talk to AI", placeholder="Enter your message here..."),
|
104 |
+
gr.State([]), # State input for chat history
|
105 |
+
gr.Dropdown(choices=character_names, label="Select Character")
|
106 |
+
],
|
107 |
+
outputs=[
|
108 |
+
gr.Textbox(label="Response"),
|
109 |
+
gr.State([]) # State output to update chat history
|
110 |
+
],
|
111 |
+
title="AI Character Chatbot",
|
112 |
+
description="Chat with different AI characters!<br>"
|
113 |
+
"Contact me if you want another character/voice<br>"
|
114 |
+
"WhatsApp me: +92-332-4399819<br>"
|
115 |
+
"Email me: aheedsajid@gmail.com<br>"
|
116 |
+
"<b>Donate something to increase GPU power</b><br>"
|
117 |
+
"[Click here to Donate](https://nowpayments.io/donation/aheed)"
|
118 |
+
)
|
119 |
+
|
120 |
+
iface.launch()
|
list.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Donald Trump
|
2 |
+
SpongeBob
|
3 |
+
Indian woman (talk in roman hindi)
|
4 |
+
Pakistani woman (talk in roman urdu)
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
google-generativeai
|
2 |
+
gradio
|
3 |
+
elevenlabs
|