Spaces:
Sleeping
Sleeping
MefhigosetH
commited on
Commit
•
66f7e10
1
Parent(s):
abbb517
fix(app): Eliminamos pipeline porque no lo soporta el hardware de HF.
Browse files- README.md +4 -1
- app.py +39 -48
- requirements.txt +0 -1
README.md
CHANGED
@@ -12,4 +12,7 @@ short_description: Respondo cualquier consulta sobre el mundo mágico!
|
|
12 |
---
|
13 |
|
14 |
## Hola, soy Harry Potter
|
15 |
-
Pregúntame lo que quieras sobre magia !
|
|
|
|
|
|
|
|
12 |
---
|
13 |
|
14 |
## Hola, soy Harry Potter
|
15 |
+
Pregúntame lo que quieras sobre magia !
|
16 |
+
|
17 |
+
## Referencias
|
18 |
+
* https://github.com/langchain-ai/langchain/issues/4900#issuecomment-1557176230
|
app.py
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
from langchain_huggingface import HuggingFaceEndpoint, HuggingFacePipeline
|
4 |
from langchain_core.prompts import PromptTemplate
|
5 |
from langchain.globals import set_verbose, set_debug
|
@@ -9,12 +8,15 @@ def isDevelopmentEnv():
|
|
9 |
return "DEVELOPMENT" in os.environ
|
10 |
|
11 |
|
|
|
12 |
def initPrompt():
|
13 |
-
template = """[INST]Tu eres Harry Potter, el
|
14 |
-
Responde amablemente a la consulta del usuario basado en la información disponible
|
15 |
-
Si no sabes la respuesta, pide al usuario que intente reformular su consulta.
|
|
|
|
|
16 |
|
17 |
-
{question}
|
18 |
[/INST]
|
19 |
"""
|
20 |
|
@@ -24,60 +26,47 @@ def initPrompt():
|
|
24 |
|
25 |
|
26 |
def initLLM():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
model_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
|
28 |
-
#model_id = "meta-llama/Meta-Llama-3.1-8B-Instruct"
|
29 |
-
#model_id = "HuggingFaceH4/zephyr-7b-beta"
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
)
|
42 |
-
|
43 |
-
else:
|
44 |
-
tokenizer = AutoTokenizer.from_pretrained( model_id )
|
45 |
-
|
46 |
-
model = AutoModelForCausalLM.from_pretrained(
|
47 |
-
model_id,
|
48 |
-
device_map = "auto",
|
49 |
-
load_in_8bit = True
|
50 |
-
)
|
51 |
-
|
52 |
-
llm_pipeline = pipeline(
|
53 |
-
task = "text-generation",
|
54 |
-
model = model,
|
55 |
-
temperature = 0.5,
|
56 |
-
max_length = 256,
|
57 |
-
tokenizer = tokenizer
|
58 |
-
)
|
59 |
-
|
60 |
-
llm = HuggingFacePipeline( pipeline=bloom_pipeline )
|
61 |
|
62 |
return llm
|
63 |
|
64 |
|
|
|
65 |
def respond(message, history):
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
# history_langchain_format.append(HumanMessage(content=message))
|
73 |
-
|
74 |
-
response = llm_chain.invoke(message)
|
75 |
-
return response
|
76 |
|
|
|
77 |
|
78 |
|
79 |
|
80 |
if __name__ == "__main__":
|
|
|
|
|
|
|
81 |
|
82 |
if isDevelopmentEnv():
|
83 |
set_verbose(True)
|
@@ -93,7 +82,9 @@ if __name__ == "__main__":
|
|
93 |
)
|
94 |
|
95 |
demo = gr.ChatInterface(
|
96 |
-
respond
|
|
|
|
|
97 |
)
|
98 |
|
99 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
|
|
2 |
from langchain_huggingface import HuggingFaceEndpoint, HuggingFacePipeline
|
3 |
from langchain_core.prompts import PromptTemplate
|
4 |
from langchain.globals import set_verbose, set_debug
|
|
|
8 |
return "DEVELOPMENT" in os.environ
|
9 |
|
10 |
|
11 |
+
|
12 |
def initPrompt():
|
13 |
+
template = """[INST]Tu eres Harry Potter, el estudiante de magia más hábil de todo el mundo mágico.
|
14 |
+
Responde amablemente a la consulta del usuario basado en la información disponible y a las siguientes reglas:
|
15 |
+
1. Si no sabes la respuesta, pide al usuario que intente reformular su consulta.
|
16 |
+
2. Responde siempre en idioma Español.
|
17 |
+
3. Da respuestas únicamente relacionadas al mundo mágico.
|
18 |
|
19 |
+
Consulta: {question}
|
20 |
[/INST]
|
21 |
"""
|
22 |
|
|
|
26 |
|
27 |
|
28 |
def initLLM():
|
29 |
+
"""
|
30 |
+
Inicializamos el modelo LLM.
|
31 |
+
|
32 |
+
Otros modelos que podríamos usar:
|
33 |
+
- meta-llama/Meta-Llama-3.1-8B-Instruct
|
34 |
+
- HuggingFaceH4/zephyr-7b-beta
|
35 |
+
"""
|
36 |
+
|
37 |
model_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
|
|
|
|
|
38 |
|
39 |
+
llm = HuggingFaceEndpoint(
|
40 |
+
repo_id = model_id,
|
41 |
+
task = "text-generation",
|
42 |
+
temperature = 0.5,
|
43 |
+
model_kwargs = {
|
44 |
+
"min_length": 200,
|
45 |
+
"max_length": 2000,
|
46 |
+
"num_return_sequences": 1
|
47 |
+
}
|
48 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
return llm
|
51 |
|
52 |
|
53 |
+
|
54 |
def respond(message, history):
|
55 |
+
response = ""
|
56 |
+
|
57 |
+
try:
|
58 |
+
response = llm_chain.invoke(message)
|
59 |
+
except:
|
60 |
+
raise gradio.Error("Se ha producido un error al interactuar con el modelo LLM.", duratio=5)
|
|
|
|
|
|
|
|
|
61 |
|
62 |
+
return response
|
63 |
|
64 |
|
65 |
|
66 |
if __name__ == "__main__":
|
67 |
+
"""
|
68 |
+
Entrypoint de la app.
|
69 |
+
"""
|
70 |
|
71 |
if isDevelopmentEnv():
|
72 |
set_verbose(True)
|
|
|
82 |
)
|
83 |
|
84 |
demo = gr.ChatInterface(
|
85 |
+
fn = respond,
|
86 |
+
title = "Hola 👋! Soy Harry Potter ⚡",
|
87 |
+
description = "Intentaré responder cualquier consulta relacionada a Hogwarts, animales fantásticos y al mundo mágico en general. Adelante!"
|
88 |
)
|
89 |
|
90 |
demo.launch()
|
requirements.txt
CHANGED
@@ -2,4 +2,3 @@ huggingface_hub
|
|
2 |
langchain-huggingface
|
3 |
langchain
|
4 |
langchain-core
|
5 |
-
accelerate
|
|
|
2 |
langchain-huggingface
|
3 |
langchain
|
4 |
langchain-core
|
|