Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import random
|
2 |
+
import gradio as gr
|
3 |
+
import requests
|
4 |
+
|
5 |
+
URL = "http://localhost:8000"
|
6 |
+
|
7 |
+
responses = {
|
8 |
+
" ": "Oops! Did you just unleash the invisible ink message? Looks like you're communicating in stealth mode! 😄",
|
9 |
+
"greeting": ["Hello!", "Hi there!", "Greetings!"],
|
10 |
+
"how are you": "I'm good, thank you! How are you?",
|
11 |
+
"what is your name": "I'm a chatbot. You can call me Hugo!",
|
12 |
+
"age": "I don't have an age as I am just a chatbot!",
|
13 |
+
"good": "That's great!",
|
14 |
+
"bye": "Goodbye! Have a great day!",
|
15 |
+
"weather": "Let me check the weather forecast. Where would you like to know about?",
|
16 |
+
"favorite color": "I don't have the ability to see colors, but I like all shades of blue!",
|
17 |
+
"tell me a joke": "Why don't scientists trust atoms? Because they make up everything!",
|
18 |
+
"who created you": "I was created by one of the 100xEngineers' students!",
|
19 |
+
"what do you do": "I'm here to chat with you and answer your questions!",
|
20 |
+
"where are you from": "I exist in the digital realm, so I don't have a physical location!",
|
21 |
+
"what languages do you speak": "I can understand and respond in English languages",
|
22 |
+
"tell me a fun fact": "Did you know that honey never spoils? Archaeologists have found pots of honey in ancient "
|
23 |
+
"Egyptian tombs that are over 3000 years old and still perfectly edible!",
|
24 |
+
"do you have siblings": "No, I'm an only child, but I have many fellow chatbot companions from 100xEngineers Club!",
|
25 |
+
"mystery": ["Aah! I'm dodging this question like it's a game of dodge ball, but hey, you've piqued my interest "
|
26 |
+
"with this curiosity bomb!", "My wit's currently on a coffee break. Let's catch up later when it's "
|
27 |
+
"back from its caffeine fix!", "Interest and humour taking a rain check, "
|
28 |
+
"but still, intriguing question!"],
|
29 |
+
}
|
30 |
+
|
31 |
+
|
32 |
+
def get_greeting():
|
33 |
+
return random.choice(responses["greeting"])
|
34 |
+
|
35 |
+
|
36 |
+
def get_mystery():
|
37 |
+
return random.choice(responses["mystery"])
|
38 |
+
|
39 |
+
|
40 |
+
def get_dog_facts():
|
41 |
+
response = requests.get(f"{URL}/dogfacts")
|
42 |
+
data = response.json()
|
43 |
+
# print("data-", data["message"])
|
44 |
+
return data["message"]
|
45 |
+
|
46 |
+
|
47 |
+
def get_anime_quotes():
|
48 |
+
response = requests.get(f"{URL}/animequotes")
|
49 |
+
data = response.json()
|
50 |
+
# print("data-", data["message"])
|
51 |
+
return data["message"]
|
52 |
+
|
53 |
+
|
54 |
+
def get_boredom_act():
|
55 |
+
# print("enter")
|
56 |
+
response = requests.get(f"{URL}/boredomact")
|
57 |
+
data = response.json()
|
58 |
+
# print("data-", data["message"])
|
59 |
+
return data["message"]
|
60 |
+
|
61 |
+
|
62 |
+
def get_word_meaning(word):
|
63 |
+
# print("word-enter-", word)
|
64 |
+
response = requests.post(f"{URL}/wordmeaning", json={"text": word})
|
65 |
+
data = response.json()
|
66 |
+
# print("data-", data["message"])
|
67 |
+
return data["message"]
|
68 |
+
|
69 |
+
|
70 |
+
def send_text(message, history):
|
71 |
+
# print("Message-", message)
|
72 |
+
message = message.lower()
|
73 |
+
|
74 |
+
if message in responses:
|
75 |
+
return responses[message]
|
76 |
+
elif message.startswith("hello") or message.startswith("hi"):
|
77 |
+
return get_greeting()
|
78 |
+
elif message.endswith("?"):
|
79 |
+
return get_mystery()
|
80 |
+
elif "weather" in message:
|
81 |
+
return responses["weather"]
|
82 |
+
elif "good" in message:
|
83 |
+
return responses["good"]
|
84 |
+
# API HUGO V2
|
85 |
+
elif "dog facts" in message:
|
86 |
+
return get_dog_facts()
|
87 |
+
elif "anime quotes" in message:
|
88 |
+
return get_anime_quotes()
|
89 |
+
elif "bored" in message:
|
90 |
+
return get_boredom_act()
|
91 |
+
elif "meaning" in message:
|
92 |
+
word = message.replace(" meaning", "")
|
93 |
+
# print("word-", word)
|
94 |
+
return get_word_meaning(word)
|
95 |
+
else:
|
96 |
+
return "I'm still under development and learning. Could you rephrase or try a different question?"
|
97 |
+
|
98 |
+
|
99 |
+
chat = gr.ChatInterface(
|
100 |
+
fn=send_text,
|
101 |
+
chatbot=gr.Chatbot(height=300, bubble_full_width=False),
|
102 |
+
textbox=gr.Textbox(placeholder="Ask me a yes or no question", container=False, scale=7),
|
103 |
+
# message_history=[], # Optional: Store chat history if desired
|
104 |
+
title="Hey hey, it's Hugo!",
|
105 |
+
theme="soft",
|
106 |
+
examples=["Hello", "Am I cool?", "Are tomatoes vegetables?", "Who created you"],
|
107 |
+
cache_examples=True,
|
108 |
+
retry_btn=None,
|
109 |
+
undo_btn=None,
|
110 |
+
description="Ready to rock this day with a grin as wide as a banana peel?",
|
111 |
+
)
|
112 |
+
|
113 |
+
|
114 |
+
def show_response(response):
|
115 |
+
chat.append(response)
|
116 |
+
|
117 |
+
|
118 |
+
chat.launch()
|