Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from unsloth import FastLanguageModel
|
2 |
+
|
3 |
+
# 4bit pre quantized models we support for 4x faster downloading + no OOMs.
|
4 |
+
fourbit_models = [
|
5 |
+
"unsloth/mistral-7b-instruct-v0.2-bnb-4bit",
|
6 |
+
"unsloth/gemma-7b-it-bnb-4bit",
|
7 |
+
] # More models at https://huggingface.co/unsloth
|
8 |
+
|
9 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
10 |
+
model_name = "unsloth/Meta-Llama-3.1-8B-Instruct",
|
11 |
+
max_seq_length = 8192,
|
12 |
+
load_in_4bit = True,
|
13 |
+
# token = "hf_...", # use one if using gated models like meta-llama/Llama-2-7b-hf
|
14 |
+
)
|
15 |
+
####
|
16 |
+
from transformers import TextStreamer
|
17 |
+
from unsloth.chat_templates import get_chat_template
|
18 |
+
tokenizer = get_chat_template(
|
19 |
+
tokenizer,
|
20 |
+
chat_template = "llama-3.1",
|
21 |
+
mapping = {"role" : "from", "content" : "value", "user" : "human", "assistant" : "gpt"}, # ShareGPT style
|
22 |
+
)
|
23 |
+
FastLanguageModel.for_inference(model) # Enable native 2x faster inference
|
24 |
+
####
|
25 |
+
from datetime import datetime
|
26 |
+
import pendulum
|
27 |
+
from babel.dates import format_date
|
28 |
+
|
29 |
+
def get_day_month():
|
30 |
+
# using S茫o Paulo time zone
|
31 |
+
SP = pendulum.timezone('America/Sao_Paulo')
|
32 |
+
#
|
33 |
+
date = datetime.now(SP).strftime('%Y/%m/%d')
|
34 |
+
date = datetime.strptime(date, "%Y/%m/%d")
|
35 |
+
# Spanish locale
|
36 |
+
spanish_date = format_date(date, "dd 'de' MMMM", locale='es')
|
37 |
+
|
38 |
+
return (spanish_date)
|
39 |
+
|
40 |
+
#return f"""The {quantity} {animal}s from {" and ".join(countries)} went to the {place} where they {" and ".join(activity_list)} until the {"morning" if morning else "night"}"""
|
41 |
+
def sentence_builder(signo, estilo,tema):
|
42 |
+
dia_texto = get_day_month()
|
43 |
+
texto_request = None
|
44 |
+
#if tema == "General":
|
45 |
+
# texto_request = f"""Actua como un experto en astrolog铆a. Hoy es {dia_texto} Soy del {signo}, haz una predicci贸n sucinta, resumida, y precisa con un estilo {estilo}. No agregues ningun comentario en relaci贸n a la astrolog铆a"""
|
46 |
+
#else:
|
47 |
+
match tema:
|
48 |
+
case "Salud":
|
49 |
+
texto_request = f"""Actua como un experto en astrolog铆a. Hoy es {dia_texto} Soy del {signo}, haz una predicci贸n sucinta, resumida, y precisa sobre {tema}, con un estilo {estilo}. No agregues ningun comentario en relaci贸n a la astrolog铆a"""
|
50 |
+
case "Econ贸mico":
|
51 |
+
texto_request = f"""Actua como un experto en astrolog铆a. Hoy es {dia_texto} Soy del {signo}, haz una predicci贸n sucinta, resumida, y precisa sobre lo {tema}, con un estilo {estilo}. No agregues ningun comentario en relaci贸n a la astrolog铆a"""
|
52 |
+
case "Emocional":
|
53 |
+
texto_request = f"""Actua como un experto en astrolog铆a. Hoy es {dia_texto} Soy del {signo}, haz una predicci贸n sucinta, resumida, y precisa sobre mi vida {tema}, con un estilo {estilo}. No agregues ningun comentario en relaci贸n a la astrolog铆a"""
|
54 |
+
case "Amistades":
|
55 |
+
texto_request = f"""Actua como un experto en astrolog铆a. Hoy es {dia_texto} Soy del {signo}, haz una predicci贸n sucinta, resumida, y precisa sobre mis {tema}, con un estilo {estilo}. No agregues ningun comentario en relaci贸n a la astrolog铆a"""
|
56 |
+
case "Familia":
|
57 |
+
texto_request = f"""Actua como un experto en astrolog铆a. Hoy es {dia_texto} Soy del {signo}, haz una predicci贸n sucinta, resumida, y precisa sobre mi vida {tema}r, con un estilo {estilo}. No agregues ningun comentario en relaci贸n a la astrolog铆a"""
|
58 |
+
case _:
|
59 |
+
texto_request = f"""Actua como un experto en astrolog铆a. Hoy es {dia_texto} Soy del {signo}, haz una predicci贸n sucinta, resumida, y precisa con un estilo {estilo}. No agregues ningun comentario en relaci贸n a la astrolog铆a"""
|
60 |
+
# "Emocional","Amistades","Familia"
|
61 |
+
messages = [
|
62 |
+
{"from": "human", "value": texto_request},
|
63 |
+
]
|
64 |
+
inputs = tokenizer.apply_chat_template(messages, tokenize = True, add_generation_prompt = True, return_tensors = "pt").to("cuda")
|
65 |
+
#
|
66 |
+
model_output = model.generate(input_ids = inputs, streamer = text_streamer, max_new_tokens = 1024, use_cache = True)
|
67 |
+
#
|
68 |
+
model_output = tokenizer.decode(model_output[0].tolist(),skip_special_tokens=True)
|
69 |
+
#print("===============")
|
70 |
+
#print(model_output.split("assistant"))
|
71 |
+
return model_output.split("assistant")[1]
|
72 |
+
|
73 |
+
###########
|
74 |
+
import gradio as gr
|
75 |
+
|
76 |
+
|
77 |
+
demo = gr.Interface(
|
78 |
+
sentence_builder,
|
79 |
+
[
|
80 |
+
gr.Dropdown(
|
81 |
+
["Aries", "Tauro", "G茅minis", "C谩ncer", "Leo", "Virgo", "Libra", "Escorpio", "Sagitario", "Capricornio", "Acuario", "Piscis"], label="Signo", info="Escoge tu signo del Zodiaco"
|
82 |
+
),
|
83 |
+
gr.Dropdown(
|
84 |
+
["Alegre","Serio","Sarc谩stico","Depresivo","Coqueto","Sensual"], label="Estilo", info="Elige el estilo que m谩s gustes!",value="Alegre"
|
85 |
+
),
|
86 |
+
gr.Dropdown(
|
87 |
+
["General","Salud","Econ贸mico","Emocional","Amistades","Familia"], label="Tema", info="Elige el aspecto de tu vida que te preocupa.",value="General"
|
88 |
+
),
|
89 |
+
],
|
90 |
+
"textbox"
|
91 |
+
)
|
92 |
+
|
93 |
+
if __name__ == "__main__":
|
94 |
+
demo.launch()
|