File size: 2,151 Bytes
a771fda
 
 
 
 
 
 
 
d474cef
a771fda
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import gradio as gr
import zipfile
import os

from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline

# Carregando o modelo StarCoder (especialista em programação)
tokenizer = AutoTokenizer.from_pretrained("bigcode/starcoderbase")
model = AutoModelForCausalLM.from_pretrained("bigcode/starcoder", trust_remote_code=True)
gen = pipeline("text-generation", model=model, tokenizer=tokenizer)

def process_input(user_input, files):
    all_text = ""

    if files:
        for file in files:
            if file.name.endswith(".zip"):
                with zipfile.ZipFile(file.name, 'r') as zip_ref:
                    zip_ref.extractall("temp_zip")
                for root, dirs, filenames in os.walk("temp_zip"):
                    for fname in filenames:
                        if fname.endswith((".py", ".txt")):
                            with open(os.path.join(root, fname), 'r', encoding='utf-8', errors='ignore') as f:
                                all_text += f"\n\n# Arquivo: {fname}\n" + f.read()
            else:
                with open(file.name, 'r', encoding='utf-8', errors='ignore') as f:
                    all_text += f"\n\n# Arquivo: {file.name}\n" + f.read()

    prompt = f"""
Você é um assistente especialista em Python. Analise a seguinte mensagem do usuário e os arquivos enviados (se houver). Corrija, melhore ou explique o código conforme necessário.

Mensagem do usuário:
{user_input}

Arquivos enviados:
{all_text}

Responda com sugestões claras e organizadas.
"""

    output = gen(prompt, max_new_tokens=512, temperature=0.2)[0]["generated_text"]
    return output[len(prompt):]  # Remove o prompt da resposta final

iface = gr.Interface(
    fn=process_input,
    inputs=[
        gr.Textbox(label="Sua pergunta ou instrução", placeholder="Ex: Corrija esse código..."),
        gr.File(label="Arquivos (.py, .txt ou .zip)", file_types=[".py", ".txt", ".zip"], file_count="multiple"),
    ],
    outputs=gr.Textbox(label="Resposta da IA"),
    title="Bot IA Python",
    description="Envie sua mensagem e arquivos para corrigir, melhorar ou entender código Python."
)

iface.launch()