Spaces:
Sleeping
Sleeping
Update pipelines.py
Browse files- pipelines.py +20 -4
pipelines.py
CHANGED
|
@@ -4,8 +4,10 @@ import streamlit as st
|
|
| 4 |
from haystack import Pipeline
|
| 5 |
from haystack.components.converters import HTMLToDocument
|
| 6 |
from haystack.components.fetchers import LinkContentFetcher
|
| 7 |
-
from haystack.components.builders import PromptBuilder
|
| 8 |
-
from haystack.components.generators import HuggingFaceAPIGenerator
|
|
|
|
|
|
|
| 9 |
#from haystack.components.builders import DynamicChatPromptBuilder
|
| 10 |
from haystack.utils import Secret
|
| 11 |
#from dotenv import load_dotenv
|
|
@@ -44,7 +46,7 @@ def generate_quiz_pipeline():
|
|
| 44 |
quiz_generation_pipeline = Pipeline()
|
| 45 |
quiz_generation_pipeline.add_component("link_content_fetcher", LinkContentFetcher())
|
| 46 |
quiz_generation_pipeline.add_component("html_converter", HTMLToDocument())
|
| 47 |
-
quiz_generation_pipeline.add_component("prompt_builder",
|
| 48 |
quiz_generation_pipeline.add_component(
|
| 49 |
"generator",
|
| 50 |
HuggingFaceAPIGenerator(api_type=api_type,
|
|
@@ -53,6 +55,17 @@ def generate_quiz_pipeline():
|
|
| 53 |
token=Secret.from_token(HUGGINGFACE_API_KEY),
|
| 54 |
)
|
| 55 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
quiz_generation_pipeline.connect("link_content_fetcher", "html_converter")
|
| 57 |
quiz_generation_pipeline.connect("html_converter", "prompt_builder")
|
| 58 |
quiz_generation_pipeline.connect("prompt_builder", "generator")
|
|
@@ -64,7 +77,10 @@ def generate_quiz(url):
|
|
| 64 |
try:
|
| 65 |
results = pipeline.run({"link_content_fetcher": {"urls": [url]}})
|
| 66 |
if "generator" in results and "replies" in results["generator"]:
|
| 67 |
-
raw_reply = results["generator"]["replies"][0]
|
|
|
|
|
|
|
|
|
|
| 68 |
# Extract the JSON part of the reply
|
| 69 |
json_start = raw_reply.find("{")
|
| 70 |
json_end = raw_reply.rfind("}") + 1
|
|
|
|
| 4 |
from haystack import Pipeline
|
| 5 |
from haystack.components.converters import HTMLToDocument
|
| 6 |
from haystack.components.fetchers import LinkContentFetcher
|
| 7 |
+
#from haystack.components.builders import PromptBuilder
|
| 8 |
+
#from haystack.components.generators import HuggingFaceAPIGenerator
|
| 9 |
+
from haystack.components.generators import HuggingFaceAPIChatGenerator
|
| 10 |
+
from haystack.components.builders import ChatPromptBuilder
|
| 11 |
#from haystack.components.builders import DynamicChatPromptBuilder
|
| 12 |
from haystack.utils import Secret
|
| 13 |
#from dotenv import load_dotenv
|
|
|
|
| 46 |
quiz_generation_pipeline = Pipeline()
|
| 47 |
quiz_generation_pipeline.add_component("link_content_fetcher", LinkContentFetcher())
|
| 48 |
quiz_generation_pipeline.add_component("html_converter", HTMLToDocument())
|
| 49 |
+
quiz_generation_pipeline.add_component("prompt_builder", ChatPromptBuilder(template=quiz_generation_template))
|
| 50 |
quiz_generation_pipeline.add_component(
|
| 51 |
"generator",
|
| 52 |
HuggingFaceAPIGenerator(api_type=api_type,
|
|
|
|
| 55 |
token=Secret.from_token(HUGGINGFACE_API_KEY),
|
| 56 |
)
|
| 57 |
)
|
| 58 |
+
quiz_generation_pipeline.add_component(
|
| 59 |
+
"generator",
|
| 60 |
+
HuggingFaceAPIChatGenerator(
|
| 61 |
+
api_type=api_type,
|
| 62 |
+
api_params={
|
| 63 |
+
"model": "meta-llama/Llama-3.1-8B-Instruct",
|
| 64 |
+
"provider": "novita", # important for Inference Providers
|
| 65 |
+
},
|
| 66 |
+
token=Secret.from_token(HUGGINGFACE_API_KEY),
|
| 67 |
+
),
|
| 68 |
+
)
|
| 69 |
quiz_generation_pipeline.connect("link_content_fetcher", "html_converter")
|
| 70 |
quiz_generation_pipeline.connect("html_converter", "prompt_builder")
|
| 71 |
quiz_generation_pipeline.connect("prompt_builder", "generator")
|
|
|
|
| 77 |
try:
|
| 78 |
results = pipeline.run({"link_content_fetcher": {"urls": [url]}})
|
| 79 |
if "generator" in results and "replies" in results["generator"]:
|
| 80 |
+
#raw_reply = results["generator"]["replies"][0]
|
| 81 |
+
reply = results["generator"]["replies"][0]
|
| 82 |
+
raw_reply = reply.content if hasattr(reply, "content") else str(reply)
|
| 83 |
+
|
| 84 |
# Extract the JSON part of the reply
|
| 85 |
json_start = raw_reply.find("{")
|
| 86 |
json_end = raw_reply.rfind("}") + 1
|