Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from haystack import Pipeline
|
3 |
+
from haystack.components.fetchers import LinkContentFetcher
|
4 |
+
from haystack.components.converters import HTMLToDocument
|
5 |
+
from haystack.components.builders import PromptBuilder
|
6 |
+
from haystack.components.generators import OpenAIGenerator
|
7 |
+
from haystack.utils import Secret
|
8 |
+
|
9 |
+
def budget_chatbot(query):
|
10 |
+
fetcher = LinkContentFetcher()
|
11 |
+
converter = HTMLToDocument()
|
12 |
+
prompt_template = """
|
13 |
+
According to the contents of this website:
|
14 |
+
{% for document in documents %}
|
15 |
+
{{document.content}}
|
16 |
+
{% endfor %}
|
17 |
+
Answer the given question: {{query}}
|
18 |
+
Answer:
|
19 |
+
"""
|
20 |
+
prompt_builder = PromptBuilder(template=prompt_template)
|
21 |
+
llm = OpenAIGenerator(
|
22 |
+
api_key=Secret.from_env_var("MONSTER_API_KEY"),
|
23 |
+
api_base_url="https://llm.monsterapi.ai/v1/",
|
24 |
+
model="microsoft/Phi-3-mini-4k-instruct",
|
25 |
+
generation_kwargs = {"max_tokens": 256}
|
26 |
+
)
|
27 |
+
pipeline = Pipeline()
|
28 |
+
pipeline.add_component("fetcher", fetcher)
|
29 |
+
pipeline.add_component("converter", converter)
|
30 |
+
pipeline.add_component("prompt", prompt_builder)
|
31 |
+
pipeline.add_component("llm", llm)
|
32 |
+
|
33 |
+
pipeline.connect("fetcher.streams", "converter.sources")
|
34 |
+
pipeline.connect("converter.documents", "prompt.documents")
|
35 |
+
pipeline.connect("prompt.prompt", "llm.prompt")
|
36 |
+
|
37 |
+
result = pipeline.run({"fetcher": {"urls": ["https://example.com/indian-2024-budget"]},
|
38 |
+
"prompt": {"query": query}})
|
39 |
+
return result["llm"]["replies"][0]
|
40 |
+
|
41 |
+
gr.Interface(fn=budget_chatbot, inputs="text", outputs="text", title="Indian 2024 Budget Chatbot").launch()
|