Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,50 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
4 |
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
from datasets import load_dataset
|
4 |
|
5 |
+
# Load LLaMA model and tokenizer from Hugging Face
|
6 |
+
model_name = "meta-llama/Llama-3.2-1B"
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
9 |
|
10 |
+
# Load financial dataset to enrich responses
|
11 |
+
dataset = load_dataset("gbharti/finance-alpaca")
|
12 |
+
|
13 |
+
# Helper function to extract dataset info (optional enhancement)
|
14 |
+
def get_insight_from_dataset():
|
15 |
+
sample = dataset["train"].shuffle(seed=42).select([0])[0]
|
16 |
+
return f"Example insight: {sample['text']}"
|
17 |
+
|
18 |
+
# Function to process user input and generate financial advice
|
19 |
+
def financial_advisor(user_input):
|
20 |
+
# Tokenize the user input
|
21 |
+
inputs = tokenizer(user_input, return_tensors="pt")
|
22 |
+
|
23 |
+
# Generate response using the LLaMA model
|
24 |
+
outputs = model.generate(**inputs, max_length=256, num_return_sequences=1)
|
25 |
+
advice = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
26 |
+
|
27 |
+
# Get additional insight from dataset to enrich advice (optional)
|
28 |
+
insight = get_insight_from_dataset()
|
29 |
+
|
30 |
+
# Combine the advice and the insight
|
31 |
+
full_response = f"Advice: {advice}\n\n{insight}"
|
32 |
+
return full_response
|
33 |
+
|
34 |
+
# Create Gradio Interface
|
35 |
+
interface = gr.Interface(
|
36 |
+
fn=financial_advisor,
|
37 |
+
inputs=gr.Textbox(lines=5, placeholder="Enter your financial question..."),
|
38 |
+
outputs="text",
|
39 |
+
title="AI Financial Advisor",
|
40 |
+
description="Ask me anything related to finance, investments, savings, and more.",
|
41 |
+
examples=[
|
42 |
+
"Should I invest in stocks or real estate?",
|
43 |
+
"How can I save more money on a tight budget?",
|
44 |
+
"What are some good investment options for retirement?",
|
45 |
+
]
|
46 |
+
)
|
47 |
|
48 |
+
# Launch the Gradio app
|
49 |
+
if __name__ == "__main__":
|
50 |
+
interface.launch()
|