File size: 1,557 Bytes
174cd13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
import torch

# -------------------------------
# Load TinyLlama Model (LLaMA-based)
# -------------------------------
model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"

tokenizer = AutoTokenizer.from_pretrained(model_name)

model = AutoModelForCausalLM.from_pretrained(
    model_name,
    torch_dtype=torch.float32,
    device_map="auto"
)

pipe = pipeline(
    "text-generation",
    model=model,
    tokenizer=tokenizer,
    max_new_tokens=256,
    temperature=0.7,
    do_sample=True
)

# -------------------------------
# College Assistant Function
# -------------------------------
def college_ai(question):
    if not question:
        return "Please ask a question."

    prompt = f"""
You are a helpful college assistant.
Answer clearly for students.

Student Question: {question}

Answer:
"""

    result = pipe(prompt)[0]["generated_text"]

    # Clean output
    answer = result.split("Answer:")[-1].strip()
    return answer


# -------------------------------
# Gradio UI
# -------------------------------
with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown("# 🎓 College AI Assistant (LLaMA Powered)")
    gr.Markdown("Ask any academic question!")

    user_input = gr.Textbox(
        label="Enter Your Question",
        placeholder="Example: Explain Machine Learning"
    )

    output = gr.Textbox(label="AI Answer")

    ask_btn = gr.Button("Ask AI")

    ask_btn.click(college_ai, inputs=user_input, outputs=output)

demo.launch()