muhammadharis222 commited on
Commit
661d930
verified
1 Parent(s): 7544ddf

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -0
app.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from google import genai
4
+
5
+ # Load Gemini API key (set this in your Hugging Face Space Secrets)
6
+ API_KEY = os.environ.get("GEMINI_API_KEY")
7
+ if not API_KEY:
8
+ raise RuntimeError("Missing GEMINI_API_KEY. Set it in Space Secrets.")
9
+
10
+ client = genai.Client(api_key=API_KEY)
11
+ MODEL = os.environ.get("GEMINI_MODEL", "gemini-2.5-flash")
12
+
13
+
14
+ def call_gemini(prompt: str) -> str:
15
+ """Call Gemini and return model response text."""
16
+ try:
17
+ response = client.models.generate_content(model=MODEL, contents=prompt)
18
+ return getattr(response, "text", str(response))
19
+ except Exception as e:
20
+ return f"[Error calling Gemini API: {e}]"
21
+
22
+
23
+ def chat_turn(user_message: str, history: list):
24
+ """Handle a single user chat turn (Gradio messages format)."""
25
+ if history is None:
26
+ history = []
27
+
28
+ user_message = user_message.strip()
29
+ if not user_message:
30
+ return history, history
31
+
32
+ history.append({"role": "user", "content": user_message})
33
+ reply = call_gemini(user_message)
34
+ history.append({"role": "assistant", "content": reply})
35
+ return history, history
36
+
37
+
38
+ def clear_chat():
39
+ return [], []
40
+
41
+
42
+ with gr.Blocks(
43
+ title="Muhammad Haris Chatbot",
44
+ css="""
45
+ body {background: linear-gradient(135deg, #e0f7fa, #e1bee7);}
46
+ #chatbot .user {
47
+ background-color: #0288d1 !important;
48
+ color: white !important;
49
+ border-radius: 16px !important;
50
+ padding: 10px !important;
51
+ }
52
+ #chatbot .assistant {
53
+ background-color: #ffffff !important;
54
+ border: 1px solid #ccc !important;
55
+ border-radius: 16px !important;
56
+ padding: 10px !important;
57
+ }
58
+ #title {
59
+ text-align: center;
60
+ font-size: 2em;
61
+ font-weight: bold;
62
+ color: #4a148c;
63
+ margin-bottom: 20px;
64
+ text-shadow: 1px 1px 2px #aaa;
65
+ }
66
+ #footer {
67
+ text-align: center;
68
+ font-size: 0.9em;
69
+ color: #555;
70
+ margin-top: 15px;
71
+ }
72
+ """,
73
+ ) as demo:
74
+ gr.HTML('<div id="title">馃 Muhammad Haris Chatbot</div>')
75
+
76
+ chatbot = gr.Chatbot(label="", type="messages", elem_id="chatbot")
77
+ state = gr.State([])
78
+
79
+ with gr.Row():
80
+ txt = gr.Textbox(
81
+ show_label=False,
82
+ placeholder="Type your message and press Enter...",
83
+ lines=1,
84
+ scale=9,
85
+ )
86
+ send = gr.Button("Send", scale=1)
87
+
88
+ txt.submit(fn=chat_turn, inputs=[txt, state], outputs=[chatbot, state])
89
+ send.click(fn=chat_turn, inputs=[txt, state], outputs=[chatbot, state])
90
+ gr.Button("Clear").click(fn=clear_chat, inputs=None, outputs=[chatbot, state])
91
+
92
+ gr.HTML('<div id="footer">Powered by Google Gemini 路 Built by Muhammad Haris</div>')
93
+
94
+ if __name__ == "__main__":
95
+ demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))