Prashanthsrn commited on
Commit
d0c8a21
·
verified ·
1 Parent(s): 1d3ed5b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -48
app.py CHANGED
@@ -1,52 +1,101 @@
1
  import gradio as gr
2
- from transformers import BloomForCausalLM, BloomTokenizerFast
3
- import torch
4
-
5
- # Load model and tokenizer
6
- model_name = "bigscience/bloom-1b7" # This is a 1.7B parameter model, adjust as needed
7
- tokenizer = BloomTokenizerFast.from_pretrained(model_name)
8
- model = BloomForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto")
9
-
10
- def generate_response(message, history):
11
- # Prepare the input
12
- prompt = f"Human: {message}\nAI:"
13
- input_ids = tokenizer.encode(prompt, return_tensors="pt").to(model.device)
14
-
15
- # Generate a response
16
- with torch.no_grad():
17
- output = model.generate(
18
- input_ids,
19
- max_new_tokens=100,
20
- do_sample=True,
21
- top_p=0.95,
22
- top_k=60,
23
- temperature=0.7,
24
- num_return_sequences=1,
25
- pad_token_id=tokenizer.eos_token_id,
26
- )
27
-
28
- # Decode the response
29
- response = tokenizer.decode(output[0], skip_special_tokens=True)
30
-
31
- # Extract only the AI's response
32
- ai_response = response.split("AI:")[-1].strip()
 
 
 
 
 
 
33
 
34
- return ai_response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
  # Create the Gradio interface
37
- iface = gr.ChatInterface(
38
- generate_response,
39
- chatbot=gr.Chatbot(height=400),
40
- textbox=gr.Textbox(placeholder="Type your message here...", container=False),
41
- title="Chat with BLOOM",
42
- description="A chatbot using the BLOOM model",
43
- theme="soft",
44
- examples=[["How are you?"], ["Tell me a joke"], ["What's the weather like?"]],
45
- cache_examples=True,
46
- retry_btn=None,
47
- undo_btn="Delete Previous",
48
- clear_btn="Clear",
49
- )
50
-
51
- # Launch the interface
52
- iface.launch()
 
 
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()