nafisneehal commited on
Commit
addaa24
·
verified ·
1 Parent(s): ecfd995

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +113 -0
app.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+ import os
5
+
6
+ # File to store model links
7
+ MODEL_FILE = "model_links.txt"
8
+
9
+ def load_model_links():
10
+ # """Load model links from file"""
11
+ # if not os.path.exists(MODEL_FILE):
12
+ # # Create default file with some example models
13
+ # with open(MODEL_FILE, "w") as f:
14
+ # f.write("facebook/opt-125m\n")
15
+ # f.write("facebook/opt-350m\n")
16
+
17
+ with open(MODEL_FILE, "r") as f:
18
+ return [line.strip() for line in f.readlines() if line.strip()]
19
+
20
+ class ModelManager:
21
+ def __init__(self):
22
+ self.current_model = None
23
+ self.current_tokenizer = None
24
+ self.current_model_name = None
25
+
26
+ def load_model(self, model_name):
27
+ """Load model and free previous model's memory"""
28
+ if self.current_model is not None:
29
+ del self.current_model
30
+ del self.current_tokenizer
31
+ torch.cuda.empty_cache()
32
+
33
+ self.current_tokenizer = AutoTokenizer.from_pretrained(model_name)
34
+ self.current_model = AutoModelForCausalLM.from_pretrained(model_name)
35
+ self.current_model_name = model_name
36
+ return f"Loaded model: {model_name}"
37
+
38
+ def generate_response(self, system_message, user_message):
39
+ """Generate response from the model"""
40
+ if self.current_model is None:
41
+ return "Please select and load a model first."
42
+
43
+ # Combine system and user messages
44
+ prompt = f"{system_message}\n\nUser: {user_message}\n\nAssistant:"
45
+
46
+ # Generate response
47
+ inputs = self.current_tokenizer(prompt, return_tensors="pt", padding=True)
48
+ outputs = self.current_model.generate(
49
+ inputs.input_ids,
50
+ max_length=200,
51
+ num_return_sequences=1,
52
+ temperature=0.7,
53
+ pad_token_id=self.current_tokenizer.eos_token_id
54
+ )
55
+
56
+ response = self.current_tokenizer.decode(outputs[0], skip_special_tokens=True)
57
+ # Extract only the assistant's response
58
+ response = response.split("Assistant:")[-1].strip()
59
+ return response
60
+
61
+ # Initialize model manager
62
+ model_manager = ModelManager()
63
+
64
+ # Create Gradio interface
65
+ with gr.Blocks() as demo:
66
+ gr.Markdown("# Chat Interface with Model Selection")
67
+
68
+ with gr.Row():
69
+ with gr.Column(scale=1):
70
+ # Input components
71
+ model_dropdown = gr.Dropdown(
72
+ choices=load_model_links(),
73
+ label="Select Model",
74
+ info="Choose a model from the list"
75
+ )
76
+ load_button = gr.Button("Load Selected Model")
77
+ system_msg = gr.Textbox(
78
+ label="System Message",
79
+ placeholder="Enter system message here...",
80
+ lines=3
81
+ )
82
+ user_msg = gr.Textbox(
83
+ label="User Message",
84
+ placeholder="Enter your message here...",
85
+ lines=3
86
+ )
87
+ submit_button = gr.Button("Generate Response")
88
+
89
+ with gr.Column(scale=1):
90
+ # Output components
91
+ model_status = gr.Textbox(label="Model Status")
92
+ chat_output = gr.Textbox(
93
+ label="Assistant Response",
94
+ lines=10,
95
+ interactive=False
96
+ )
97
+
98
+ # Event handlers
99
+ load_button.click(
100
+ fn=model_manager.load_model,
101
+ inputs=[model_dropdown],
102
+ outputs=[model_status]
103
+ )
104
+
105
+ submit_button.click(
106
+ fn=model_manager.generate_response,
107
+ inputs=[system_msg, user_msg],
108
+ outputs=[chat_output]
109
+ )
110
+
111
+ # Launch the app
112
+ if __name__ == "__main__":
113
+ demo.launch()