Reality123b commited on
Commit
6989cb8
·
verified ·
1 Parent(s): 4006e28

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -71
app.py CHANGED
@@ -1,78 +1,21 @@
1
  import gradio as gr
2
- import torch
3
- from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
4
 
5
- # Set seed for reproducibility
6
- torch.random.manual_seed(0)
7
-
8
- # Load the model and tokenizer (using Accelerate)
9
- model = AutoModelForCausalLM.from_pretrained(
10
- "microsoft/Phi-3.5-mini-instruct",
11
- device_map="auto", # Managed by Accelerate
12
- torch_dtype="auto",
13
- trust_remote_code=True,
14
- )
15
- tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-3.5-mini-instruct")
16
-
17
- # Define the pipeline (no device argument)
18
- pipe = pipeline(
19
- "text-generation",
20
- model=model,
21
- tokenizer=tokenizer,
22
- )
23
-
24
- # System message (invisible to the user)
25
- SYSTEM_MESSAGE = {"role": "system", "content": "You are a helpful AI assistant."}
26
-
27
- # Function to process the user input and generate output
28
- def chatbot_response(conversation_history):
29
- # Build message sequence
30
- messages = [SYSTEM_MESSAGE] + [
31
- {"role": "user", "content": message["user_input"]} for message in conversation_history
32
- ]
33
- # Pass messages to the model
34
- generation_args = {
35
- "max_new_tokens": 500,
36
- "return_full_text": False,
37
- "temperature": 0.0,
38
- "do_sample": False,
39
- }
40
- output = pipe(messages, **generation_args)
41
- assistant_reply = output[0]["generated_text"]
42
- # Append assistant's response to history
43
- conversation_history[-1]["assistant_reply"] = assistant_reply
44
- return conversation_history
45
-
46
- # Define Gradio interface
47
- with gr.Blocks() as demo:
48
- gr.Markdown("# AI Chatbot with System Message")
49
 
50
- with gr.Row():
51
- with gr.Column():
52
- chatbox = gr.Chatbot(label="Conversation")
53
- input_box = gr.Textbox(label="Your Message", placeholder="Type your message here...")
54
- submit_btn = gr.Button("Submit")
55
 
56
- conversation_state = gr.State([]) # Maintain conversation history
 
 
57
 
58
- # Function to update the conversation
59
- def update_conversation(user_input, history):
60
- if user_input.strip():
61
- history.append({"user_input": user_input})
62
- updated_history = chatbot_response(history)
63
- # Format conversation history for the Chatbot component
64
- formatted_conversation = [
65
- (msg["user_input"], msg.get("assistant_reply", ""))
66
- for msg in updated_history
67
- ]
68
- return formatted_conversation, updated_history, ""
69
- return [], history, ""
70
 
71
- submit_btn.click(
72
- update_conversation,
73
- inputs=[input_box, conversation_state],
74
- outputs=[chatbox, conversation_state, input_box],
75
- )
76
 
77
- # Launch the interface
78
- demo.launch()
 
1
  import gradio as gr
 
 
2
 
3
+ def custom_model_input(input_text):
4
+ # First prompt to be used before anything else
5
+ system_prompt = "you are xylaria 1.4 senoa, developed by sk md saad amin"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
+ # Combine system prompt with user input
8
+ prompt = f"{system_prompt} User's input: {input_text}"
 
 
 
9
 
10
+ # Call your model with the combined prompt
11
+ response = your_model_function(prompt) # Replace this with actual model call
12
+ return response
13
 
14
+ # Create the interface
15
+ interface = gr.Interface(fn=custom_model_input, inputs=gr.Textbox(), outputs=gr.Textbox())
 
 
 
 
 
 
 
 
 
 
16
 
17
+ # Load the model
18
+ interface.load("models/Qwen/QwQ-32B-Preview")
 
 
 
19
 
20
+ # Start the interface
21
+ interface.launch()