File size: 1,417 Bytes
8dae174
ff195b8
d55c517
8dae174
ff195b8
d948d75
 
 
ff195b8
 
 
d55c517
 
 
ff195b8
 
 
 
 
 
 
 
 
d55c517
 
 
 
9111270
 
 
 
 
ff195b8
 
9111270
d55c517
9111270
8dae174
d55c517
 
ff195b8
 
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
36
37
38
39
40
41
42
from transformers import pipeline
import gradio as gr

# Load the pipeline for text generation
try:
 #   generator = pipeline("text-generation", model="microsoft/DialoGPT-small")
    generator = pipeline("text-generation", model="microsoft/DialoGPT-medium")

except Exception as e:
    print(f"Error loading the model: {e}")
    raise

# Function to generate a response
def dialoGPT_response(user_input, history):
    try:
        conversation = [{"role": "user", "content": user_input}] if history is None else history + [{"role": "user", "content": user_input}]
        response = generator(conversation, return_full_text=False, max_length=1000)
        assistant_response = response[0]['generated_text']
        new_history = conversation + [{"role": "assistant", "content": assistant_response}]
        return assistant_response, new_history
    except Exception as e:
        print(f"Error generating response: {e}")
        return "An error occurred while generating a response.", history

# Gradio interface
iface = gr.Interface(
    fn=dialoGPT_response,
    inputs=[
        gr.Textbox(placeholder="Enter your message..."),
        "state"
    ],
    outputs=[
        "text",  
        "state"  
    ],
    title="DialoGPT Chat",
    description="Chat with DialoGPT-small model. Your conversation history is maintained.",
    allow_flagging="never"
)

if __name__ == "__main__":
    iface.launch(debug=True)