Bagda commited on
Commit
97e5eed
·
verified ·
1 Parent(s): 07a3e99

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -0
app.py CHANGED
@@ -24,6 +24,47 @@ with gr.Blocks() as demo:
24
 
25
  demo.launch()
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  from datasets import load_dataset
28
 
29
  # Login using e.g. `huggingface-cli login` to access this dataset
 
24
 
25
  demo.launch()
26
 
27
+ import gradio as gr
28
+ from transformers import AutoModelForCausalLM, AutoTokenizer
29
+ import torch
30
+
31
+ # Model और Tokenizer लोड करें (आप चाहें तो कोई और चैट मॉडल भी ले सकते हैं)
32
+ model_name = "microsoft/DialoGPT-medium" # या "mistralai/Mistral-7B-Instruct-v0.2" (अगर Spaces पर चलता है)
33
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
34
+ model = AutoModelForCausalLM.from_pretrained(model_name)
35
+
36
+ def respond_to_message(message, chat_history):
37
+ # Chat history को एक स्ट्रिंग में जोड़ें
38
+ chat_input = ""
39
+ for user, bot in chat_history:
40
+ chat_input += f"User: {user}\nBot: {bot}\n"
41
+ chat_input += f"User: {message}\nBot:"
42
+
43
+ # Encode input
44
+ input_ids = tokenizer.encode(chat_input, return_tensors="pt")
45
+ # Generate response
46
+ output = model.generate(
47
+ input_ids,
48
+ max_length=input_ids.shape[1] + 64,
49
+ pad_token_id=tokenizer.eos_token_id,
50
+ do_sample=True,
51
+ top_k=50,
52
+ top_p=0.95
53
+ )
54
+ response = tokenizer.decode(output[0][input_ids.shape[1]:], skip_special_tokens=True)
55
+ chat_history.append((message, response.strip()))
56
+ return "", chat_history
57
+
58
+ with gr.Blocks() as demo:
59
+ chatbot = gr.Chatbot(label="AI चैट बोर्ड")
60
+ msg = gr.Textbox(label="आपका मैसेज")
61
+ clear = gr.ClearButton([msg, chatbot])
62
+
63
+ msg.submit(respond_to_message, [msg, chatbot], [msg, chatbot])
64
+
65
+ demo.launch()
66
+
67
+
68
  from datasets import load_dataset
69
 
70
  # Login using e.g. `huggingface-cli login` to access this dataset