muhammadrazapathan commited on
Commit
174cd13
·
verified ·
1 Parent(s): 9254a9c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -0
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
3
+ import torch
4
+
5
+ # -------------------------------
6
+ # Load TinyLlama Model (LLaMA-based)
7
+ # -------------------------------
8
+ model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
9
+
10
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
11
+
12
+ model = AutoModelForCausalLM.from_pretrained(
13
+ model_name,
14
+ torch_dtype=torch.float32,
15
+ device_map="auto"
16
+ )
17
+
18
+ pipe = pipeline(
19
+ "text-generation",
20
+ model=model,
21
+ tokenizer=tokenizer,
22
+ max_new_tokens=256,
23
+ temperature=0.7,
24
+ do_sample=True
25
+ )
26
+
27
+ # -------------------------------
28
+ # College Assistant Function
29
+ # -------------------------------
30
+ def college_ai(question):
31
+ if not question:
32
+ return "Please ask a question."
33
+
34
+ prompt = f"""
35
+ You are a helpful college assistant.
36
+ Answer clearly for students.
37
+
38
+ Student Question: {question}
39
+
40
+ Answer:
41
+ """
42
+
43
+ result = pipe(prompt)[0]["generated_text"]
44
+
45
+ # Clean output
46
+ answer = result.split("Answer:")[-1].strip()
47
+ return answer
48
+
49
+
50
+ # -------------------------------
51
+ # Gradio UI
52
+ # -------------------------------
53
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
54
+ gr.Markdown("# 🎓 College AI Assistant (LLaMA Powered)")
55
+ gr.Markdown("Ask any academic question!")
56
+
57
+ user_input = gr.Textbox(
58
+ label="Enter Your Question",
59
+ placeholder="Example: Explain Machine Learning"
60
+ )
61
+
62
+ output = gr.Textbox(label="AI Answer")
63
+
64
+ ask_btn = gr.Button("Ask AI")
65
+
66
+ ask_btn.click(college_ai, inputs=user_input, outputs=output)
67
+
68
+ demo.launch()