Spaces:
Sleeping
Sleeping
prithivMLmods
commited on
Commit
•
4b9bbd9
1
Parent(s):
eaad29f
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
import google.generativeai as genai
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
|
6 |
+
load_dotenv()
|
7 |
+
|
8 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
9 |
+
|
10 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
11 |
+
|
12 |
+
def chatbot_response(user_input, mood):
|
13 |
+
|
14 |
+
mood_prompts = {
|
15 |
+
"Fun": "Respond in a light-hearted, playful manner.",
|
16 |
+
"Serious": "Respond in a thoughtful, serious tone.",
|
17 |
+
"Professional": "Respond in a formal, professional manner.",
|
18 |
+
"Upset": "Respond in a slightly irritated, upset tone."
|
19 |
+
}
|
20 |
+
|
21 |
+
try:
|
22 |
+
mood_instruction = mood_prompts.get(mood, "")
|
23 |
+
prompt = f"{mood_instruction}\n\nUser: {user_input}\nAI:"
|
24 |
+
|
25 |
+
response = genai.generate_text(prompt=prompt)
|
26 |
+
|
27 |
+
reply = response.candidates[0]['text']
|
28 |
+
|
29 |
+
return reply
|
30 |
+
except Exception as e:
|
31 |
+
return f"An error occurred: {str(e)}"
|
32 |
+
|
33 |
+
# Define the Gradio interface
|
34 |
+
iface = gr.Interface(
|
35 |
+
fn=chatbot_response,
|
36 |
+
inputs=[
|
37 |
+
gr.inputs.Textbox(lines=2, placeholder="Enter your message here..."),
|
38 |
+
gr.inputs.Radio(["Fun", "Serious", "Professional", "Upset"], label="Choose the chatbot's mood")
|
39 |
+
],
|
40 |
+
outputs="text",
|
41 |
+
title="Gemini Chatbot with Mood Options",
|
42 |
+
description="Chat with the Gemini AI-powered chatbot! Choose the mood for the response."
|
43 |
+
)
|
44 |
+
|
45 |
+
|
46 |
+
iface.launch()
|