Spaces:
Running
Running
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,45 +1,231 @@
|
|
| 1 |
"""
|
| 2 |
-
Devrayog AI -
|
| 3 |
-
Uses
|
| 4 |
"""
|
| 5 |
|
| 6 |
import gradio as gr
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
"
|
| 14 |
-
"
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
}
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
)
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
"""
|
| 2 |
+
Devrayog AI - Gradio App with Real AI Responses
|
| 3 |
+
Uses Hugging Face Inference API for actual model responses
|
| 4 |
"""
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
+
import requests
|
| 8 |
+
import os
|
| 9 |
|
| 10 |
+
HF_TOKEN = os.environ.get("HF_TOKEN", "")
|
| 11 |
+
|
| 12 |
+
# Model endpoints on Hugging Face
|
| 13 |
+
MODELS = {
|
| 14 |
+
"Devrayog Agni (Fast)": {
|
| 15 |
+
"id": "microsoft/Phi-3-mini-4k-instruct",
|
| 16 |
+
"description": "Fast and sharp - optimized for quick responses"
|
| 17 |
+
},
|
| 18 |
+
"Devrayog Vayu (Creative)": {
|
| 19 |
+
"id": "google/gemma-2-9b-it",
|
| 20 |
+
"description": "Creative and flowing - optimized for storytelling"
|
| 21 |
+
},
|
| 22 |
+
"Devrayog Akash (Powerful)": {
|
| 23 |
+
"id": "meta-llama/Llama-3.1-8B-Instruct",
|
| 24 |
+
"description": "Most powerful - optimized for complex reasoning"
|
| 25 |
+
}
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
API_URL = "https://api-inference.huggingface.co/models/"
|
| 29 |
+
|
| 30 |
+
def query_hf_model(model_id, message, system_prompt=""):
|
| 31 |
+
"""Query Hugging Face Inference API for real AI response"""
|
| 32 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"} if HF_TOKEN else {}
|
| 33 |
+
|
| 34 |
+
if system_prompt:
|
| 35 |
+
prompt = f"[INST] {system_prompt}\n\n{message} [/INST]"
|
| 36 |
+
else:
|
| 37 |
+
prompt = f"[INST] {message} [/INST]"
|
| 38 |
|
| 39 |
+
payload = {
|
| 40 |
+
"inputs": prompt,
|
| 41 |
+
"parameters": {
|
| 42 |
+
"max_new_tokens": 512,
|
| 43 |
+
"temperature": 0.7,
|
| 44 |
+
"top_p": 0.9,
|
| 45 |
+
"return_full_text": False
|
| 46 |
+
}
|
| 47 |
}
|
| 48 |
+
|
| 49 |
+
try:
|
| 50 |
+
response = requests.post(
|
| 51 |
+
f"{API_URL}{model_id}",
|
| 52 |
+
headers=headers,
|
| 53 |
+
json=payload,
|
| 54 |
+
timeout=30
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
if response.status_code == 200:
|
| 58 |
+
result = response.json()
|
| 59 |
+
if isinstance(result, list) and len(result) > 0:
|
| 60 |
+
return result[0].get("generated_text", "No response generated.")
|
| 61 |
+
return str(result)
|
| 62 |
+
elif response.status_code == 503:
|
| 63 |
+
return f"⏳ Model is loading... Please try again in a few seconds."
|
| 64 |
+
else:
|
| 65 |
+
return f"⚠️ Error {response.status_code}: {response.text[:200]}"
|
| 66 |
+
except requests.exceptions.Timeout:
|
| 67 |
+
return "⏰ Request timed out. The model might be loading. Please try again."
|
| 68 |
+
except Exception as e:
|
| 69 |
+
return f"❌ Error: {str(e)}"
|
| 70 |
|
| 71 |
+
def chat(message, model_name, system_prompt, history):
|
| 72 |
+
"""Chat function with real AI responses"""
|
| 73 |
+
if not message.strip():
|
| 74 |
+
return history, ""
|
| 75 |
|
| 76 |
+
model_info = MODELS.get(model_name, MODELS["Devrayog Agni (Fast)"])
|
| 77 |
+
model_id = model_info["id"]
|
| 78 |
+
|
| 79 |
+
# Add user message to history
|
| 80 |
+
history = history or []
|
| 81 |
+
history.append(("user", message))
|
| 82 |
+
|
| 83 |
+
# Get AI response
|
| 84 |
+
response = query_hf_model(model_id, message, system_prompt)
|
| 85 |
+
|
| 86 |
+
# Add AI response to history
|
| 87 |
+
history.append(("assistant", response))
|
| 88 |
+
|
| 89 |
+
return history, ""
|
| 90 |
+
|
| 91 |
+
def format_chat(history):
|
| 92 |
+
"""Format chat history for display"""
|
| 93 |
+
if not history:
|
| 94 |
+
return ""
|
| 95 |
|
| 96 |
+
formatted = ""
|
| 97 |
+
for role, message in history:
|
| 98 |
+
if role == "user":
|
| 99 |
+
formatted += f"👤 You: {message}\n\n"
|
| 100 |
+
else:
|
| 101 |
+
formatted += f"🤖 Devrayog AI: {message}\n\n"
|
| 102 |
+
return formatted
|
| 103 |
|
| 104 |
+
# Custom CSS for WhatsApp-like UI
|
| 105 |
+
custom_css = """
|
| 106 |
+
.gradio-container {max-width: 800px !important; margin: auto !important;}
|
| 107 |
+
.main-title {text-align: center; background: linear-gradient(135deg, #FF6B35, #7B2FFF); padding: 15px; border-radius: 10px; margin-bottom: 15px;}
|
| 108 |
+
.main-title h1 {color: white !important; margin: 0 !important; font-size: 1.5rem !important;}
|
| 109 |
+
.main-title p {color: rgba(255,255,255,0.9) !important; margin: 5px 0 0 0 !important; font-size: 0.85rem !important;}
|
| 110 |
+
.chat-container {background: #0b141a !important; border-radius: 10px !important; padding: 15px !important;}
|
| 111 |
+
.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;}
|
| 112 |
+
.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;}
|
| 113 |
+
.input-row {display: flex !important; gap: 10px !important; margin-top: 10px !important;}
|
| 114 |
+
.input-box {flex: 1 !important;}
|
| 115 |
+
.send-btn {background: #FF6B35 !important; border: none !important; border-radius: 50% !important; width: 50px !important; height: 50px !important; cursor: pointer !important;}
|
| 116 |
+
.send-btn:hover {background: #e55a2b !important;}
|
| 117 |
+
"""
|
| 118 |
|
| 119 |
+
with gr.Blocks(title="Devrayog AI", css=custom_css) as demo:
|
| 120 |
+
|
| 121 |
+
# Header
|
| 122 |
+
gr.HTML("""
|
| 123 |
+
<div class="main-title">
|
| 124 |
+
<h1>🇮🇳 Devrayog AI</h1>
|
| 125 |
+
<p>India's First Solo-Built AI | Founded by Devanshu Sharma (Hermes), Age 18, Rajasthan</p>
|
| 126 |
+
</div>
|
| 127 |
""")
|
| 128 |
+
|
| 129 |
+
with gr.Row():
|
| 130 |
+
with gr.Column(scale=4):
|
| 131 |
+
# Model selector
|
| 132 |
+
model_select = gr.Dropdown(
|
| 133 |
+
choices=list(MODELS.keys()),
|
| 134 |
+
value="Devrayog Agni (Fast)",
|
| 135 |
+
label="Select Model",
|
| 136 |
+
interactive=True
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
# System prompt (collapsible)
|
| 140 |
+
with gr.Accordion("⚙️ Advanced Settings", open=False):
|
| 141 |
+
sys_prompt = gr.Textbox(
|
| 142 |
+
label="System Prompt",
|
| 143 |
+
placeholder="Set a custom system prompt for the AI...",
|
| 144 |
+
lines=2,
|
| 145 |
+
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."
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
# Chat display
|
| 149 |
+
chat_display = gr.Textbox(
|
| 150 |
+
label="Chat",
|
| 151 |
+
lines=20,
|
| 152 |
+
interactive=False,
|
| 153 |
+
elem_classes=["chat-container"],
|
| 154 |
+
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"
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
# Input row
|
| 158 |
+
with gr.Row():
|
| 159 |
+
msg_input = gr.Textbox(
|
| 160 |
+
label="",
|
| 161 |
+
placeholder="Type your message here...",
|
| 162 |
+
lines=1,
|
| 163 |
+
scale=4,
|
| 164 |
+
container=False
|
| 165 |
+
)
|
| 166 |
+
send_btn = gr.Button("➤", scale=1, variant="primary")
|
| 167 |
+
|
| 168 |
+
with gr.Column(scale=1):
|
| 169 |
+
gr.Markdown("""
|
| 170 |
+
### 📚 Models
|
| 171 |
+
|
| 172 |
+
**🔥 Agni (Fast)**
|
| 173 |
+
3.8B params
|
| 174 |
+
Quick responses, coding, daily tasks
|
| 175 |
+
|
| 176 |
+
**🌊 Vayu (Creative)**
|
| 177 |
+
9B params
|
| 178 |
+
Storytelling, brainstorming, content
|
| 179 |
+
|
| 180 |
+
**🌌 Akash (Powerful)**
|
| 181 |
+
8B params
|
| 182 |
+
Complex reasoning, analysis, research
|
| 183 |
+
""")
|
| 184 |
+
|
| 185 |
+
gr.Markdown("""
|
| 186 |
+
### 🎯 SSB Prep Tips
|
| 187 |
+
- Practice OIR daily
|
| 188 |
+
- Write 6 TAT stories/day
|
| 189 |
+
- Current affairs updates
|
| 190 |
+
- Physical fitness
|
| 191 |
+
- Be confident & authentic
|
| 192 |
+
""")
|
| 193 |
+
|
| 194 |
+
gr.Markdown("""
|
| 195 |
+
### 🔗 Links
|
| 196 |
+
- HF: huggingface.co/dev2089
|
| 197 |
+
- Founder: Hermes, 18, Rajasthan
|
| 198 |
+
""")
|
| 199 |
+
|
| 200 |
+
# Hidden state for chat history
|
| 201 |
+
chat_state = gr.State([])
|
| 202 |
+
|
| 203 |
+
# Event handlers
|
| 204 |
+
def on_send(message, model, prompt, history):
|
| 205 |
+
if not message.strip():
|
| 206 |
+
return format_chat(history) if history else "", "", history
|
| 207 |
+
|
| 208 |
+
model_info = MODELS.get(model, MODELS["Devrayog Agni (Fast)"])
|
| 209 |
+
model_id = model_info["id"]
|
| 210 |
+
|
| 211 |
+
history = history or []
|
| 212 |
+
history.append(("user", message))
|
| 213 |
+
|
| 214 |
+
response = query_hf_model(model_id, message, prompt)
|
| 215 |
+
history.append(("assistant", response))
|
| 216 |
+
|
| 217 |
+
return format_chat(history), "", history
|
| 218 |
+
|
| 219 |
+
send_btn.click(
|
| 220 |
+
fn=on_send,
|
| 221 |
+
inputs=[msg_input, model_select, sys_prompt, chat_state],
|
| 222 |
+
outputs=[chat_display, msg_input, chat_state]
|
| 223 |
+
)
|
| 224 |
+
|
| 225 |
+
msg_input.submit(
|
| 226 |
+
fn=on_send,
|
| 227 |
+
inputs=[msg_input, model_select, sys_prompt, chat_state],
|
| 228 |
+
outputs=[chat_display, msg_input, chat_state]
|
| 229 |
+
)
|
| 230 |
|
| 231 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|