File size: 2,740 Bytes
ea66d36 07a3e99 97e5eed 1d658d4 97e5eed 07a3e99 |
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
import gradio as gr
import openai
# OpenAI API Key (यहाँ अपनी API Key डालें)
openai.api_key = "YOUR_API_KEY"
# Backend Function: यूजर का मैसेज लेकर OpenAI से रिस्पॉन्स लाता है
def respond_to_message(message, chat_history):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": message}]
)
bot_message = response.choices[0].message['content']
chat_history.append((message, bot_message))
return "", chat_history
# Frontend: Gradio UI
with gr.Blocks() as demo:
chatbot = gr.Chatbot(label="AI चैट बोर्ड")
msg = gr.Textbox(label="आपका मैसेज")
clear = gr.ClearButton([msg, chatbot])
msg.submit(respond_to_message, [msg, chatbot], [msg, chatbot])
demo.launch()
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
# Model और Tokenizer लोड करें (आप चाहें तो कोई और चैट मॉडल भी ले सकते हैं)
model_name = "microsoft/DialoGPT-medium" # या "mistralai/Mistral-7B-Instruct-v0.2" (अगर Spaces पर चलता है)
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
def respond_to_message(message, chat_history):
# Chat history को एक स्ट्रिंग में जोड़ें
chat_input = ""
for user, bot in chat_history:
chat_input += f"User: {user}\nBot: {bot}\n"
chat_input += f"User: {message}\nBot:"
# Encode input
input_ids = tokenizer.encode(chat_input, return_tensors="pt")
# Generate response
output = model.generate(
input_ids,
max_length=input_ids.shape[1] + 64,
pad_token_id=tokenizer.eos_token_id,
do_sample=True,
top_k=50,
top_p=0.95
)
response = tokenizer.decode(output[0][input_ids.shape[1]:], skip_special_tokens=True)
chat_history.append((message, response.strip()))
return "", chat_history
with gr.Blocks() as demo:
chatbot = gr.Chatbot(label="AI चैट बोर्ड")
msg = gr.Textbox(label="आपका मैसेज")
clear = gr.ClearButton([msg, chatbot])
msg.submit(respond_to_message, [msg, chatbot], [msg, chatbot])
demo.launch()
from transformers import GPT2Tokenizer, GPT2LMHeadModel
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
model = GPT2LMHeadModel.from_pretrained("gpt2")
from datasets import load_dataset
# Login using e.g. `huggingface-cli login` to access this dataset
ds = load_dataset("KadamParth/NCERT_Chemistry_11th")
|