Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import random
|
3 |
+
import os
|
4 |
+
from openai import OpenAI
|
5 |
+
|
6 |
+
# --- Configuration ---
|
7 |
+
client = OpenAI(
|
8 |
+
base_url="https://openrouter.ai/api/v1",
|
9 |
+
api_key=os.environ.get("OPENROUTER_API_KEY"),
|
10 |
+
)
|
11 |
+
|
12 |
+
REFERER_HEADER = "https://alphaai.org.in"
|
13 |
+
TITLE_HEADER = "Alpha AI Chatbot"
|
14 |
+
MODEL_NAME = "google/gemini-2.0-flash-exp:free"
|
15 |
+
|
16 |
+
# --- CAPTCHA Logic ---
|
17 |
+
def generate_captcha():
|
18 |
+
a, b = random.randint(1, 9), random.randint(1, 9)
|
19 |
+
return f"What is {a} + {b}?", a + b
|
20 |
+
|
21 |
+
# Store correct answers in-memory (for stateless apps, better use secure session)
|
22 |
+
captcha_answers = {}
|
23 |
+
|
24 |
+
# --- Chatbot Backend ---
|
25 |
+
def chat_with_llm(user_input, session_id, captcha_input):
|
26 |
+
correct_answer = captcha_answers.get(session_id)
|
27 |
+
if correct_answer is None:
|
28 |
+
return "CAPTCHA not initialized properly. Please reload.", generate_new_captcha(session_id)[0]
|
29 |
+
|
30 |
+
try:
|
31 |
+
if int(captcha_input) != correct_answer:
|
32 |
+
return "Incorrect CAPTCHA. Try again.", generate_new_captcha(session_id)[0]
|
33 |
+
except ValueError:
|
34 |
+
return "Please enter a valid number for the CAPTCHA.", generate_new_captcha(session_id)[0]
|
35 |
+
|
36 |
+
# Make OpenRouter API call
|
37 |
+
try:
|
38 |
+
completion = client.chat.completions.create(
|
39 |
+
extra_headers={
|
40 |
+
"HTTP-Referer": REFERER_HEADER,
|
41 |
+
"X-Title": TITLE_HEADER,
|
42 |
+
},
|
43 |
+
model=MODEL_NAME,
|
44 |
+
messages=[{"role": "user", "content": user_input}]
|
45 |
+
)
|
46 |
+
response = completion.choices[0].message.content
|
47 |
+
return response, generate_new_captcha(session_id)[0]
|
48 |
+
except Exception as e:
|
49 |
+
return f"Error: {str(e)}", generate_new_captcha(session_id)[0]
|
50 |
+
|
51 |
+
|
52 |
+
def generate_new_captcha(session_id):
|
53 |
+
captcha_q, captcha_ans = generate_captcha()
|
54 |
+
captcha_answers[session_id] = captcha_ans
|
55 |
+
return captcha_q, captcha_ans
|
56 |
+
|
57 |
+
|
58 |
+
# --- Gradio UI ---
|
59 |
+
def start_session():
|
60 |
+
session_id = str(random.randint(100000, 999999))
|
61 |
+
captcha_q, _ = generate_new_captcha(session_id)
|
62 |
+
return session_id, captcha_q
|
63 |
+
|
64 |
+
with gr.Blocks(theme=gr.themes.Base(primary_hue="blue", secondary_hue="gray", font=["Inter", "sans-serif"], dark=True)) as demo:
|
65 |
+
gr.Markdown("#🧠 Alpha AI Assistant with CAPTCHA")
|
66 |
+
gr.Markdown("Welcome to Alpha AI’s intelligent assistant. Please solve the CAPTCHA to continue.")
|
67 |
+
|
68 |
+
with gr.Row():
|
69 |
+
chatbot = gr.Chatbot()
|
70 |
+
|
71 |
+
with gr.Row():
|
72 |
+
user_input = gr.Textbox(label="Your Message")
|
73 |
+
captcha_input = gr.Textbox(label="CAPTCHA Answer")
|
74 |
+
|
75 |
+
with gr.Row():
|
76 |
+
session_id = gr.Textbox(visible=False)
|
77 |
+
captcha_display = gr.Textbox(label="CAPTCHA", interactive=False)
|
78 |
+
submit_btn = gr.Button("Submit")
|
79 |
+
|
80 |
+
def handle_submit(user_msg, sid, cap_input):
|
81 |
+
response, new_captcha_q = chat_with_llm(user_msg, sid, cap_input)
|
82 |
+
return [(user_msg, response)], sid, new_captcha_q
|
83 |
+
|
84 |
+
def init_session():
|
85 |
+
sid, cap_q = start_session()
|
86 |
+
return sid, cap_q
|
87 |
+
|
88 |
+
submit_btn.click(handle_submit, inputs=[user_input, session_id, captcha_input], outputs=[chatbot, session_id, captcha_display])
|
89 |
+
demo.load(init_session, inputs=[], outputs=[session_id, captcha_display])
|
90 |
+
|
91 |
+
# Launch (required for Hugging Face Space)
|
92 |
+
demo.launch()
|