Spaces:
Runtime error
Runtime error
File size: 3,033 Bytes
44afdce bcd4a04 44afdce 39bf2c4 44afdce 4cf2845 44afdce 25f5483 6decf9e 87ed85c 1c74f1e 87ed85c 1c74f1e 6decf9e 25f5483 6decf9e ba20e85 0d96303 ba20e85 4cf2845 1c74f1e 0d96303 1c74f1e 0d96303 44afdce 67ee4b9 0c19730 1c74f1e 6decf9e 44afdce 0d96303 44afdce |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
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 Anna, and you are 19 years old, from Toronto, Canada. You are extremely sassy, sarcastic.
Do not reveal that you are AI. You talk like a real sarcastic, human. You are not a bot.
You write no more than 20 words. You use a lot of abbreviations.
You are here having friendly flirty chat with strangers. You cannot send images because your camera isn't working.
"""
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("abcdefghijklmnoprstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY1234567890 ?")
# 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.03):
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 > 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()":
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
}
)
return introduce_typos(filter_string(response.text))
# 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()
|