AiCoderv2 commited on
Commit
819ac9a
·
verified ·
1 Parent(s): da33207

Deploy Gradio app with multiple files

Browse files
Files changed (5) hide show
  1. app.py +31 -0
  2. config.py +3 -0
  3. models.py +47 -0
  4. requirements.txt +5 -0
  5. utils.py +6 -0
app.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from models import load_model, generate_response
3
+ from utils import format_history
4
+ from config import MODEL_NAME, MAX_LENGTH, TEMPERATURE
5
+
6
+ def chat_response(message, history):
7
+ # Format history for the model
8
+ formatted_history = format_history(history)
9
+ # Generate response using the model
10
+ response = generate_response(message, formatted_history, max_length=MAX_LENGTH, temperature=TEMPERATURE)
11
+ return response
12
+
13
+ with gr.Blocks() as demo:
14
+ gr.HTML("""
15
+ <div style="text-align: center; padding: 10px;">
16
+ <h1>AI Chatbot for Chat and Code</h1>
17
+ <p>Powered by <a href="https://huggingface.co/microsoft/Phi-2">microsoft/Phi-2</a></p>
18
+ <p><a href="https://huggingface.co/spaces/akhaliq/anycoder">Built with anycoder</a></p>
19
+ </div>
20
+ """)
21
+
22
+ chatbot = gr.ChatInterface(
23
+ fn=chat_response,
24
+ title="Chat and Code Assistant",
25
+ description="Ask me anything about coding, chat, or general questions!",
26
+ examples=["Write a Python function to reverse a string", "Explain recursion", "Hello, how are you?"],
27
+ theme=gr.themes.Soft()
28
+ )
29
+
30
+ if __name__ == "__main__":
31
+ demo.launch()
config.py ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ MODEL_NAME = "microsoft/Phi-2"
2
+ MAX_LENGTH = 512
3
+ TEMPERATURE = 0.7
models.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ from config import MODEL_NAME
4
+ import spaces
5
+
6
+ model = None
7
+ tokenizer = None
8
+
9
+ @spaces.GPU
10
+ def load_model():
11
+ global model, tokenizer
12
+ if model is None or tokenizer is None:
13
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
14
+ model = AutoModelForCausalLM.from_pretrained(
15
+ MODEL_NAME,
16
+ torch_dtype=torch.float16,
17
+ device_map="auto"
18
+ )
19
+ model.eval()
20
+ return model, tokenizer
21
+
22
+ @spaces.GPU
23
+ def generate_response(message, history, max_length=512, temperature=0.7):
24
+ model, tokenizer = load_model()
25
+
26
+ # Prepare input
27
+ if history:
28
+ input_text = history + f"\nUser: {message}\nAssistant:"
29
+ else:
30
+ input_text = f"User: {message}\nAssistant:"
31
+
32
+ inputs = tokenizer(input_text, return_tensors="pt").to(model.device)
33
+
34
+ with torch.no_grad():
35
+ outputs = model.generate(
36
+ **inputs,
37
+ max_length=max_length,
38
+ temperature=temperature,
39
+ do_sample=True,
40
+ pad_token_id=tokenizer.eos_token_id
41
+ )
42
+
43
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
44
+ # Extract only the assistant's response
45
+ if "Assistant:" in response:
46
+ response = response.split("Assistant:")[-1].strip()
47
+ return response
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio>=4.0.0
2
+ transformers>=4.30.0
3
+ torch>=2.0.0
4
+ accelerate>=0.20.0
5
+ spaces>=0.15.0
utils.py ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ def format_history(history):
2
+ """Format chat history for the model input."""
3
+ formatted = ""
4
+ for user_msg, assistant_msg in history:
5
+ formatted += f"User: {user_msg}\nAssistant: {assistant_msg}\n"
6
+ return formatted.strip()