Spaces:
Sleeping
Sleeping
Update run.py
Browse files
run.py
CHANGED
|
@@ -1,12 +1,27 @@
|
|
| 1 |
import time
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
yield "You typed: " + message[: i+1]
|
| 8 |
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
if __name__ == "__main__":
|
| 12 |
demo.launch()
|
|
|
|
| 1 |
import time
|
| 2 |
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
| 5 |
|
| 6 |
+
# Load pre-trained model and tokenizer
|
| 7 |
+
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
| 8 |
+
model = GPT2LMHeadModel.from_pretrained("gpt2")
|
|
|
|
| 9 |
|
| 10 |
+
def generate_response(user_input, max_length=50):
|
| 11 |
+
# Tokenize user input and convert to tensor
|
| 12 |
+
input_ids = tokenizer.encode(user_input, return_tensors="pt")
|
| 13 |
+
|
| 14 |
+
# Generate response using the model
|
| 15 |
+
output = model.generate(input_ids, max_length=max_length, num_return_sequences=1)
|
| 16 |
+
|
| 17 |
+
# Decode the generated response
|
| 18 |
+
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 19 |
+
return response
|
| 20 |
+
|
| 21 |
+
def resposeYielder(message, history):
|
| 22 |
+
yield generate_response(message)
|
| 23 |
+
|
| 24 |
+
demo = gr.ChatInterface(resposeYielder).queue()
|
| 25 |
|
| 26 |
if __name__ == "__main__":
|
| 27 |
demo.launch()
|