Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import google.generativeai as genai
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
# Configure the Google Gemini API with the environment variable for the API key
|
6 |
+
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
7 |
+
|
8 |
+
# Set up the generation configuration
|
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 |
+
# Load the model (Gemini 1.5 flash in this case)
|
18 |
+
model = genai.GenerativeModel(
|
19 |
+
model_name="gemini-1.5-flash",
|
20 |
+
generation_config=generation_config,
|
21 |
+
)
|
22 |
+
|
23 |
+
# Function to handle conversation
|
24 |
+
def generate_response(user_input):
|
25 |
+
chat_session = model.start_chat(
|
26 |
+
history=[{
|
27 |
+
"role": "user",
|
28 |
+
"parts": [user_input],
|
29 |
+
}]
|
30 |
+
)
|
31 |
+
|
32 |
+
response = chat_session.send_message(user_input)
|
33 |
+
return response.text
|
34 |
+
|
35 |
+
# Gradio Interface
|
36 |
+
with gr.Blocks() as demo:
|
37 |
+
gr.Markdown("# Google Gemini Chat Interface")
|
38 |
+
|
39 |
+
user_input = gr.Textbox(label="Enter your prompt", placeholder="Ask anything...")
|
40 |
+
output_text = gr.Textbox(label="Model Output")
|
41 |
+
|
42 |
+
generate_button = gr.Button("Generate Response")
|
43 |
+
|
44 |
+
generate_button.click(generate_response, inputs=user_input, outputs=output_text)
|
45 |
+
|
46 |
+
# Launch the Gradio app
|
47 |
+
demo.launch()
|