Spaces:
Sleeping
Sleeping
| import os | |
| import pandas as pd | |
| import gradio as gr | |
| from groq import Groq | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.linear_model import LinearRegression | |
| # ========================================================= | |
| # 1️⃣ GROQ CHATBOT (App 1) | |
| # ========================================================= | |
| client = Groq(api_key=os.getenv("GROQ_API_KEY")) | |
| def groq_chat(text): | |
| """Chat interface powered by Groq LLM.""" | |
| completion = client.chat.completions.create( | |
| model="llama-3.1-8b-instant", | |
| messages=[{"role": "user", "content": text}] | |
| ) | |
| return completion.choices[0].message.content | |
| # ========================================================= | |
| # 2️⃣ DBS Regression Model (App 2) | |
| # ========================================================= | |
| # Load local CSV (must be uploaded into the HF Space) | |
| df = pd.read_csv("DBS_SingDollar.csv") | |
| df = df[["DBS", "SGD"]].dropna() | |
| # Prepare data | |
| X = df[["SGD"]] | |
| y = df["DBS"] | |
| X_train, X_test, y_train, y_test = train_test_split( | |
| X, y, test_size=0.2, random_state=42 | |
| ) | |
| model = LinearRegression() | |
| model.fit(X_train, y_train) | |
| def predict_dbs_price(sgd_rate: float): | |
| """Predict DBS share price from SGD exchange rate.""" | |
| pred = model.predict([[sgd_rate]])[0] | |
| return f"Predicted DBS Share Price: {pred:.2f}" | |
| # ========================================================= | |
| # 3️⃣ BUILD MULTI-APP GRADIO UI | |
| # ========================================================= | |
| with gr.Blocks() as app: | |
| gr.Markdown("## 🚀 Multi-App: Groq Chatbot + DBS Share Price Predictor (CPU Version)") | |
| # ---- TAB 1: Groq Chatbot ---- | |
| with gr.Tab("💬 Groq Chatbot"): | |
| user_in = gr.Textbox(label="Enter your message:", lines=4) | |
| bot_out = gr.Textbox(label="Model Reply:", lines=8) | |
| send_btn = gr.Button("Send") | |
| send_btn.click(fn=groq_chat, inputs=user_in, outputs=bot_out) | |
| # ---- TAB 2: DBS Predictor ---- | |
| with gr.Tab("📈 DBS Price Predictor"): | |
| rate_in = gr.Number(label="SGD Exchange Rate") | |
| result_out = gr.Textbox(label="Predicted DBS Price") | |
| predict_btn = gr.Button("Predict") | |
| predict_btn.click(fn=predict_dbs_price, inputs=rate_in, outputs=result_out) | |
| if __name__ == "__main__": | |
| app.launch() | |