Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,10 @@
|
|
1 |
import pandas as pd
|
2 |
import gradio as gr
|
3 |
-
from transformers import
|
4 |
-
import torch
|
5 |
|
6 |
-
|
7 |
-
|
|
|
8 |
|
9 |
# Dados iniciais
|
10 |
data = {
|
@@ -15,38 +15,24 @@ data = {
|
|
15 |
}
|
16 |
df = pd.DataFrame(data)
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
# Função para adicionar feedback
|
19 |
def add_feedback(nome, feedback):
|
20 |
global df
|
21 |
-
df
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
# Converte o DataFrame para string CSV
|
27 |
-
csv_data = df.to_csv(index=False)
|
28 |
-
# Cria contexto com feedback
|
29 |
-
context = f"""
|
30 |
-
Aqui estão os dados das pessoas incluindo seus nomes, idades, cidades onde moram e feedback:
|
31 |
-
|
32 |
-
{csv_data}
|
33 |
-
|
34 |
-
"""
|
35 |
-
inputs = tokenizer(query, return_tensors="pt", padding=True, truncation=True)
|
36 |
-
outputs = model(**inputs)
|
37 |
-
prediction = torch.argmax(outputs.logits, dim=1)
|
38 |
-
labels = df.columns
|
39 |
-
predicted_label = labels[prediction]
|
40 |
-
return predicted_label
|
41 |
-
|
42 |
-
|
43 |
-
def ask_question(pergunta):
|
44 |
-
resposta = get_gpt_response(pergunta)
|
45 |
-
return resposta
|
46 |
-
|
47 |
-
def submit_feedback(nome, feedback):
|
48 |
-
updated_df = add_feedback(nome, feedback)
|
49 |
-
return updated_df
|
50 |
|
51 |
with gr.Blocks() as demo:
|
52 |
gr.Markdown("# Sistema de Consulta e Feedback de Dados")
|
@@ -54,16 +40,16 @@ with gr.Blocks() as demo:
|
|
54 |
with gr.Row():
|
55 |
with gr.Column():
|
56 |
question_input = gr.Textbox(label="Faça uma Pergunta")
|
57 |
-
|
58 |
ask_button = gr.Button("Perguntar")
|
59 |
|
60 |
with gr.Column():
|
61 |
name_input = gr.Textbox(label="Nome para Feedback")
|
62 |
feedback_input = gr.Textbox(label="Feedback")
|
|
|
63 |
submit_button = gr.Button("Enviar Feedback")
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
submit_button.click(fn=submit_feedback, inputs=[name_input, feedback_input], outputs=feedback_df)
|
68 |
|
69 |
demo.launch()
|
|
|
1 |
import pandas as pd
|
2 |
import gradio as gr
|
3 |
+
from transformers import GPT2Tokenizer, GPT2LMHeadModel
|
|
|
4 |
|
5 |
+
# Carregando o modelo e o tokenizador do GPT-2
|
6 |
+
tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
|
7 |
+
model = GPT2LMHeadModel.from_pretrained('gpt2')
|
8 |
|
9 |
# Dados iniciais
|
10 |
data = {
|
|
|
15 |
}
|
16 |
df = pd.DataFrame(data)
|
17 |
|
18 |
+
# Função para responder perguntas com GPT-2
|
19 |
+
def answer_question_with_gpt(question):
|
20 |
+
# Supondo que você queira incorporar dados do DataFrame na pergunta
|
21 |
+
prompt = f"Considerando os dados: {df.to_string(index=False)}. Pergunta: {question} Resposta:"
|
22 |
+
input_ids = tokenizer.encode(prompt, return_tensors='pt')
|
23 |
+
max_length = len(input_ids[0]) + 50 # Define um limite máximo razoável para o comprimento da resposta
|
24 |
+
generated_ids = model.generate(input_ids, max_length=max_length)
|
25 |
+
generated_text = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
|
26 |
+
return generated_text.split("Resposta:")[1] if "Resposta:" in generated_text else generated_text
|
27 |
+
|
28 |
# Função para adicionar feedback
|
29 |
def add_feedback(nome, feedback):
|
30 |
global df
|
31 |
+
if nome in df['Nome'].values:
|
32 |
+
df.loc[df['Nome'] == nome, 'Feedback'] = feedback
|
33 |
+
return "Feedback adicionado com sucesso."
|
34 |
+
else:
|
35 |
+
return "Nome não encontrado no DataFrame."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
with gr.Blocks() as demo:
|
38 |
gr.Markdown("# Sistema de Consulta e Feedback de Dados")
|
|
|
40 |
with gr.Row():
|
41 |
with gr.Column():
|
42 |
question_input = gr.Textbox(label="Faça uma Pergunta")
|
43 |
+
answer_output = gr.Textbox(label="Resposta", interactive=False)
|
44 |
ask_button = gr.Button("Perguntar")
|
45 |
|
46 |
with gr.Column():
|
47 |
name_input = gr.Textbox(label="Nome para Feedback")
|
48 |
feedback_input = gr.Textbox(label="Feedback")
|
49 |
+
feedback_result = gr.Textbox(label="Resultado do Feedback", interactive=False)
|
50 |
submit_button = gr.Button("Enviar Feedback")
|
51 |
+
|
52 |
+
ask_button.click(fn=answer_question_with_gpt, inputs=question_input, outputs=answer_output)
|
53 |
+
submit_button.click(fn=add_feedback, inputs=[name_input, feedback_input], outputs=feedback_result)
|
|
|
54 |
|
55 |
demo.launch()
|