Update app.py
Browse files
app.py
CHANGED
|
@@ -1,57 +1,37 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
-
import time
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
chatbot = pipeline("conversational", model=model_name)
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
theme = gr.themes.Soft(
|
| 15 |
-
primary_hue="teal",
|
| 16 |
-
secondary_hue="teal",
|
| 17 |
-
neutral_hue="slate",
|
| 18 |
-
font=["Roboto", "ui-sans-serif", "sans-serif"],
|
| 19 |
-
font_mono=["Roboto Mono", "ui-monospace", "monospace"]
|
| 20 |
-
)
|
| 21 |
-
|
| 22 |
-
# चैट फंक्शन
|
| 23 |
-
def chat_with_ai(message, chat_history):
|
| 24 |
-
# सिस्टम प्रॉम्प्ट को चैट हिस्ट्री में जोड़ें (यदि पहली बार चैट कर रहे हैं)
|
| 25 |
-
if not chat_history:
|
| 26 |
-
chat_history.append((None, "नमस्ते! मैं आपकी कैसे मदद कर सकता हूँ?"))
|
| 27 |
|
| 28 |
-
# AI
|
| 29 |
-
|
| 30 |
-
|
| 31 |
|
| 32 |
-
# चैट हिस्ट्री अपडेट करें
|
| 33 |
-
chat_history.append((
|
| 34 |
return "", chat_history
|
| 35 |
|
| 36 |
-
# साफ़ करने का फंक्शन
|
| 37 |
def clear_chat():
|
| 38 |
return [], []
|
| 39 |
|
| 40 |
-
# एडवांस्ड
|
| 41 |
-
with gr.Blocks(
|
| 42 |
-
gr.Markdown("#
|
| 43 |
-
gr.Markdown("यहां
|
| 44 |
-
|
| 45 |
-
chatbot = gr.Chatbot(label="चैट", height=500)
|
| 46 |
-
msg = gr.Textbox(label="आपका सवाल", placeholder="यहां लिखें...")
|
| 47 |
-
clear = gr.Button("साफ़ करें", variant="secondary")
|
| 48 |
-
|
| 49 |
-
msg.submit(chat_with_ai, [msg, chatbot], [msg, chatbot])
|
| 50 |
-
clear.click(clear_chat, None, chatbot)
|
| 51 |
-
|
| 52 |
-
# फुटर/क्रेडिट
|
| 53 |
-
gr.Markdown("---")
|
| 54 |
-
gr.Markdown("**बनाया गया: Gradio + Hugging Face + Transformers**")
|
| 55 |
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
|
|
|
| 3 |
|
| 4 |
+
# Hugging Face का कोई भी चैट मॉडल लोड करें (यहाँ facebook/blenderbot-400M-distill उदाहरण है)
|
| 5 |
+
model = pipeline("conversational", model="facebook/blenderbot-400M-distill")
|
|
|
|
| 6 |
|
| 7 |
+
# यह फंक्शन AI को प्रश्न भेजता है और उत्तर लेता है
|
| 8 |
+
def qna_ai(question, chat_history):
|
| 9 |
+
# चैट हिस्ट्री को सिर्फ प्रश्न-उत्तर के फॉर्मेट में रखें
|
| 10 |
+
formatted_history = "\n".join([f"User: {q}\nAI: {a}" for q, a in chat_history])
|
| 11 |
+
prompt = f"{formatted_history}\nUser: {question}\nAI:"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# AI से जवाब लें
|
| 14 |
+
response = model(prompt)
|
| 15 |
+
ai_answer = response.generated_responses[-1]
|
| 16 |
|
| 17 |
+
# चैट हिस्ट्री को अपडेट करें
|
| 18 |
+
chat_history.append((question, ai_answer))
|
| 19 |
return "", chat_history
|
| 20 |
|
| 21 |
+
# चैट हिस्ट्री साफ़ करने का फंक्शन
|
| 22 |
def clear_chat():
|
| 23 |
return [], []
|
| 24 |
|
| 25 |
+
# एडवांस्ड चैट इंटरफेस बनाएं
|
| 26 |
+
with gr.Blocks(title="छात्र सहायक AI", theme=gr.themes.Soft()) as demo:
|
| 27 |
+
gr.Markdown("# 🎓 छात्रों के लिए प्रश्न-उत्तर AI")
|
| 28 |
+
gr.Markdown("यहां अपना प्रश्न लिखें और AI आपको उत्तर देगा!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
chatbot = gr.Chatbot(label="चैट", height=400)
|
| 31 |
+
msg = gr.Textbox(label="प्रश्न", placeholder="यहां लिखें...")
|
| 32 |
+
clear = gr.Button("साफ़ करें")
|
| 33 |
+
|
| 34 |
+
msg.submit(qna_ai, [msg, chatbot], [msg, chatbot])
|
| 35 |
+
clear.click(clear_chat, None, chatbot)
|
| 36 |
|
| 37 |
+
demo.launch()
|