Spaces:
Running
Running
Upload app.py with huggingface_hub
Browse files12323456789891010111213141516171819202122232425262728293031323334353637381213141539404142434445464717484950515253545556575859606162636465666768697019202171727374752324252627282930317677787980818283848586878889909192939495333435363796979899100101102103394010410510610710810911011111211311411511611711842119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231"""Devrayog AI - Minimal App for Hugging Face SpacesUses only pre-installed dependenciesDevrayog AI - Gradio App with Real AI ResponsesUses Hugging Face Inference API for actual model responses"""
import gradio as grimport requestsimport os
def chat(message, model_name): if not message.strip(): return "Please enter a message."HF_TOKEN = os.environ.get("HF_TOKEN", "")
# Model endpoints on Hugging FaceMODELS = { "Devrayog Agni (Fast)": { "id": "microsoft/Phi-3-mini-4k-instruct", "description": "Fast and sharp - optimized for quick responses" }, "Devrayog Vayu (Creative)": { "id": "google/gemma-2-9b-it", "description": "Creative and flowing - optimized for storytelling" }, "Devrayog Akash (Powerful)": { "id": "meta-llama/Llama-3.1-8B-Instruct", "description": "Most powerful - optimized for complex reasoning" }}
API_URL = "https://api-inference.huggingface.co/models/"
def query_hf_model(model_id, message, system_prompt=""): """Query Hugging Face Inference API for real AI response""" headers = {"Authorization": f"Bearer {HF_TOKEN}"} if HF_TOKEN else {} if system_prompt: prompt = f"[INST] {system_prompt}\n\n{message} [/INST]" else: prompt = f"[INST] {message} [/INST]" responses = { "Devrayog Agni (Fast)": f"๐ฅ [Devrayog Agni]\n\nMessage: '{message}'\n\nI'm Agni, the fast model. I help with quick answers, coding, daily tasks, and SSB prep. How can I assist you?", "Devrayog Vayu (Creative)": f"๐ [Devrayog Vayu]\n\nMessage: '{message}'\n\nI'm Vayu, the creative model. I excel at storytelling, brainstorming, and content generation.", "Devrayog Akash (Powerful)": f"๐ [Devrayog Akash]\n\nAnalyzing: '{message}'\n\nI'm Akash, the most powerful model. I handle complex reasoning, analysis, and research." payload = { "inputs": prompt, "parameters": { "max_new_tokens": 512, "temperature": 0.7, "top_p": 0.9, "return_full_text": False } } return responses.get(model_name, responses["Devrayog Agni (Fast)"]) try: response = requests.post( f"{API_URL}{model_id}", headers=headers, json=payload, timeout=30 ) if response.status_code == 200: result = response.json() if isinstance(result, list) and len(result) > 0: return result[0].get("generated_text", "No response generated.") return str(result) elif response.status_code == 503: return f"โณ Model is loading... Please try again in a few seconds." else: return f"โ ๏ธ Error {response.status_code}: {response.text[:200]}" except requests.exceptions.Timeout: return "โฐ Request timed out. The model might be loading. Please try again." except Exception as e: return f"โ Error: {str(e)}"
with gr.Blocks(title="Devrayog AI") as demo: gr.Markdown("# ๐ฎ๐ณ Devrayog AI") gr.Markdown("*India's First Solo-Built AI - By Devanshu Sharma (Hermes), Age 18, Rajasthan*")def chat(message, model_name, system_prompt, history): """Chat function with real AI responses""" if not message.strip(): return history, "" msg = gr.Textbox(label="Your Message", placeholder="Type here...", lines=3) model = gr.Dropdown( choices=["Devrayog Agni (Fast)", "Devrayog Vayu (Creative)", "Devrayog Akash (Powerful)"], value="Devrayog Agni (Fast)", label="Select Model" ) btn = gr.Button("Send ๐", variant="primary") output = gr.Textbox(label="Response", lines=10, interactive=False) btn.click(fn=chat, inputs=[msg, model], outputs=output) model_info = MODELS.get(model_name, MODELS["Devrayog Agni (Fast)"]) model_id = model_info["id"] # Add user message to history history = history or [] history.append(("user", message)) # Get AI response response = query_hf_model(model_id, message, system_prompt) # Add AI response to history history.append(("assistant", response)) return history, ""
def format_chat(history): """Format chat history for display""" if not history: return "" gr.Markdown("""### Models- ๐ฅ **Devrayog Agni** - Fast (3.8B params)- ๐ **Devrayog Vayu** - Creative (9B params)- ๐ **Devrayog Akash** - Powerful (8B params) formatted = "" for role, message in history: if role == "user": formatted += f"๐ค You: {message}\n\n" else: formatted += f"๐ค Devrayog AI: {message}\n\n" return formatted
**Founder:** Devanshu Sharma (Hermes), 18, Neem Ka Thana, Rajasthan**Tagline:** India's First Solo-Built AI# Custom CSS for WhatsApp-like UIcustom_css = """.gradio-container {max-width: 800px !important; margin: auto !important;}.main-title {text-align: center; background: linear-gradient(135deg, #FF6B35, #7B2FFF); padding: 15px; border-radius: 10px; margin-bottom: 15px;}.main-title h1 {color: white !important; margin: 0 !important; font-size: 1.5rem !important;}.main-title p {color: rgba(255,255,255,0.9) !important; margin: 5px 0 0 0 !important; font-size: 0.85rem !important;}.chat-container {background: #0b141a !important; border-radius: 10px !important; padding: 15px !important;}.user-msg {background: #005c4b !important; color: white !important; padding: 10px 15px !important; border-radius: 15px 15px 0 15px !important; margin: 5px 0 5px auto !important; max-width: 70% !important; text-align: right !important;}.ai-msg {background: #1f2c34 !important; color: #e9edef !important; padding: 10px 15px !important; border-radius: 15px 15px 15px 0 !important; margin: 5px auto 5px 0 !important; max-width: 70% !important;}.input-row {display: flex !important; gap: 10px !important; margin-top: 10px !important;}.input-box {flex: 1 !important;}.send-btn {background: #FF6B35 !important; border: none !important; border-radius: 50% !important; width: 50px !important; height: 50px !important; cursor: pointer !important;}.send-btn:hover {background: #e55a2b !important;}"""
๐ Hugging Face: https://huggingface.co/dev2089with gr.Blocks(title="Devrayog AI", css=custom_css) as demo: # Header gr.HTML(""" <div class="main-title"> <h1>๐ฎ๐ณ Devrayog AI</h1> <p>India's First Solo-Built AI | Founded by Devanshu Sharma (Hermes), Age 18, Rajasthan</p> </div> """) with gr.Row(): with gr.Column(scale=4): # Model selector model_select = gr.Dropdown( choices=list(MODELS.keys()), value="Devrayog Agni (Fast)", label="Select Model", interactive=True ) # System prompt (collapsible) with gr.Accordion("โ๏ธ Advanced Settings", open=False): sys_prompt = gr.Textbox( label="System Prompt", placeholder="Set a custom system prompt for the AI...", lines=2, value="You are Devrayog AI, India's first solo-built AI created by Devanshu Sharma (Hermes), an 18-year-old from Neem Ka Thana, Rajasthan. You are helpful, knowledgeable, and proud of your Indian roots. You specialize in SSB interview preparation, general knowledge, and coding help." ) # Chat display chat_display = gr.Textbox( label="Chat", lines=20, interactive=False, elem_classes=["chat-container"], value="๐ค Welcome to Devrayog AI! ๐ฎ๐ณ\n\nI'm India's first solo-built AI. I can help you with:\nโข General questions and knowledge\nโข SSB interview preparation\nโข Coding and technical help\nโข Creative writing and brainstorming\nโข Current affairs and more\n\nSelect a model above and start chatting!\n\nโ\nFounder: Devanshu Sharma (Hermes), Age 18\nFrom: Neem Ka Thana, Rajasthan" ) # Input row with gr.Row(): msg_input = gr.Textbox( label="", placeholder="Type your message here...", lines=1, scale=4, container=False ) send_btn = gr.Button("โค", scale=1, variant="primary") with gr.Column(scale=1): gr.Markdown("""### ๐ Models
**๐ฅ Agni (Fast)**3.8B paramsQuick responses, coding, daily tasks
**๐ Vayu (Creative)**9B paramsStorytelling, brainstorming, content
**๐ Akash (Powerful)**8B paramsComplex reasoning, analysis, research """) gr.Markdown("""### ๐ฏ SSB Prep Tips- Practice OIR daily- Write 6 TAT stories/day- Current affairs updates- Physical fitness- Be confident & authentic """) gr.Markdown("""### ๐ Links- HF: huggingface.co/dev2089- Founder: Hermes, 18, Rajasthan """) # Hidden state for chat history chat_state = gr.State([]) # Event handlers def on_send(message, model, prompt, history): if not message.strip(): return format_chat(history) if history else "", "", history model_info = MODELS.get(model, MODELS["Devrayog Agni (Fast)"]) model_id = model_info["id"] history = history or [] history.append(("user", message)) response = query_hf_model(model_id, message, prompt) history.append(("assistant", response)) return format_chat(history), "", history send_btn.click( fn=on_send, inputs=[msg_input, model_select, sys_prompt, chat_state], outputs=[chat_display, msg_input, chat_state] ) msg_input.submit( fn=on_send, inputs=[msg_input, model_select, sys_prompt, chat_state], outputs=[chat_display, msg_input, chat_state] )
demo.launch(server_name="0.0.0.0", server_port=7860)app.py+216-30