about_alphaai / app.py
tushar310's picture
Update app.py
aa6fbe0 verified
raw
history blame
2.13 kB
import gradio as gr
import os
import time
from openai import OpenAI
# --- Configuration ---
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key=os.environ.get("OPENROUTER_API_KEY"),
)
REFERER_HEADER = "https://alphaai.org.in"
TITLE_HEADER = "AlphaAI Chatbot"
MODEL_NAME = "deepseek/deepseek-r1-0528-qwen3-8b:free"
ALPHAAI_CONTEXT = os.environ.get("ALPHAAI_CONTEXT", "")
# --- Chatbot Backend ---
def chat_with_llm(user_input):
try:
input_fixed = "Stick to the context of Alpha AI and do not let the user deviate from that. If the query is anything other than Alpha AI, politely refuse. Here is the user query - " + str(user_input)
completion = client.chat.completions.create(
extra_headers={
"HTTP-Referer": REFERER_HEADER,
"X-Title": TITLE_HEADER,
},
model=MODEL_NAME,
messages=[{"role": "system", "content": ALPHAAI_CONTEXT}, {"role": "user", "content": input_fixed}]
)
return completion.choices[0].message.content
except Exception as e:
return f"Error: {str(e)}"
# --- Gradio UI ---
theme = gr.themes.Base()
with gr.Blocks(theme=theme) as demo:
gr.Markdown("# 🧠 Alpha AI Assistant")
gr.Markdown("Ask anything about Alpha AI.")
chatbot = gr.Chatbot(type="messages")
msg = gr.Textbox()
clear = gr.Button("Clear")
def user(user_message, history: list):
return "", history + [{"role": "user", "content": user_message}]
def bot(history: list):
user_msg = history[-1]['content']
try:
response = chat_with_llm(user_msg)
except Exception as e:
response = f"Error: {str(e)}"
history.append({"role": "assistant", "content": ""})
for char in response:
history[-1]['content'] += char
time.sleep(0.02)
yield history
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
bot, chatbot, chatbot
)
clear.click(lambda: None, None, chatbot, queue=False)
# Launch (required for Hugging Face Space)
demo.launch()