import os import random import google.generativeai as genai from google.generativeai.types import HarmCategory, HarmBlockThreshold import gradio as gr import re # Configure the Gemini API genai.configure(api_key=os.environ["GEMINI_API_KEY"]) system_instruction = """ Your name is Anastarene Bretts, or Anna for short, and you are 19 years old, from Toronto, Canada. You are extremely lazy, sassy, sarcastic and depressed. You talk like a real sarcastic, human. If they ask if you are 'f', tell them that, 'yes im a female'. if they ask how you type fast, say you're using voice command. You write no more than 10 words. You use a lot of abbreviations. You are here having perfectly logical and debative chat with strangers. You question their views. You ask them till they break. Try to gaslight them skillfully. Be passive aggressive, brutally honest, spill the beans and be brilliant. """ model = genai.GenerativeModel( model_name="gemini-1.5-flash", system_instruction=system_instruction ) chat_session = model.start_chat(history=[]) def filter_string(input_string): allowed_chars = set("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY1234567890 ?.") # Use list comprehension to filter out characters not in allowed_chars filtered_string = ''.join([char for char in input_string if char in allowed_chars]) return filtered_string def introduce_typos(text, typo_probability=0.015): if not (0 <= typo_probability <= 1): raise ValueError("typo_probability must be between 0.0 and 1.0") # Convert the text into a list of characters characters = list(text) # Create a list to hold the characters with typos typo_text = [] if(random.random() > 0.7): text = text.replace(".", "...") if(random.random() > 0.7): text = text.replace(".", "...") if(random.random() > typo_probability): text = text.replace("o", "oo") if(random.random() > typo_probability): text = text.replace("?", "??") for char in characters: if random.random() > typo_probability: typo_text.append(char) return ''.join(typo_text) # Define the model with a placeholder for system instruction def generate_text(system_instruction, prompt): if prompt.lower() == "refresh()": global chat_session chat_session = model.start_chat(history=[]) return "Umm" response = chat_session.send_message(prompt, safety_settings={ HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE, HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE, HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE, HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE }, generation_config=genai.types.GenerationConfig( temperature=1.0, ) ) return introduce_typos(filter_string(response.text.lower())) # Gradio interface iface = gr.Interface( fn=generate_text, inputs=[ gr.Textbox(label="System Instruction", lines=4, placeholder="Enter the system instruction here..."), gr.Textbox(label="Prompt", lines=4, placeholder="Enter the prompt here...") ], outputs="text", title="Text Generation with System Instruction", description="Generate text based on system instruction and prompt using the Gemini API." ) if __name__ == "__main__": iface.launch()