Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,52 +1,101 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
import
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
# Create the Gradio interface
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
)
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import random
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
+
|
| 6 |
+
# Load pre-trained model and tokenizer
|
| 7 |
+
model_name = "EleutherAI/gpt-neo-2.7B"
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 10 |
+
|
| 11 |
+
# Predefined list of jokes
|
| 12 |
+
jokes = [
|
| 13 |
+
"Why don't scientists trust atoms? Because they make up everything!",
|
| 14 |
+
"Why did the scarecrow win an award? Because he was outstanding in his field!",
|
| 15 |
+
"Why don't skeletons fight each other? They don't have the guts."
|
| 16 |
+
]
|
| 17 |
+
|
| 18 |
+
# Function to fetch weather data from wttr.in
|
| 19 |
+
def get_weather(city):
|
| 20 |
+
base_url = f"http://wttr.in/{city}?format=%C+%t+%w+%m"
|
| 21 |
+
response = requests.get(base_url)
|
| 22 |
+
if response.status_code == 200:
|
| 23 |
+
return response.text.strip()
|
| 24 |
+
else:
|
| 25 |
+
return "City not found."
|
| 26 |
+
|
| 27 |
+
# Function to generate chatbot response
|
| 28 |
+
def chatbot_response(input_text, audio, sketch, chat_history=[]):
|
| 29 |
+
if "tell me a joke" in input_text.lower():
|
| 30 |
+
response = random.choice(jokes)
|
| 31 |
+
elif "weather in" in input_text.lower():
|
| 32 |
+
city = input_text.split("weather in")[-1].strip()
|
| 33 |
+
response = get_weather(city)
|
| 34 |
+
else:
|
| 35 |
+
conversation = " ".join([pair[0] + pair[1] for pair in chat_history]) + input_text
|
| 36 |
+
inputs = tokenizer.encode(conversation, return_tensors="pt")
|
| 37 |
+
reply_ids = model.generate(inputs, max_length=256, pad_token_id=tokenizer.eos_token_id)
|
| 38 |
+
response = tokenizer.decode(reply_ids[0], skip_special_tokens=True)
|
| 39 |
|
| 40 |
+
chat_history.append((input_text, response))
|
| 41 |
+
return chat_history, chat_history
|
| 42 |
+
|
| 43 |
+
# Custom CSS for a modern chat UI
|
| 44 |
+
custom_css = """
|
| 45 |
+
body {
|
| 46 |
+
background-color: #f5f5f5;
|
| 47 |
+
color: #333333;
|
| 48 |
+
font-family: Arial, sans-serif;
|
| 49 |
+
}
|
| 50 |
+
.gradio-container {
|
| 51 |
+
background-color: #ffffff;
|
| 52 |
+
border: 1px solid #dddddd;
|
| 53 |
+
border-radius: 8px;
|
| 54 |
+
max-width: 400px;
|
| 55 |
+
margin: auto;
|
| 56 |
+
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
| 57 |
+
}
|
| 58 |
+
.gr-chatbot-message {
|
| 59 |
+
padding: 10px 20px;
|
| 60 |
+
border-radius: 20px;
|
| 61 |
+
margin: 5px 0;
|
| 62 |
+
}
|
| 63 |
+
.gr-chatbot-message.user {
|
| 64 |
+
background-color: #007aff;
|
| 65 |
+
color: white;
|
| 66 |
+
text-align: right;
|
| 67 |
+
}
|
| 68 |
+
.gr-chatbot-message.bot {
|
| 69 |
+
background-color: #e5e5ea;
|
| 70 |
+
color: black;
|
| 71 |
+
text-align: left;
|
| 72 |
+
}
|
| 73 |
+
.gr-button, .gr-textbox {
|
| 74 |
+
background-color: #ffffff;
|
| 75 |
+
border: 1px solid #dddddd;
|
| 76 |
+
color: #333333;
|
| 77 |
+
border-radius: 20px;
|
| 78 |
+
padding: 10px 15px;
|
| 79 |
+
margin: 5px 0;
|
| 80 |
+
width: 100%;
|
| 81 |
+
}
|
| 82 |
+
"""
|
| 83 |
|
| 84 |
# Create the Gradio interface
|
| 85 |
+
with gr.Blocks(css=custom_css) as demo:
|
| 86 |
+
gr.Markdown("# Chat with GPT-Neo 2.7B")
|
| 87 |
+
chatbot_ui = gr.Chatbot(label="Chatbot")
|
| 88 |
+
input_box = gr.Textbox(show_label=False, placeholder="Type a message...", container=False)
|
| 89 |
+
audio_input = gr.Audio(source="microphone", type="filepath", label="Speak to the bot")
|
| 90 |
+
sketch_input = gr.Sketchpad()
|
| 91 |
+
chat_history = gr.State([])
|
| 92 |
+
|
| 93 |
+
def respond(message, audio, sketch, history):
|
| 94 |
+
history, new_history = chatbot_response(message, audio, sketch, history)
|
| 95 |
+
return new_history, new_history
|
| 96 |
+
|
| 97 |
+
input_box.submit(respond, [input_box, audio_input, sketch_input, chat_history], [chatbot_ui, chat_history])
|
| 98 |
+
audio_input.change(respond, [input_box, audio_input, sketch_input, chat_history], [chatbot_ui, chat_history])
|
| 99 |
+
sketch_input.submit(respond, [input_box, audio_input, sketch_input, chat_history], [chatbot_ui, chat_history])
|
| 100 |
+
|
| 101 |
+
demo.launch()
|