rishh76 commited on
Commit
de7f838
·
1 Parent(s): 2ad3fd6

initial commit

Browse files
Files changed (2) hide show
  1. app.py +75 -1
  2. dataset.csv +0 -0
app.py CHANGED
@@ -1 +1,75 @@
1
- # sample file
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import textwrap
3
+ from dotenv import load_dotenv
4
+ import gradio as gr
5
+ from haystack import Pipeline
6
+ from haystack.utils import Secret
7
+ from haystack.components.builders import PromptBuilder
8
+ from haystack.components.generators import OpenAIGenerator
9
+ import pandas as pd
10
+
11
+ load_dotenv()
12
+
13
+ MODEL = "microsoft/Phi-3-mini-4k-instruct"
14
+
15
+ # Load the CSV file
16
+ df = pd.read_csv("dataset.csv")
17
+
18
+ # Set up components
19
+ prompt_template = """
20
+ Based on the Indian Union Budget data for FY 21-22 to 23-24:
21
+
22
+ {{budget_data}}
23
+
24
+ Answer the given question: {{query}}
25
+ Answer:
26
+ """
27
+ prompt_builder = PromptBuilder(template=prompt_template)
28
+ llm = OpenAIGenerator(
29
+ api_key=Secret.from_env_var("MONSTER_API_KEY"),
30
+ api_base_url="https://llm.monsterapi.ai/v1/",
31
+ model=MODEL,
32
+ generation_kwargs={"max_tokens": 512}
33
+ )
34
+ pipeline = Pipeline()
35
+ pipeline.add_component("prompt", prompt_builder)
36
+ pipeline.add_component("llm", llm)
37
+
38
+ pipeline.connect("prompt.prompt", "llm.prompt")
39
+
40
+ # Function to handle the query
41
+ # def answer_query(query):
42
+ # # Convert DataFrame to string representation
43
+ # budget_data = df.to_string()
44
+ # result = pipeline.run({"prompt": {"budget_data": budget_data, "query": query}})
45
+ # return result["llm"]["replies"][0]
46
+
47
+
48
+ def answer_query(query):
49
+ try:
50
+ # Select a subset of the data (adjust as needed)
51
+ sample_data = df.sample(n=10).to_string()
52
+
53
+ # Truncate the data if it's too long
54
+ budget_data = textwrap.shorten(sample_data, width=1000, placeholder="...")
55
+
56
+ result = pipeline.run({"prompt": {"budget_data": budget_data, "query": query}})
57
+ return result["llm"]["replies"][0]
58
+ except Exception as e:
59
+ return f"An error occurred: {str(e)}"
60
+
61
+ # Gradio interface
62
+ def chat_interface(query):
63
+ return answer_query(query)
64
+
65
+ with gr.Blocks() as demo:
66
+ gr.Markdown("# Indian 2024 Budget Chatbot")
67
+ query_input = gr.Textbox(label="Enter Your Question")
68
+ submit_button = gr.Button("Get Answer")
69
+ output_text = gr.Textbox(label="Answer", interactive=False)
70
+
71
+ submit_button.click(fn=chat_interface, inputs=[query_input], outputs=output_text)
72
+
73
+ # Run the app locally
74
+ if __name__ == "__main__":
75
+ demo.launch()
dataset.csv ADDED
The diff for this file is too large to render. See raw diff