Spaces:
Sleeping
Sleeping
Commit ·
f60e304
1
Parent(s): 5a39748
Upload 2 files
Browse files- app.py +57 -4
- requirements.txt +5 -0
app.py
CHANGED
|
@@ -1,7 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
iface.launch()
|
|
|
|
| 1 |
+
# import gradio as gr
|
| 2 |
+
|
| 3 |
+
# def greet(name):
|
| 4 |
+
# return "Hello " + name + "!!"
|
| 5 |
+
|
| 6 |
+
# iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 7 |
+
# iface.launch()
|
| 8 |
+
|
| 9 |
import gradio as gr
|
| 10 |
+
import torch
|
| 11 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 12 |
+
|
| 13 |
+
# Load the pre-trained model and tokenizer
|
| 14 |
+
model_name = 'microsoft/DialoGPT-medium'
|
| 15 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 16 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 17 |
+
|
| 18 |
+
# Set the device to GPU if available, otherwise use CPU
|
| 19 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 20 |
+
model = model.to(device)
|
| 21 |
+
|
| 22 |
+
# Define a function to generate a response given a list of user inputs
|
| 23 |
+
def generate_response(user_inputs):
|
| 24 |
+
# Tokenize the user inputs
|
| 25 |
+
input_ids = tokenizer.encode(user_inputs, return_tensors='pt').to(device)
|
| 26 |
+
|
| 27 |
+
# Generate a response
|
| 28 |
+
with torch.no_grad():
|
| 29 |
+
output = model.generate(input_ids, max_length=100, num_return_sequences=1)
|
| 30 |
+
|
| 31 |
+
# Decode the generated output
|
| 32 |
+
response = tokenizer.decode(output[:, input_ids.shape[-1]:][0], skip_special_tokens=True)
|
| 33 |
+
return response
|
| 34 |
+
|
| 35 |
+
# Define the chatbot function
|
| 36 |
+
def chatbot(input_text):
|
| 37 |
+
# Append user input to the chat history
|
| 38 |
+
history = [input_text]
|
| 39 |
+
|
| 40 |
+
# Generate a response
|
| 41 |
+
response = generate_response(history)
|
| 42 |
+
|
| 43 |
+
# Append the user input and generated response to the chat history
|
| 44 |
+
history.append(response)
|
| 45 |
+
|
| 46 |
+
# Return the response
|
| 47 |
+
return response
|
| 48 |
|
| 49 |
+
# Set up the Gradio interface
|
| 50 |
+
iface = gr.Interface(
|
| 51 |
+
fn=chatbot,
|
| 52 |
+
inputs=gr.inputs.Textbox(placeholder="Enter your message..."),
|
| 53 |
+
outputs="text",
|
| 54 |
+
title="Conversational Chatbot",
|
| 55 |
+
description="An AI-powered chatbot that engages in conversation.",
|
| 56 |
+
theme="default"
|
| 57 |
+
)
|
| 58 |
|
| 59 |
+
# Launch the Gradio interface
|
| 60 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas
|
| 2 |
+
numpy
|
| 3 |
+
torch
|
| 4 |
+
gradio
|
| 5 |
+
transformers
|