Guines / app.py
Bagda's picture
Update app.py
1d658d4 verified
raw
history blame
2.74 kB
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")