Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,85 +1,106 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
TITLE = "ChatGEO"
|
| 4 |
-
EXAMPLE_INPUT = "Olá, ChatGEO! Como você pode me ajudar hoje?"
|
| 5 |
-
from typing import Iterator
|
| 6 |
-
|
| 7 |
import gradio as gr
|
| 8 |
-
import
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
"""
|
| 33 |
-
Sends a POST request to the predefined Zephyr-7b-Beta URL and returns the JSON response.
|
| 34 |
-
"""
|
| 35 |
-
response = requests.post(zephyr_7b_beta, headers=HEADERS, json=payload)
|
| 36 |
-
response.raise_for_status() # Will raise an HTTPError if the HTTP request returned an unsuccessful status code
|
| 37 |
-
return response.json()
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
def predict_beta(message, chatbot=[], system_prompt=""):
|
| 41 |
-
input_prompt = build_input_prompt(message, chatbot, system_prompt)
|
| 42 |
-
data = {
|
| 43 |
-
"inputs": input_prompt
|
| 44 |
-
}
|
| 45 |
-
|
| 46 |
-
try:
|
| 47 |
-
response_data = post_request_beta(data)
|
| 48 |
-
json_obj = response_data[0]
|
| 49 |
-
|
| 50 |
-
if 'generated_text' in json_obj and len(json_obj['generated_text']) > 0:
|
| 51 |
-
bot_message = json_obj['generated_text']
|
| 52 |
-
return bot_message
|
| 53 |
-
elif 'error' in json_obj:
|
| 54 |
-
raise gr.Error(json_obj['error'] + ' Please refresh and try again with smaller input prompt')
|
| 55 |
-
else:
|
| 56 |
-
warning_msg = f"Unexpected response: {json_obj}"
|
| 57 |
-
raise gr.Error(warning_msg)
|
| 58 |
-
except requests.HTTPError as e:
|
| 59 |
-
error_msg = f"Request failed with status code {e.response.status_code}"
|
| 60 |
-
raise gr.Error(error_msg)
|
| 61 |
-
except json.JSONDecodeError as e:
|
| 62 |
-
error_msg = f"Failed to decode response as JSON: {str(e)}"
|
| 63 |
-
raise gr.Error(error_msg)
|
| 64 |
-
|
| 65 |
-
def test_preview_chatbot(message, history):
|
| 66 |
-
response = predict_beta(message, history, SYSTEM_PROMPT)
|
| 67 |
-
text_start = response.rfind("<|assistant|>", ) + len("<|assistant|>")
|
| 68 |
-
response = response[text_start:]
|
| 69 |
-
return response
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
welcome_preview_message = f"""
|
| 74 |
-
Bem vindo ao **{TITLE}** Pergunte-me algo como:
|
| 75 |
-
**"{EXAMPLE_INPUT}"**
|
| 76 |
-
"""
|
| 77 |
-
|
| 78 |
-
chatbot_preview = gr.Chatbot(layout="panel", value=[(None, welcome_preview_message)],
|
| 79 |
-
avatar_images=["./user.png", "./botz.png"], bubble_full_width=False, show_label=False, show_copy_button=False,)
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
demo.queue().launch(show_api=False)
|
|
|
|
| 1 |
+
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
+
import random
|
| 4 |
+
|
| 5 |
+
client = InferenceClient("google/gemma-2b-it")
|
| 6 |
+
|
| 7 |
+
def format_prompt(message, history):
|
| 8 |
+
prompt = "O ChatGEO é um assistente virtual especializado em geografia, projetado para ajudar estudantes a explorar e compreender diversos tópicos relacionados ao mundo ao seu redor. Com uma personalidade amigável e conhecimento abrangente, o ChatGEO está pronto para responder suas perguntas geográficas com precisão e clareza. Exemplos de Perguntas para ChatGEO: Qual é a capital do Brasil? Descreva o processo de formação de uma cadeia de montanhas. Quais são os principais rios da América do Sul? Explique o conceito de zonas climáticas e suas características. Como a globalização impacta as relações entre os países? Quais são as diferenças entre latitude e longitude? Liste os países que fazem parte da União Europeia. O que são fenômenos naturais e como eles afetam o ambiente?Fale sobre as características geográficas da África Subsaariana. Como os vulcões se formam e quais são seus efeitos na paisagem? Ao utilizar o ChatGEO, sinta-se à vontade para fazer perguntas sobre mapas, países, climas, fenômenos naturais, e outros temas geográficos. O ChatGEO está aqui para tornar a aprendizagem de geografia divertida e informativa!"
|
| 9 |
+
if history:
|
| 10 |
+
for user_prompt, bot_response in history:
|
| 11 |
+
prompt += f"<start_of_turn>user{user_prompt}<end_of_turn>"
|
| 12 |
+
prompt += f"<start_of_turn>model{bot_response}"
|
| 13 |
+
prompt += f"<start_of_turn>user{message}<end_of_turn><start_of_turn>model"
|
| 14 |
+
return prompt
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def generate(prompt, history, temperature=0.7, max_new_tokens=1024, top_p=0.90, repetition_penalty=0.9):
|
| 18 |
+
temperature = float(temperature)
|
| 19 |
+
if temperature < 1e-2:
|
| 20 |
+
temperature = 1e-2
|
| 21 |
+
top_p = float(top_p)
|
| 22 |
+
|
| 23 |
+
if not history:
|
| 24 |
+
history = []
|
| 25 |
+
|
| 26 |
+
rand_seed = random.randint(1, 1111111111111111)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
generate_kwargs = dict(
|
| 29 |
+
temperature=temperature,
|
| 30 |
+
max_new_tokens=max_new_tokens,
|
| 31 |
+
top_p=top_p,
|
| 32 |
+
repetition_penalty=repetition_penalty,
|
| 33 |
+
do_sample=True,
|
| 34 |
+
seed=rand_seed,
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
formatted_prompt = format_prompt(prompt, history)
|
| 38 |
+
|
| 39 |
+
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
| 40 |
+
output = ""
|
| 41 |
+
|
| 42 |
+
for response in stream:
|
| 43 |
+
output += response.token.text
|
| 44 |
+
yield output
|
| 45 |
+
history.append((prompt, output))
|
| 46 |
+
return output
|
| 47 |
|
| 48 |
+
|
| 49 |
+
mychatbot = gr.Chatbot(
|
| 50 |
+
avatar_images=["./user.png", "./botgm.png"], bubble_full_width=False, show_label=False, show_copy_button=True, likeable=True,)
|
| 51 |
+
|
| 52 |
+
additional_inputs=[
|
| 53 |
+
gr.Slider(
|
| 54 |
+
label="Temperature",
|
| 55 |
+
value=0.7,
|
| 56 |
+
minimum=0.0,
|
| 57 |
+
maximum=1.0,
|
| 58 |
+
step=0.01,
|
| 59 |
+
interactive=True,
|
| 60 |
+
info="Higher values generate more diverse outputs",
|
| 61 |
+
),
|
| 62 |
+
gr.Slider(
|
| 63 |
+
label="Max new tokens",
|
| 64 |
+
value=6400,
|
| 65 |
+
minimum=0,
|
| 66 |
+
maximum=8000,
|
| 67 |
+
step=64,
|
| 68 |
+
interactive=True,
|
| 69 |
+
info="The maximum numbers of new tokens",
|
| 70 |
+
),
|
| 71 |
+
gr.Slider(
|
| 72 |
+
label="Top-p",
|
| 73 |
+
value=0.90,
|
| 74 |
+
minimum=0.0,
|
| 75 |
+
maximum=1,
|
| 76 |
+
step=0.01,
|
| 77 |
+
interactive=True,
|
| 78 |
+
info="Higher values sample more low-probability tokens",
|
| 79 |
+
),
|
| 80 |
+
gr.Slider(
|
| 81 |
+
label="Repetition penalty",
|
| 82 |
+
value=1.0,
|
| 83 |
+
minimum=0.1,
|
| 84 |
+
maximum=2.0,
|
| 85 |
+
step=0.1,
|
| 86 |
+
interactive=True,
|
| 87 |
+
info="Penalize repeated tokens",
|
| 88 |
+
)
|
| 89 |
+
]
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
iface = gr.ChatInterface(fn=generate,
|
| 94 |
+
chatbot=mychatbot,
|
| 95 |
+
additional_inputs=additional_inputs,
|
| 96 |
+
submit_btn='Enviar',
|
| 97 |
+
retry_btn=None,
|
| 98 |
+
undo_btn=None,
|
| 99 |
+
clear_btn=None
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
with gr.Blocks() as demo:
|
| 103 |
+
gr.HTML("<center><h2 style='font-size: 22px; text-align: center; color: #007BFF;'>ChatGEO IA</h2><p><b>Tire suas dúvidas, peça sugestões sobre os assuntos da Geografia e muito mais!</b></p></center>")
|
| 104 |
+
iface.render()
|
| 105 |
+
|
| 106 |
demo.queue().launch(show_api=False)
|