File size: 2,281 Bytes
fa9c7ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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()