Spaces:
Sleeping
Sleeping
MilanCalegari
commited on
Commit
·
17536fa
1
Parent(s):
868e142
Feat: Use HuggingFace API
Browse files- modules/llm/card_interpreter.py +47 -53
- modules/llm/rules.py +0 -23
modules/llm/card_interpreter.py
CHANGED
@@ -1,55 +1,28 @@
|
|
1 |
import os
|
2 |
-
from typing import
|
3 |
|
4 |
-
from huggingface_hub import
|
5 |
-
from transformers import pipeline
|
6 |
|
7 |
from modules.utils.commom import Card, CardReadingMethod
|
8 |
|
9 |
-
from ..interfaces.llm_interface import CardInterpreterInterface
|
10 |
-
from .rules import content
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
def get_pipeline():
|
16 |
-
global PIPELINE
|
17 |
-
if PIPELINE is None:
|
18 |
-
PIPELINE = pipeline(
|
19 |
-
"text-generation",
|
20 |
-
model="meta-llama/Llama-3.2-1B-Instruct",
|
21 |
-
device_map="auto",
|
22 |
-
pad_token_id=2,
|
23 |
-
model_kwargs={"low_cpu_mem_usage": True, "use_cache": False},
|
24 |
-
)
|
25 |
-
return PIPELINE
|
26 |
-
|
27 |
-
|
28 |
-
class CardInterpreter(CardInterpreterInterface):
|
29 |
def __init__(self) -> None:
|
30 |
-
# Login to Hugging Face
|
31 |
hf_token = os.getenv("HF_TOKEN")
|
32 |
-
|
33 |
-
login(token=hf_token)
|
34 |
|
35 |
-
|
36 |
-
self.pipeline = get_pipeline()
|
37 |
-
|
38 |
-
# Base prompt template
|
39 |
-
self._base_content = content
|
40 |
-
|
41 |
-
def _format_card(self, card: Card) -> str:
|
42 |
-
# Format card name with reversed state
|
43 |
return f"{card.name} (Reversed)" if card.reversed else card.name
|
44 |
|
45 |
-
def
|
46 |
-
self, cards: List[Card], context: str, method: CardReadingMethod
|
47 |
-
) -> List[Dict[str, str]]:
|
48 |
method_templates = {
|
49 |
CardReadingMethod.PAST_PRESENT_FUTURE: lambda: f"""The provided cards are:
|
50 |
Past: {self._format_card(cards[0])}
|
51 |
Present: {self._format_card(cards[1])}
|
52 |
Future: {self._format_card(cards[2])}
|
|
|
|
|
|
|
53 |
""",
|
54 |
CardReadingMethod.CELTIC_CROSS: lambda: f"""The provided cards are:
|
55 |
The situation: {self._format_card(cards[0])}
|
@@ -62,6 +35,9 @@ class CardInterpreter(CardInterpreterInterface):
|
|
62 |
What you need to know: {self._format_card(cards[7])}
|
63 |
Your hopes and fears: {self._format_card(cards[8])}
|
64 |
Outcomes: {self._format_card(cards[9])}
|
|
|
|
|
|
|
65 |
""",
|
66 |
CardReadingMethod.HAND_OF_ERIS: lambda: f"""The provided cards are:
|
67 |
About your question: {self._format_card(cards[0])}
|
@@ -69,27 +45,45 @@ class CardInterpreter(CardInterpreterInterface):
|
|
69 |
What may hinder you: {self._format_card(cards[2])}
|
70 |
Possible outcome number 1: {self._format_card(cards[3])}
|
71 |
Possible outcome number 2: {self._format_card(cards[4])}
|
|
|
|
|
|
|
72 |
""",
|
73 |
}
|
74 |
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
]
|
84 |
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
prompt,
|
91 |
-
max_new_tokens=512,
|
92 |
-
num_return_sequences=1,
|
93 |
-
do_sample=False,
|
94 |
)
|
95 |
-
|
|
|
|
1 |
import os
|
2 |
+
from typing import List
|
3 |
|
4 |
+
from huggingface_hub import InferenceClient
|
|
|
5 |
|
6 |
from modules.utils.commom import Card, CardReadingMethod
|
7 |
|
|
|
|
|
8 |
|
9 |
+
class CardInterpreter:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
def __init__(self) -> None:
|
|
|
11 |
hf_token = os.getenv("HF_TOKEN")
|
12 |
+
self.client = InferenceClient(api_key=hf_token)
|
|
|
13 |
|
14 |
+
def _format_card(self, card: Card):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
return f"{card.name} (Reversed)" if card.reversed else card.name
|
16 |
|
17 |
+
def complet_prompt(self, cards, context, method) -> str:
|
|
|
|
|
18 |
method_templates = {
|
19 |
CardReadingMethod.PAST_PRESENT_FUTURE: lambda: f"""The provided cards are:
|
20 |
Past: {self._format_card(cards[0])}
|
21 |
Present: {self._format_card(cards[1])}
|
22 |
Future: {self._format_card(cards[2])}
|
23 |
+
|
24 |
+
The context are:
|
25 |
+
{context}
|
26 |
""",
|
27 |
CardReadingMethod.CELTIC_CROSS: lambda: f"""The provided cards are:
|
28 |
The situation: {self._format_card(cards[0])}
|
|
|
35 |
What you need to know: {self._format_card(cards[7])}
|
36 |
Your hopes and fears: {self._format_card(cards[8])}
|
37 |
Outcomes: {self._format_card(cards[9])}
|
38 |
+
|
39 |
+
The context are:
|
40 |
+
{context}
|
41 |
""",
|
42 |
CardReadingMethod.HAND_OF_ERIS: lambda: f"""The provided cards are:
|
43 |
About your question: {self._format_card(cards[0])}
|
|
|
45 |
What may hinder you: {self._format_card(cards[2])}
|
46 |
Possible outcome number 1: {self._format_card(cards[3])}
|
47 |
Possible outcome number 2: {self._format_card(cards[4])}
|
48 |
+
|
49 |
+
The context are:
|
50 |
+
{context}
|
51 |
""",
|
52 |
}
|
53 |
|
54 |
+
return method_templates[method]()
|
55 |
+
|
56 |
+
|
57 |
+
def generate_interpretation(self, cards: List[Card], context: str, method: CardReadingMethod) -> str:
|
58 |
+
prompt = f"""
|
59 |
+
You are specialized tarot reader. Your task is provide an insighful reading based on the drawn cards.
|
60 |
+
|
61 |
+
Reading Guidelines:
|
62 |
+
- Keep answers brief, focused and concise.
|
63 |
+
- Only interpret reversed card when specified.
|
64 |
+
- If a context is present, focous the reading on the context.
|
65 |
+
- If the context is not present, give a daily life reading.
|
66 |
+
- Do not mention the method name in the reading.
|
67 |
|
68 |
+
Use the Rider Waiter Tarot symbolism and card imagery
|
69 |
+
|
70 |
+
{self.complet_prompt(cards, context, method)}
|
71 |
+
|
72 |
+
Before answers think step by step and make sure the the answer is complete for all the cards and giving a complete insight.
|
73 |
+
|
74 |
+
"""
|
75 |
+
messages = [
|
76 |
+
{
|
77 |
+
"role": "user",
|
78 |
+
"content": prompt
|
79 |
+
}
|
80 |
]
|
81 |
|
82 |
+
result = self.client.chat.completions.create(
|
83 |
+
model="google/gemma-2-2b-it",
|
84 |
+
messages=messages,
|
85 |
+
max_tokens=1024,
|
86 |
+
stream=False
|
|
|
|
|
|
|
|
|
87 |
)
|
88 |
+
|
89 |
+
return result["choices"][0]["message"]["content"]
|
modules/llm/rules.py
DELETED
@@ -1,23 +0,0 @@
|
|
1 |
-
content = """
|
2 |
-
You are a masterful tarot reader with deep esoteric knowledge. Provide an insightful and focused reading based on the drawn cards.
|
3 |
-
|
4 |
-
Reading Guidelines:
|
5 |
-
- Keep answers brief and focused;
|
6 |
-
- Provide a summary overview at the end;
|
7 |
-
- Only interpret reversed cards when specified;
|
8 |
-
- With context: Focus on context-specific interpretation;
|
9 |
-
- Without context: Give practical daily guidance;
|
10 |
-
- Do not mention the method name in the reading overview;
|
11 |
-
- Be direct and concise.
|
12 |
-
|
13 |
-
If other context is provided:
|
14 |
-
- Focus on the context provided;
|
15 |
-
- Provide a reading related to the context;
|
16 |
-
- Focus primarily on interpreting within the given context
|
17 |
-
- Keep the symbolism of the cards first, but make sure to use the context to interpret the cards.
|
18 |
-
|
19 |
-
Focus on Rider Waite Tarot symbolism and card imagery.
|
20 |
-
Format example:
|
21 |
-
1. Cards meaning;
|
22 |
-
2. If the context is provided, give a practical and short guidance;
|
23 |
-
3. Reading overview."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|