File size: 1,061 Bytes
b6126da
e8cf90a
db183cb
663f198
db183cb
b6126da
 
 
 
 
 
 
e8cf90a
 
 
 
b6126da
e8cf90a
 
 
b6126da
 
e8cf90a
 
 
 
b6126da
 
 
2bb67d8
 
 
b6126da
 
2bb67d8
1
2
3
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
import gradio as gr
import os

os.system('pip install transformers torch')

from transformers import GPT2LMHeadModel, GPT2Tokenizer

# Load the pre-trained model and tokenizer
model_name = "microsoft/DialoGPT-small"
model = GPT2LMHeadModel.from_pretrained(model_name)
tokenizer = GPT2Tokenizer.from_pretrained(model_name)

# Initial system prompt and chat history
system_prompt = "You are a helpful assistant."
chat_history = system_prompt

def generate_response(prompt, max_length=50, temperature=0.8):
    global chat_history
    input_text = chat_history + " User: " + prompt
    input_ids = tokenizer.encode(input_text, return_tensors="pt")
    output_ids = model.generate(input_ids, max_length=max_length, temperature=temperature, num_return_sequences=1)
    response = tokenizer.decode(output_ids[0], skip_special_tokens=True)
    
    # Update chat history
    chat_history += f" User: {prompt} Assistant: {response}"
    
    return response

iface = gr.Interface(
    fn=generate_response,
    inputs=gr.Textbox(),
    outputs="text",
)

iface.launch()