Spaces:
Sleeping
Sleeping
File size: 1,168 Bytes
289affd e7fd04b 289affd 3391c58 e7fd04b b7cdc20 f180c8c e7fd04b b7cdc20 e7fd04b 289affd e7fd04b 289affd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import gradio as gr
import google.generativeai as genai
GOOGLE_API_KEY = "AIzaSyBrEIzU4mH733NMtkVs-axMHhRb0B-1h70"
genai.configure(api_key=GOOGLE_API_KEY)
def chat_with_gemini(message, persona):
try:
# 列出可用模型
models = genai.list_models()
# 选择支持 generate_content 的模型
model_name = None
for m in models:
if "generateContent" in m.supported_generation_methods:
model_name = m.name
break
if not model_name:
return "没有可用的 Gemini 聊天模型,请检查 API Key 权限或 Google Cloud 配置。"
model = genai.GenerativeModel(model_name)
response = model.generate_content([f"你是一个{persona}。", message])
return response.text
except Exception as e:
return f"请求或解析出错: {str(e)}"
with gr.Blocks() as demo:
persona = gr.Textbox(label="AI Persona 个性描述")
message = gr.Textbox(label="你的问题")
output = gr.Textbox(label="AI 回复")
btn = gr.Button("发送")
btn.click(chat_with_gemini, inputs=[message, persona], outputs=output)
demo.launch() |