AbuSaleh28 commited on
Commit
ced39f2
·
verified ·
1 Parent(s): 489424f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +141 -0
app.py ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ # Create requirements.txt content
4
+ requirements_content = """gradio>=4.0.0
5
+ groq>=0.4.0
6
+ """
7
+
8
+ # Create app.py content (modifying the user's working Colab code slightly for HF Spaces environment variable safety)
9
+ app_content = """import gradio as gr
10
+ import os
11
+ from groq import Groq
12
+
13
+ # Initialize the Groq client
14
+ # On Hugging Face Spaces, set GROQ_API_KEY as a Secret in the Space Settings
15
+ api_key = os.environ.get("GROQ_API_KEY")
16
+ if not api_key:
17
+ raise ValueError("GROQ_API_KEY environment variable not found. Please add it as a Secret in your Space Settings.")
18
+
19
+ client = Groq(api_key=api_key)
20
+
21
+ # Custom CSS to give it a sleek, modern look
22
+ custom_css = \"\"\"
23
+ .gradio-container { background-color: #0b0f19; font-family: 'Inter', sans-serif; }
24
+ #title-header { text-align: center; margin-bottom: 20px; }
25
+ #title-header h1 { color: #38bdf8; font-weight: 800; font-size: 2.2rem; }
26
+ .sidebar-panel { background: #111827 !important; border: 1px solid #1f2937 !important; border-radius: 12px !important; }
27
+ .chat-window { border: 1px solid #1f2937 !important; border-radius: 12px !important; background: #111827 !important; }
28
+ \"\"\"
29
+
30
+ def chat_stream(message, history, model, system_prompt, temperature, max_tokens):
31
+ \"\"\"
32
+ Custom chat function built for gr.Chatbot component format.
33
+ history is a list of dicts or tuples depending on older/newer Gradio versions.
34
+ \"\"\"
35
+ # 1. Start with the custom system prompt
36
+ messages = [{"role": "system", "content": system_prompt}]
37
+
38
+ # 2. Append the conversation history
39
+ for turn in history:
40
+ # Check if history format is dict or list/tuple
41
+ if isinstance(turn, dict):
42
+ messages.append({"role": turn["role"], "content": turn["content"]})
43
+ else:
44
+ messages.append({"role": "user", "content": turn[0]})
45
+ messages.append({"role": "assistant", "content": turn[1]})
46
+
47
+ # 3. Append current user message
48
+ messages.append({"role": "user", "content": message})
49
+
50
+ # 4. Stream response from Groq
51
+ try:
52
+ stream = client.chat.completions.create(
53
+ model=model,
54
+ messages=messages,
55
+ temperature=temperature,
56
+ max_tokens=max_tokens,
57
+ stream=True,
58
+ )
59
+
60
+ partial_response = ""
61
+ for chunk in stream:
62
+ if chunk.choices[0].delta.content:
63
+ partial_response += chunk.choices[0].delta.content
64
+ yield partial_response
65
+
66
+ except Exception as e:
67
+ yield f"⚠️ Error connecting to Groq: {str(e)}"
68
+
69
+
70
+ # Building the Interactive Interface using Blocks
71
+ with gr.Blocks() as demo:
72
+
73
+ # Title Section
74
+ gr.Markdown("# 🚀 Personal AI ChatBot", elem_id="title-header")
75
+ gr.Markdown("A fully customizable, hyper-fast LLM workspace.")
76
+
77
+ with gr.Row():
78
+ # --- LEFT COLUMN: CONTROL PANEL ---
79
+ with gr.Column(scale=1, elem_classes="sidebar-panel"):
80
+ gr.Markdown("### ⚙️ Engine Configurations")
81
+
82
+ model_select = gr.Dropdown(
83
+ choices=["llama-3.3-70b-versatile", "llama-3.1-8b-instant"],
84
+ value="llama-3.3-70b-versatile",
85
+ label="Select AI Model"
86
+ )
87
+
88
+ system_input = gr.Textbox(
89
+ value="You are a helpful, brilliant, and concise AI assistant.",
90
+ label="System Prompt / AI Persona",
91
+ lines=3,
92
+ placeholder="Ex: Act as a cynical senior developer..."
93
+ )
94
+
95
+ gr.Markdown("---")
96
+ gr.Markdown("### 🧠 Hyperparameters")
97
+
98
+ temp_slider = gr.Slider(
99
+ minimum=0.0, maximum=2.0, value=0.7, step=0.1,
100
+ label="Temperature", info="Higher = more creative, Lower = more factual"
101
+ )
102
+
103
+ tokens_slider = gr.Slider(
104
+ minimum=128, maximum=4096, value=1024, step=128,
105
+ label="Max Output Tokens"
106
+ )
107
+
108
+ gr.Markdown("---")
109
+ gr.Markdown("**Status:** 🟢 Connected to Groq API")
110
+
111
+ # --- RIGHT COLUMN: CHAT INTERFACE ---
112
+ with gr.Column(scale=3):
113
+ chatbot = gr.Chatbot(elem_classes="chat-window")
114
+
115
+ # Textbox and setup for user interaction
116
+ msg_input = gr.Textbox(
117
+ placeholder="Type your message here and press Enter...",
118
+ show_label=False,
119
+ container=False
120
+ )
121
+
122
+ # Wrap everything into Gradio's chat system
123
+ gr.ChatInterface(
124
+ fn=chat_stream,
125
+ chatbot=chatbot,
126
+ textbox=msg_input,
127
+ additional_inputs=[model_select, system_input, temp_slider, tokens_slider]
128
+ )
129
+
130
+ if __name__ == "__main__":
131
+ demo.launch(css=custom_css, theme=gr.themes.Soft())
132
+ """
133
+
134
+ # Write files to disk
135
+ with open("requirements.txt", "w", encoding="utf-8") as f:
136
+ f.write(requirements_content.strip())
137
+
138
+ with open("app.py", "w", encoding="utf-8") as f:
139
+ f.write(app_content.strip())
140
+
141
+ print("Files created successfully.")