Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import google.generativeai as genai
|
| 4 |
+
|
| 5 |
+
# Configure the Google Generative AI
|
| 6 |
+
genai.configure(api_key=os.environ["GEMINI_API_KEY"])
|
| 7 |
+
|
| 8 |
+
# Create the model
|
| 9 |
+
generation_config = {
|
| 10 |
+
"temperature": 1,
|
| 11 |
+
"top_p": 0.95,
|
| 12 |
+
"top_k": 64,
|
| 13 |
+
"max_output_tokens": 8192,
|
| 14 |
+
"response_mime_type": "text/plain",
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
model = genai.GenerativeModel(
|
| 18 |
+
model_name="gemini-1.5-flash",
|
| 19 |
+
generation_config=generation_config,
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# Initialize a chat session
|
| 23 |
+
chat_session = model.start_chat(
|
| 24 |
+
history=[
|
| 25 |
+
{
|
| 26 |
+
"role": "user",
|
| 27 |
+
"parts": [
|
| 28 |
+
"tell me a story about me",
|
| 29 |
+
],
|
| 30 |
+
},
|
| 31 |
+
{
|
| 32 |
+
"role": "model",
|
| 33 |
+
"parts": [
|
| 34 |
+
"Please tell me a little bit about yourself! I need some information to write a story about you. For example:\n\n* **What are your hobbies and interests?** \n* **What are some of your favorite things?** \n* **What kind of story do you want to hear?** (Adventure, romance, mystery, etc.)\n* **Is there anything unique or interesting about you that you'd like to see in the story?**\n\nOnce I know more about you, I can write a fun and engaging story just for you! 😊 \n",
|
| 35 |
+
],
|
| 36 |
+
},
|
| 37 |
+
]
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
def generate_story(user_input):
|
| 41 |
+
response = chat_session.send_message(user_input)
|
| 42 |
+
return response.text
|
| 43 |
+
|
| 44 |
+
# Create a Gradio Interface
|
| 45 |
+
iface = gr.Interface(
|
| 46 |
+
fn=generate_story,
|
| 47 |
+
inputs=gr.Textbox(lines=4, placeholder="Enter some information about yourself..."),
|
| 48 |
+
outputs="text",
|
| 49 |
+
title="AI Story Generator",
|
| 50 |
+
description="Enter some details about yourself, and the AI will generate a personalized story for you!"
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
# Launch the interface
|
| 54 |
+
iface.launch()
|