Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,37 @@
|
|
| 1 |
-
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
import
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
# Create
|
| 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=
|
| 47 |
-
inputs=
|
| 48 |
-
outputs="
|
| 49 |
-
title="AI
|
| 50 |
-
description="Enter
|
| 51 |
)
|
| 52 |
|
| 53 |
-
# Launch the
|
| 54 |
-
iface.launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
|
| 4 |
+
# Define the function to interact with the API
|
| 5 |
+
def generate_content(text):
|
| 6 |
+
url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key=AIzaSyAflU5a8T3dCuIrkql737ZQtqWdrPmo0ts'
|
| 7 |
+
headers = {'Content-Type': 'application/json'}
|
| 8 |
+
data = {
|
| 9 |
+
"contents": [
|
| 10 |
+
{
|
| 11 |
+
"parts": [
|
| 12 |
+
{
|
| 13 |
+
"text": text
|
| 14 |
+
}
|
| 15 |
+
]
|
| 16 |
+
}
|
| 17 |
+
]
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
response = requests.post(url, headers=headers, json=data)
|
| 21 |
+
|
| 22 |
+
if response.status_code == 200:
|
| 23 |
+
return response.json() # You can customize this to return specific parts of the response
|
| 24 |
+
else:
|
| 25 |
+
return f"Error: {response.status_code} - {response.text}"
|
| 26 |
|
| 27 |
+
# Create a Gradio interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
iface = gr.Interface(
|
| 29 |
+
fn=generate_content,
|
| 30 |
+
inputs="text",
|
| 31 |
+
outputs="json",
|
| 32 |
+
title="AI Content Generator",
|
| 33 |
+
description="Enter a prompt to generate content using the Google Gemini API."
|
| 34 |
)
|
| 35 |
|
| 36 |
+
# Launch the Gradio app
|
| 37 |
+
iface.launch()
|