Emalawi19 commited on
Commit
7596b9d
·
verified ·
1 Parent(s): d653f4d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -0
app.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import torch
4
+
5
+ MODEL_NAME = "microsoft/DialoGPT-medium"
6
+
7
+ print("🔄 Loading model from Hugging Face...")
8
+
9
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
10
+ model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
11
+
12
+ print("✅ Model loaded!")
13
+
14
+ chat_history_ids = None
15
+
16
+ def chat(user_input):
17
+ global chat_history_ids
18
+
19
+ new_input_ids = tokenizer.encode(
20
+ user_input + tokenizer.eos_token,
21
+ return_tensors='pt'
22
+ )
23
+
24
+ if chat_history_ids is not None:
25
+ bot_input_ids = torch.cat([chat_history_ids, new_input_ids], dim=-1)
26
+ else:
27
+ bot_input_ids = new_input_ids
28
+
29
+ chat_history_ids = model.generate(
30
+ bot_input_ids,
31
+ max_length=1000,
32
+ pad_token_id=tokenizer.eos_token_id,
33
+ do_sample=True,
34
+ top_k=50,
35
+ top_p=0.95,
36
+ temperature=0.75
37
+ )
38
+
39
+ response = tokenizer.decode(
40
+ chat_history_ids[:, bot_input_ids.shape[-1]:][0],
41
+ skip_special_tokens=True
42
+ )
43
+
44
+ return response
45
+
46
+
47
+ iface = gr.Interface(
48
+ fn=chat,
49
+ inputs="text",
50
+ outputs="text",
51
+ title="Emalawi19 AI 🤖",
52
+ description="Chat with your AI powered by Hugging Face"
53
+ )
54
+
55
+ iface.launch()