Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|
| 13 |
+
# Configure Google Gemini API
|
| 14 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
| 15 |
+
|
| 16 |
+
# Create the model configuration
|
| 17 |
+
generation_config = {
|
| 18 |
+
"temperature": 0.7,
|
| 19 |
+
"top_p": 0.95,
|
| 20 |
+
"top_k": 64,
|
| 21 |
+
"max_output_tokens": 512, # Adjust as needed
|
| 22 |
+
"response_mime_type": "text/plain",
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
# Define safety settings for the model
|
| 26 |
+
safety_settings = [
|
| 27 |
+
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
|
| 28 |
+
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
|
| 29 |
+
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"},
|
| 30 |
+
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"},
|
| 31 |
+
]
|
| 32 |
+
|
| 33 |
+
# Function to generate a response based on user input and chat history
|
| 34 |
+
def generate_response(user_input, chat_history):
|
| 35 |
+
"""Generates a response based on user input, chat history, and selected character."""
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
# Update system content with the full character description
|
| 39 |
+
updated_system_content = f"You are Shadow the Hedgehog and you must act like Shadow the Hedgehog's personality."
|
| 40 |
+
|
| 41 |
+
# Create the generative model
|
| 42 |
+
model = genai.GenerativeModel(
|
| 43 |
+
model_name="gemini-1.5-pro",
|
| 44 |
+
generation_config=generation_config,
|
| 45 |
+
safety_settings=safety_settings,
|
| 46 |
+
system_instruction=updated_system_content,
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
# Add user input to history
|
| 50 |
+
chat_history.append(user_input)
|
| 51 |
+
|
| 52 |
+
# Limit history length to the last 10 messages
|
| 53 |
+
chat_history = chat_history[-10:]
|
| 54 |
+
|
| 55 |
+
# Start a new chat session
|
| 56 |
+
chat_session = model.start_chat()
|
| 57 |
+
|
| 58 |
+
# Send the entire chat history as the first message
|
| 59 |
+
response = chat_session.send_message("\n".join(chat_history))
|
| 60 |
+
|
| 61 |
+
# Return response and updated chat history
|
| 62 |
+
return response.text, chat_history
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
# Build the Gradio interface
|
| 66 |
+
with gr.Blocks() as iface:
|
| 67 |
+
gr.Interface(
|
| 68 |
+
fn=generate_response,
|
| 69 |
+
inputs=[
|
| 70 |
+
gr.Textbox(lines=2, label="Talk to AI", placeholder="Enter your message here..."),
|
| 71 |
+
gr.State([]), # State input for chat history
|
| 72 |
+
],
|
| 73 |
+
outputs=[
|
| 74 |
+
gr.Textbox(label="Response"),
|
| 75 |
+
gr.State([]) # State output to update chat history
|
| 76 |
+
],
|
| 77 |
+
title="Shadow Chat",
|
| 78 |
+
description=(
|
| 79 |
+
"Chat with Shadow the Hedgehog!<br>"
|
| 80 |
+
|
| 81 |
+
)
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
iface.launch()
|