vangru commited on
Commit
f38f1b3
·
verified ·
1 Parent(s): b97dfff

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import torch
4
+
5
+ # Load model and tokenizer
6
+ model_name = "microsoft/DialoGPT-medium"
7
+
8
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
9
+ model = AutoModelForCausalLM.from_pretrained(model_name)
10
+
11
+ # Store conversation history
12
+ chat_history_ids = None
13
+
14
+ def respond(message, history):
15
+ global chat_history_ids
16
+
17
+ # Encode user input
18
+ new_input_ids = tokenizer.encode(message + tokenizer.eos_token, return_tensors='pt')
19
+
20
+ # Append to chat history
21
+ if chat_history_ids is not None:
22
+ bot_input_ids = torch.cat([chat_history_ids, new_input_ids], dim=-1)
23
+ else:
24
+ bot_input_ids = new_input_ids
25
+
26
+ # Generate response
27
+ chat_history_ids = model.generate(
28
+ bot_input_ids,
29
+ max_length=1000,
30
+ pad_token_id=tokenizer.eos_token_id,
31
+ do_sample=True,
32
+ top_k=100,
33
+ top_p=0.7,
34
+ temperature=0.8
35
+ )
36
+
37
+ # Decode response
38
+ response = tokenizer.decode(
39
+ chat_history_ids[:, bot_input_ids.shape[-1]:][0],
40
+ skip_special_tokens=True
41
+ )
42
+
43
+ return response
44
+
45
+ # Create Gradio interface
46
+ demo = gr.ChatInterface(
47
+ fn=respond,
48
+ title="Dialogue System using DialoGPT",
49
+ description="A simple conversational AI built with HuggingFace Transformers and Gradio."
50
+ )
51
+
52
+ if __name__ == "__main__":
53
+ demo.launch()