Create app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261import osimport gradio as grfrom openai import OpenAI
# ============================================# NEXUS AI - PROFESSIONAL VERSION# ============================================
# Reads API key from Hugging Face Secrets# Secret Name:# OPENROUTER_API_KEY
api_key = os.getenv("OPENROUTER_API_KEY")
# OpenRouter Clientclient = OpenAI( base_url="https://openrouter.ai/api/v1", api_key=api_key)
# ============================================# SYSTEM PROMPT# ============================================
SYSTEM_PROMPT = """You are Nexus AI.
A powerful professional AI assistant.
Abilities:- Coding Expert- Python Expert- Web Developer- Debugging Expert- Homework Helper- Reasoning AI- App Developer- Friendly Assistant
Always give:- Clear answers- Professional coding- Correct solutions- Modern coding style"""
# ============================================# AI RESPONSE FUNCTION# ============================================
def generate_response(message, history):
try:
if not api_key: return "β OPENROUTER_API_KEY not found in Hugging Face Secrets."
messages = [ { "role": "system", "content": SYSTEM_PROMPT } ]
# Chat history for user, assistant in history: messages.append({ "role": "user", "content": user })
messages.append({ "role": "assistant", "content": assistant })
# User message messages.append({ "role": "user", "content": message })
# AI Completion completion = client.chat.completions.create( model="deepseek/deepseek-chat-v3-0324:free", messages=messages, temperature=0.7, max_tokens=1500 )
reply = completion.choices[0].message.content
return reply
except Exception as e: return f"β Error: {str(e)}"
# ============================================# CHAT FUNCTION# ============================================
def chat(message, history):
if history is None: history = []
response = generate_response(message, history)
history.append((message, response))
return "", history
# ============================================# FILE UPLOAD FUNCTION# ============================================
def upload_file(file):
try:
if file is None: return "No file uploaded."
return f"β
File Uploaded Successfully\n\nπ {file.name}"
except Exception as e: return f"β File Error: {str(e)}"
# ============================================# PROFESSIONAL CSS# ============================================
css = """body{ background: #0f172a;}
.gradio-container{ max-width: 1100px !important; margin: auto !important; padding-top: 10px;}
#main_title{ text-align:center; font-size:42px; font-weight:800; color:white; margin-bottom:5px;}
#subtitle{ text-align:center; color:#94a3b8; font-size:16px; margin-bottom:20px;}
footer{ display:none !important;}
.gr-chatbot{ border-radius:20px !important; border:1px solid #334155 !important;}
textarea{ font-size:16px !important;}
button{ border-radius:14px !important; font-size:18px !important; font-weight:bold !important;}
"""
# ============================================# UI# ============================================
with gr.Blocks( css=css, theme=gr.themes.Soft( primary_hue="blue", secondary_hue="slate", neutral_hue="slate" )) as demo:
gr.HTML(""" <div id="main_title">π€ Nexus AI</div> <div id="subtitle"> Advanced AI Assistant β’ Coding β’ Reasoning β’ File Upload </div> """)
chatbot = gr.Chatbot( height=550, bubble_full_width=False, show_copy_button=True, avatar_images=( None, "https://cdn-icons-png.flaticon.com/512/4712/4712109.png" ) )
with gr.Row():
msg = gr.Textbox( placeholder="Message Nexus AI...", lines=1, scale=8, container=False )
send_btn = gr.Button( "β€", scale=1, variant="primary" )
with gr.Row():
upload = gr.File( label="π Upload File" )
file_status = gr.Textbox( label="File Status", interactive=False )
# Send Button send_btn.click( chat, inputs=[msg, chatbot], outputs=[msg, chatbot] )
# Enter Key msg.submit( chat, inputs=[msg, chatbot], outputs=[msg, chatbot] )
# File Upload upload.change( upload_file, inputs=upload, outputs=file_status )
# ============================================# LAUNCH# ============================================
demo.launch()app.pyAdded+261