Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,20 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
import gradio as gr
|
| 4 |
-
|
| 5 |
-
import torch
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
-
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 11 |
|
|
|
|
| 12 |
def respond_to_message(message, chat_history):
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
chat_input += f"User: {user}\nBot: {bot}\n"
|
| 17 |
-
chat_input += f"User: {message}\nBot:"
|
| 18 |
-
|
| 19 |
-
# Encode input
|
| 20 |
-
input_ids = tokenizer.encode(chat_input, return_tensors="pt")
|
| 21 |
-
# Generate response
|
| 22 |
-
output = model.generate(
|
| 23 |
-
input_ids,
|
| 24 |
-
max_length=input_ids.shape[1] + 64,
|
| 25 |
-
pad_token_id=tokenizer.eos_token_id,
|
| 26 |
-
do_sample=True,
|
| 27 |
-
top_k=50,
|
| 28 |
-
top_p=0.95
|
| 29 |
)
|
| 30 |
-
|
| 31 |
-
chat_history.append((message,
|
| 32 |
return "", chat_history
|
| 33 |
|
|
|
|
| 34 |
with gr.Blocks() as demo:
|
| 35 |
chatbot = gr.Chatbot(label="AI चैट बोर्ड")
|
| 36 |
msg = gr.Textbox(label="आपका मैसेज")
|
|
@@ -40,6 +24,7 @@ with gr.Blocks() as demo:
|
|
| 40 |
|
| 41 |
demo.launch()
|
| 42 |
|
|
|
|
| 43 |
from transformers import GPT2Tokenizer, GPT2LMHeadModel
|
| 44 |
|
| 45 |
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import openai
|
|
|
|
| 3 |
|
| 4 |
+
# OpenAI API Key (यहाँ अपनी API Key डालें)
|
| 5 |
+
openai.api_key = "YOUR_API_KEY"
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# Backend Function: यूजर का मैसेज लेकर OpenAI से रिस्पॉन्स लाता है
|
| 8 |
def respond_to_message(message, chat_history):
|
| 9 |
+
response = openai.ChatCompletion.create(
|
| 10 |
+
model="gpt-3.5-turbo",
|
| 11 |
+
messages=[{"role": "user", "content": message}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
)
|
| 13 |
+
bot_message = response.choices[0].message['content']
|
| 14 |
+
chat_history.append((message, bot_message))
|
| 15 |
return "", chat_history
|
| 16 |
|
| 17 |
+
# Frontend: Gradio UI
|
| 18 |
with gr.Blocks() as demo:
|
| 19 |
chatbot = gr.Chatbot(label="AI चैट बोर्ड")
|
| 20 |
msg = gr.Textbox(label="आपका मैसेज")
|
|
|
|
| 24 |
|
| 25 |
demo.launch()
|
| 26 |
|
| 27 |
+
|
| 28 |
from transformers import GPT2Tokenizer, GPT2LMHeadModel
|
| 29 |
|
| 30 |
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|