Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
# Carrega variáveis de ambiente
|
| 7 |
+
load_dotenv()
|
| 8 |
+
|
| 9 |
+
# Configura a API do Gemini
|
| 10 |
+
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
| 11 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
| 12 |
+
|
| 13 |
+
# Configurações do modelo
|
| 14 |
+
generation_config = {
|
| 15 |
+
"temperature": 0.7,
|
| 16 |
+
"top_p": 0.95,
|
| 17 |
+
"top_k": 40,
|
| 18 |
+
"max_output_tokens": 2048,
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
# Inicializa o modelo
|
| 22 |
+
model = genai.GenerativeModel(
|
| 23 |
+
model_name="gemini-1.5-pro", # ou "gemini-1.5-flash" para mais rápido
|
| 24 |
+
generation_config=generation_config,
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
def chat_with_gemini(message, history):
|
| 28 |
+
"""
|
| 29 |
+
Função para interagir com o Gemini
|
| 30 |
+
"""
|
| 31 |
+
try:
|
| 32 |
+
# Inicia uma conversa
|
| 33 |
+
chat = model.start_chat(history=[])
|
| 34 |
+
|
| 35 |
+
# Envia a mensagem e obtém resposta
|
| 36 |
+
response = chat.send_message(message)
|
| 37 |
+
|
| 38 |
+
return response.text
|
| 39 |
+
except Exception as e:
|
| 40 |
+
return f"Erro: {str(e)}"
|
| 41 |
+
|
| 42 |
+
# Cria a interface com Gradio
|
| 43 |
+
def create_interface():
|
| 44 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="Chat com Gemini") as demo:
|
| 45 |
+
gr.Markdown("""
|
| 46 |
+
# 🤖 Chat com Google Gemini
|
| 47 |
+
### Converse com o modelo Gemini do Google AI
|
| 48 |
+
""")
|
| 49 |
+
|
| 50 |
+
chatbot = gr.Chatbot(label="Conversa", height=500)
|
| 51 |
+
|
| 52 |
+
with gr.Row():
|
| 53 |
+
msg = gr.Textbox(
|
| 54 |
+
label="Sua mensagem",
|
| 55 |
+
placeholder="Digite sua pergunta aqui...",
|
| 56 |
+
scale=9
|
| 57 |
+
)
|
| 58 |
+
send_btn = gr.Button("Enviar", variant="primary", scale=1)
|
| 59 |
+
|
| 60 |
+
clear_btn = gr.Button("Limpar conversa", variant="secondary")
|
| 61 |
+
|
| 62 |
+
def respond(message, chat_history):
|
| 63 |
+
if not message:
|
| 64 |
+
return "", chat_history
|
| 65 |
+
|
| 66 |
+
# Obtém resposta do Gemini
|
| 67 |
+
response = chat_with_gemini(message, chat_history)
|
| 68 |
+
|
| 69 |
+
# Adiciona à conversa
|
| 70 |
+
chat_history.append((message, response))
|
| 71 |
+
|
| 72 |
+
return "", chat_history
|
| 73 |
+
|
| 74 |
+
def clear_chat():
|
| 75 |
+
return []
|
| 76 |
+
|
| 77 |
+
# Conecta os eventos
|
| 78 |
+
send_btn.click(
|
| 79 |
+
respond,
|
| 80 |
+
inputs=[msg, chatbot],
|
| 81 |
+
outputs=[msg, chatbot]
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
msg.submit(
|
| 85 |
+
respond,
|
| 86 |
+
inputs=[msg, chatbot],
|
| 87 |
+
outputs=[msg, chatbot]
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
clear_btn.click(
|
| 91 |
+
clear_chat,
|
| 92 |
+
outputs=[chatbot]
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
return demo
|
| 96 |
+
|
| 97 |
+
# Executa a aplicação
|
| 98 |
+
if __name__ == "__main__":
|
| 99 |
+
demo = create_interface()
|
| 100 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|