pratikshahp
commited on
Update hf_app.py
Browse files
hf_app.py
CHANGED
@@ -1,4 +1,69 @@
|
|
1 |
from dotenv import load_dotenv
|
|
|
|
|
|
|
2 |
|
3 |
load_dotenv()
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from dotenv import load_dotenv
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import pipeline
|
4 |
+
import os
|
5 |
|
6 |
load_dotenv()
|
7 |
+
# Load the Hugging Face model and tokenizer for text generation
|
8 |
+
hf_token = os.getenv('HF_TOKEN') # Hugging Face Token for authentication
|
9 |
+
model_name = "meta-llama/Llama-3-70b-chat-hf" # Hugging Face model
|
10 |
+
chat_pipeline = pipeline("text-generation", model=model_name, use_auth_token=hf_token)
|
11 |
+
|
12 |
+
# Function to handle the chatbot's response to user queries
|
13 |
+
# You can only answer finance-related queries.
|
14 |
+
# - Do not answer non-finance questions.
|
15 |
+
def run_action(message, history):
|
16 |
+
system_prompt = """You are a financial assistant.
|
17 |
+
- Answer in 50 words.
|
18 |
+
- Ensure responses adhere to the safety policy."""
|
19 |
+
|
20 |
+
messages = [{"role": "system", "content": system_prompt}]
|
21 |
+
|
22 |
+
# Convert history into the appropriate format
|
23 |
+
for entry in history:
|
24 |
+
if entry["role"] == "user":
|
25 |
+
messages.append({"role": "user", "content": entry["content"]})
|
26 |
+
elif entry["role"] == "assistant":
|
27 |
+
messages.append({"role": "assistant", "content": entry["content"]})
|
28 |
+
|
29 |
+
# Add the user's current action
|
30 |
+
messages.append({"role": "user", "content": message})
|
31 |
+
|
32 |
+
# Generate the model output using Hugging Face's pipeline
|
33 |
+
response = chat_pipeline(messages)
|
34 |
+
|
35 |
+
return response[0]['generated_text']
|
36 |
+
|
37 |
+
# Main loop for the chatbot to handle user input
|
38 |
+
def main_loop(message, history):
|
39 |
+
"""
|
40 |
+
Main loop for the chatbot to handle user input.
|
41 |
+
"""
|
42 |
+
# Validate the user's input for safety
|
43 |
+
if not is_safe(message):
|
44 |
+
return "Your input violates our safety policy. Please try again with a finance-related query."
|
45 |
+
|
46 |
+
# Generate and validate the response
|
47 |
+
return run_action(message, history)
|
48 |
+
|
49 |
+
# Gradio Chat Interface
|
50 |
+
demo = gr.ChatInterface(
|
51 |
+
main_loop,
|
52 |
+
chatbot=gr.Chatbot(
|
53 |
+
height=450,
|
54 |
+
placeholder="Ask a finance-related question. Type 'exit' to quit.",
|
55 |
+
type="messages", # Proper rendering of chat format
|
56 |
+
),
|
57 |
+
textbox=gr.Textbox(
|
58 |
+
placeholder="What do you want to ask about finance?",
|
59 |
+
container=False,
|
60 |
+
scale=7,
|
61 |
+
),
|
62 |
+
title="Finance Chatbot",
|
63 |
+
theme="Monochrome",
|
64 |
+
examples=["What is compound interest?", "How to save for retirement?", "What are tax-saving options?"],
|
65 |
+
cache_examples=False,
|
66 |
+
)
|
67 |
+
|
68 |
+
# Launch the Gradio app
|
69 |
+
demo.launch(share=True, server_name="0.0.0.0")
|