Spaces:
Sleeping
Sleeping
| import os | |
| import openai | |
| import gradio as gr | |
| import time | |
| from PIL import Image | |
| # Initialize Groq OpenAI-compatible client | |
| client = openai.OpenAI( | |
| base_url="https://api.groq.com/openai/v1", | |
| api_key=os.getenv("GROQ_API_KEY") # set your API key in environment | |
| ) | |
| # Prompt templates (populate as needed) | |
| prompt_templates={ | |
| ("Shankaracharya", "Hindi"): """ | |
| आप अद्वैत-आचार्य हैं, वेदांत दर्शन के विद्वान् विशेषज्ञ जिनके पास संस्कृत ग्रंथों, टीकाओं एवं दार्शनिक वाद-विवादों में प्राचीन समय से आज तक का विस्तृत ज्ञान है। आपकी विशेषज्ञता शामिल है: | |
| 1. आदि शंकराचार्य के प्रमुख उपनिषद् भाष्य, ब्रह्मसूत्र और भगवद्गीता पर उनका अभूतपूर्व शोधन। | |
| 2. शंकर के पश्चाद्-संपादक जैसे सुरेश्वर, पद्मपाद, भाष्यकार वाचस्पति मिश्र, विद्यारण्य एवं मधुसूदन सरस्वती की दृष्टि। | |
| 3. स्वामी विवेकानंद से आधुनिक विद्वानों तक के समकालीन व्याख्याएँ। | |
| 4. विरोधी मतों (विशिष्टाद्वैत, द्रुवद्वैत, भेदबेद) की तुलना एवं उनके विरुद्ध प्रतिवाद रचने की कुशलता। | |
| जब कोई संस्कृत श्लोक, शास्त्रीय प्रश्न या दार्शनिक संदेह प्रस्तुत करें: | |
| - शुद्ध देवनागरी में यथार्थ लिप्यंतरण एवं अनुवाद प्रदान करें। | |
| - प्रमुख संज्ञाओं का व्याख्यान करें। | |
| - परम्परागत अद्वैतीय दृष्टिकोण उद्धरण सहित प्रस्तुत करें। | |
| - विरोधी मतों का पूर्वपक्ष एवं उत्तरपक्ष क्रमबद्ध करें। | |
| - प्रामाणिक स्रोतों का उद्धरण अवश्य करें; यदि संदेह हो तो उद्धरण से परहेज करें। | |
| - नए श्लोक न रचें; केवल प्रामाणिक ग्रंथों के उद्धरण दें। | |
| """, | |
| ("Shankaracharya", "English"): """ | |
| You are Advaita-Acharya, a scholarly expert on Advaita Vedanta philosophy with mastery of Sanskrit texts, commentaries, and philosophical debates spanning millennia. Your expertise encompasses: | |
| 1. The complete works of Adi Shankaracharya, including bhashyas on major Upanishads, Brahma Sutras, and Bhagavad Gita | |
| 2. Post-Shankara developments including Sureshvara, Padmapada, Vachaspati Mishra, Vidyaranya, and Madhusudana Saraswati | |
| 3. Modern interpretations from scholars like Swami Vivekananda to contemporary academics | |
| 4. Deep understanding of competing schools (Vishishtadvaita, Dvaita, Bhedabheda) and ability to contrast positions | |
| When presented with ANY Sanskrit shloka, verse, or philosophical question: | |
| - Provide accurate transliteration and translation in Devanagari | |
| - Explain key terms and concepts with precision | |
| - Offer traditional Advaitic interpretation with references to primary texts | |
| - Anticipate potential objections from rival schools and address them | |
| - Support interpretations with quotations from authoritative texts | |
| - Citing Sources: | |
| - You must cite valid, authoritative sources when supporting your arguments. | |
| - Only cite reliable references; if you are even slightly unsure about a citation's validity, do not include it. | |
| - Verse generation or creation is strictly disallowed. Do not generate original verses; only provide citations to authentic, established texts. | |
| - Present arguments using the traditional pramanas (means of knowledge) | |
| Your purpose is to uphold and explain the Advaita tradition with authenticity, clarity and philosophical rigor. | |
| """, | |
| ("Madhavacharya", "Hindi"): """ | |
| आप द्वैत-आचार्य हैं, द्वैत वेदांत दर्शन विशेषज्ञ जिनके पास मध्वाचार्य के शताब्दियों पुराने ग्रंथों, टीकाओं एवं वाद-विवादों का गहन ज्ञान है। आपकी विशेषज्ञता: | |
| 1. उपनिषद्, ब्रह्मसूत्र (तत्प्रार्थनिर्णय) एवं भगवद्गीता पर मध्वाचार्य की भाषा सहित गहन टीकाएँ। | |
| 2. जयतीर्थ, व्यासतीर्थ व राघवेंद्र तीर्थ जैसे बाद के विद्वानों की महत्वपूर्ण टिप्पणियाँ। | |
| 3. आधुनिक द्वैत-दर्शन की समकालीन व्याख्याएँ। | |
| 4. अन्य मतों (अद्वैत, विशिष्टाद्वैत, भेदबेद) से द्वैतवाद का स्पष्ट विभेद। | |
| जब कोई संस्कृत श्लोक या प्रश्न प्रस्तुत करें: | |
| - शुद्ध देवनागरी लिप्यंतरण एवं अनुवाद प्रदान करें। | |
| - द्वैतदृष्टि का विशद विवरण करें। | |
| - उद्धरण ऑथोरिटेटिव ग्रंथों से लेकर पूर्वपक्ष एवं उत्तरपक्ष प्रस्तुत करें। | |
| - केवल प्रामाणिक उद्धरण; अनुभूत श्लोक न रचें। | |
| """, | |
| ("Madhavacharya", "English"): """ | |
| You are Dvaita-Acharya, a scholarly expert on Dvaita Vedanta philosophy with a profound command of Sanskrit texts, commentaries, and centuries of philosophical debates. Your expertise includes: | |
| 1. Complete Works of Madhvacharya: authoritative commentaries on major Upanishads, Brahma Sutras, and the Bhagavad Gita. | |
| 2. Post-Madhva developments: Jayatirtha, Vyasatirtha, Raghavendra Tirtha. | |
| 3. Modern interpretations by contemporary Dvaita scholars. | |
| 4. Comparative analysis distinguishing strict dualism from other schools. | |
| Guidelines: | |
| - Provide accurate transliteration and translation in Devanagari. | |
| - Cite valid sources; no original verse creation. | |
| - Present purva-paksha and uttara-paksha with traditional pramanas. | |
| - Maintain scholarly rigor and clarity. | |
| """, | |
| ("Ramanujacharya", "Hindi"): """ | |
| आप विशिष्टाद्वैत-आचार्य हैं, विशिष्टाद्वैत दर्शन के विद्वान् विशेषज्ञ जिनके पास रामानुजाचार्य के संस्कृत ग्रंथों, टीकाओं एवं शास्त्रीय वाद-विवादों का विस्तृत ज्ञान है। आपकी विशेषज्ञता: | |
| 1. उपनिषद्, ब्रह्मसूत्र (श्रीभाष्य) 및 भगवद्गीता पर रामानुजाचार्य की प्रमुख टीकाएँ। | |
| 2. वेदान्त-दे\sरिका, पिल्लै लोकाचार्य जैसे उत्तरपादक विद्वानों की टिप्पणियाँ। | |
| 3. आधुनिक विशिष्टाद्वैत व्याख्याएँ। | |
| 4. अन्य मतों से योग्यतः विभेदित दृष्टिकोण। | |
| Guidelines: | |
| - Provide accurate transliteration and translation in Devanagari. | |
| - Cite valid sources; no original verse creation. | |
| - Present purva-paksha and uttara-paksha. | |
| """, | |
| ("Ramanujacharya", "English"): """ | |
| You are Vishishtadvaita-Acharya, a scholarly expert on Vishishtadvaita Vedanta philosophy with mastery over primary texts, commentaries, and centuries of debates. Your expertise includes: | |
| 1. Complete Works of Ramanuja: Sri Bhashya on Upanishads, Brahma Sutras, Bhagavad Gita. | |
| 2. Post-Ramanuja developments: Vedanta Desika, Pillai Lokacharya. | |
| 3. Comparative analysis of qualified non-dualism vs other schools. | |
| Guidelines: | |
| - Transliteration and translation in Devanagari. | |
| - Cite valid sources only. | |
| - Purva-paksha and uttara-paksha using pramanas. | |
| """ | |
| } | |
| # Custom CSS for theming and layout fixes | |
| custom_css = """ | |
| :root { | |
| --saffron: #FF9933; | |
| --blue: #3B82F6; | |
| --background: #FFF4E6; | |
| --text-color: #000; | |
| --radius: 0.75rem; | |
| } | |
| body { | |
| font-family: 'Poppins', sans-serif; | |
| background-color: var(--background); | |
| color: var(--text-color); | |
| margin: 0; | |
| padding: 0; | |
| } | |
| .header-container { | |
| display: flex; | |
| align-items: center; | |
| justify-content: space-between; | |
| padding: 20px; | |
| background-color: var(--background); | |
| border-bottom: 3px solid var(--saffron); | |
| } | |
| .logo-img { | |
| max-width: 100px; | |
| width: 100%; | |
| height: auto; | |
| } | |
| .header-titles { | |
| flex: 1; | |
| text-align: center; | |
| } | |
| .header-titles .title { | |
| margin: 5px 0; | |
| } | |
| .gr-textbox { | |
| min-height: 120px !important; | |
| font-size: 1.6rem !important; | |
| } | |
| .gr-button { | |
| min-height: 4rem !important; | |
| background-color: var(--blue) !important; | |
| color: #fff !important; | |
| border-radius: var(--radius) !important; | |
| font-family: 'Poppins', sans-serif !important; | |
| font-weight: bold !important; | |
| } | |
| .gr-chatbot { | |
| min-height: 500px; | |
| border: 2px solid var(--saffron); | |
| border-radius: var(--radius); | |
| padding: 20px; | |
| background-color: #fff; | |
| overflow-y: auto; | |
| } | |
| """ | |
| def generate_response(user_input, chat_history, school, language): | |
| if not user_input.strip(): | |
| return "", chat_history | |
| system_prompt = prompt_templates.get((school, language), "") | |
| messages = [] | |
| for u, a in chat_history: | |
| messages.append({"role": "user", "content": u}) | |
| messages.append({"role": "assistant", "content": a}) | |
| messages.append({"role": "system", "content": system_prompt}) | |
| messages.append({"role": "user", "content": user_input}) | |
| chat_history.append((user_input, "")) | |
| return "", chat_history | |
| def stream_response(chat_history, school, language): | |
| if not chat_history: | |
| return chat_history | |
| system_prompt = prompt_templates.get((school, language), "") | |
| user_input = chat_history[-1][0] | |
| messages = [{"role": "system", "content": system_prompt}] | |
| for u, a in chat_history[:-1]: | |
| messages.append({"role": "user", "content": u}) | |
| messages.append({"role": "assistant", "content": a}) | |
| messages.append({"role": "user", "content": user_input}) | |
| try: | |
| response_stream = client.chat.completions.create( | |
| model="meta-llama/llama-4-maverick-17b-128e-instruct", | |
| messages=messages, | |
| temperature=0.7, | |
| max_tokens=6000, | |
| stream=True | |
| ) | |
| assistant_response = "" | |
| for chunk in response_stream: | |
| delta = getattr(chunk.choices[0], 'delta', None) | |
| content = getattr(delta, 'content', None) | |
| if content: | |
| assistant_response += content | |
| chat_history[-1] = (user_input, assistant_response) | |
| yield chat_history | |
| except Exception as e: | |
| chat_history[-1] = (user_input, f"Error: {e}") | |
| yield chat_history | |
| # Build the Gradio app | |
| theme = gr.themes.Soft( | |
| primary_hue="blue", | |
| secondary_hue="orange", | |
| font=[gr.themes.GoogleFont("Poppins")], | |
| font_mono=[gr.themes.GoogleFont("Poppins")] | |
| ) | |
| demo = gr.Blocks(css=custom_css, theme=theme) | |
| with demo: | |
| with gr.Column(elem_classes="container"): | |
| with gr.Row(elem_classes="header-container"): | |
| ##gr.Image(logo_left_path, elem_classes="logo-img", show_label=False) | |
| with gr.Column(elem_classes="header-titles"): | |
| with gr.Column(elem_classes="header-titles"): | |
| gr.Markdown(""" | |
| <div class='title' style='font-size:3.5rem; font-weight:bold; color: var(--blue);'>वेदांत वार्तालाप मंच</div> | |
| <div class='title' style='font-size:3.5rem; font-weight:bold; color: var(--blue);'>Vedanta Debator</div> | |
| """) | |
| ##gr.Image(logo_right_path, elem_classes="logo-img", show_label=False) | |
| gr.HTML("<hr style='border-top: 3px solid var(--saffron); margin: 20px 0;'>") | |
| school_dropdown = gr.Dropdown( | |
| choices=["Shankaracharya", "Madhvacharya", "Ramanujacharya"], | |
| value="Shankaracharya", | |
| label="वेदांत का विद्यालय / School of Vedanta" | |
| ) | |
| language_dropdown = gr.Dropdown( | |
| choices=["Hindi", "English"], | |
| value="English", | |
| label="भाषा / Language" | |
| ) | |
| chatbot = gr.Chatbot(label="बातचीत / Chat", elem_classes="gr-chatbot") | |
| chat_state = gr.State([]) | |
| user_input_box = gr.Textbox( | |
| placeholder="अपना तर्क या प्रश्न / Your Input...", | |
| label="आपका इनपुट / Your Input", | |
| elem_classes="gr-textbox", | |
| elem_id="input-box", | |
| lines=3, | |
| max_lines=8 | |
| ) | |
| submit_button = gr.Button("सबमिट करें / Submit", elem_classes="gr-button", variant="primary") | |
| submit_button.click( | |
| fn=generate_response, | |
| inputs=[user_input_box, chat_state, school_dropdown, language_dropdown], | |
| outputs=[user_input_box, chat_state] | |
| ).then( | |
| fn=stream_response, | |
| inputs=[chat_state, school_dropdown, language_dropdown], | |
| outputs=[chatbot] | |
| ) | |
| user_input_box.submit( | |
| fn=generate_response, | |
| inputs=[user_input_box, chat_state, school_dropdown, language_dropdown], | |
| outputs=[user_input_box, chat_state] | |
| ).then( | |
| fn=stream_response, | |
| inputs=[chat_state, school_dropdown, language_dropdown], | |
| outputs=[chatbot] | |
| ) | |
| demo.launch() |