tony1966's picture
Create app.py
0856fb5 verified
raw
history blame
1.09 kB
import gradio as gr
import google.generativeai as genai
import os, time
from dotenv import load_dotenv
def ask_gemini(model_sel, prompt):
start_time=time.time()
model=genai.GenerativeModel(model_sel)
safety_settings={
'HATE': 'BLOCK_NONE',
'HARASSMENT': 'BLOCK_NONE',
'SEXUAL' : 'BLOCK_NONE',
'DANGEROUS' : 'BLOCK_NONE'
}
reply=model.generate_content(prompt, safety_settings=safety_settings)
elapsed_time=time.time()-start_time
return f'{reply.text}\n模型: {model_sel}\n耗時: {elapsed_time:.2f} 秒'
load_dotenv()
api_key=os.environ.get('GEMINI_API_KEY')
genai.configure(api_key=api_key)
model_sel=gr.Radio(label='選擇模型',
choices=['gemini-1.5-flash', 'gemini-1.5-pro'],
value='gemini-1.5-flash')
prompt=gr.Textbox(label='您的詢問: ')
reply=gr.Textbox(label='Gemini 的回答: ')
iface=gr.Interface(
fn=ask_gemini,
inputs=[model_sel, prompt],
outputs=reply,
title='Gemini API 聊天機器人',
flagging_mode='never',
)
iface.launch()