File size: 2,402 Bytes
894f932
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
57
58
59
60
61
62
63
64
65
66
67
from transformers import AutoModelForCausalLM, AutoTokenizer
import gradio as gr

# --- 1. Load Roadmap and Rules ---
def load_roadmap_and_rules(roadmap_path="roadmap.txt", rules_path="rules.txt"):
    roadmap_content = {}
    with open(roadmap_path, 'r') as f:
        current_section = None
        for line in f:
            line = line.strip()
            if line.startswith("#"):
                current_section = line[1:].strip()
                roadmap_content[current_section] = ""
            elif current_section:
                roadmap_content[current_section] += line + "\n"
    with open(rules_path, 'r') as f:
        rules_content = f.read()
    return roadmap_content, rules_content

roadmap, rules = load_roadmap_and_rules()

# --- 2. Load the AI Model (Mistral 7B) ---
model_name = "mistralai/Mistral-7B-Instruct-v0.2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)

# --- 3. Function to Get Chatbot Response ---
def get_chatbot_response(user_query, project_phase, roadmap, rules, model, tokenizer):
    phase_roadmap_section = roadmap.get(project_phase, "General Guidance")
    context = f"""
    Project Roadmap (Phase: {project_phase}):
    {phase_roadmap_section}

    Project Rules:
    {rules}

    User Query: {user_query}

    ---
    Provide helpful guidance for this project.
    """
    prompt = f"<s>[INST] {context} [/INST]"
    inputs = tokenizer(prompt, return_tensors="pt")
    outputs = model.generate(**inputs, max_new_tokens=300)
    response = tokenizer.decode(outputs[0], skip_special_tokens=True)
    response_start_index = response.find("[/INST]") + len("[/INST]") if "[/INST]" in response else 0
    cleaned_response = response[response_start_index:].strip()
    return cleaned_response

# --- 4. Gradio Interface Function ---
def chatbot_interface(user_query, project_phase):
    return get_chatbot_response(user_query, project_phase, roadmap, rules, model, tokenizer)

# --- 5. Gradio Interface Setup ---
iface = gr.Interface(
    fn=chatbot_interface,
    inputs=[
        gr.Textbox(label="Your Question"),
        gr.Dropdown(list(roadmap.keys()), label="Project Phase", value=list(roadmap.keys())[0])
    ],
    outputs="text",
    title="Project Guidance Chatbot",
    description="Ask questions about your project phase and get guidance."
)

if __name__ == "__main__":
    iface.launch()