Spaces:
Sleeping
Sleeping
Download app.py from SubEasy/AI-Persona: direct link, hf CLI and curl.
- Browser
- Download file 1.17 kB
-
https://huggingface.co/spaces/SubEasy/AI-Persona/resolve/main/app.py
- Command line
-
hf download hf://spaces/SubEasy/AI-Persona/app.py
-
curl -L -o app.py https://huggingface.co/spaces/SubEasy/AI-Persona/resolve/main/app.py
1.17 kB
| 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() |