RPS1100 / app.py
RohiniPS's picture
Update app.py
bf66f9c verified
from pickle import TRUE
import gradio as gr
from transformers import Qwen2Tokenizer, Qwen2ForCausalLM, AutoModelForCausalLM, AutoTokenizer
import torch
#Labels
sDefaultQuestion = "What is K-means clustering?"
sDescription = "Question & Answer"
sTitle = "Question & Answer application using Qwen"
def ask_question_HFModel1(question, modelHF, tokenizerHF):
inputs = tokenizerHF.encode('Q: ' + question + ' A:', return_tensors='pt')
attention_mask = torch.ones(inputs.shape)
outputs = modelHF.generate(inputs, attention_mask = attention_mask, max_new_tokens=100, num_return_sequences=1, )
gen_text = tokenizerHF.decode(outputs[0], skip_special_tokens=True)
question, answer = gen_text.split(' A:')
return question, answer
def FetchQwenFromSpace(sQ):
HFModelName ="Qwen/Qwen1.5-0.5B"
tokenizer = Qwen2Tokenizer.from_pretrained(HFModelName) #, trust_remote_code=bTrust_remote_code, model_max_length=8192) #model_max_length = 8192 #, use_fast=False #Qwen
model = Qwen2ForCausalLM.from_pretrained(HFModelName)
modelHF = model
tokenizerHF = tokenizer
'''
#Using pre-trained model from HF
#from transformers import AutoModelCasualLM
sHuggingFacePath = "RohiniPS/Qwen1B-QnA-3-5" #"RohiniPS/Qwen1B-QnA-3-5"
modelHF = Qwen2ForCausalLM.from_pretrained(sHuggingFacePath) #("your_username/my-awesome-model")
tokenizerHF = Qwen2Tokenizer.from_pretrained(sHuggingFacePath)
###'''
sQuestion, sAnswer = ask_question_HFModel1(sQ, modelHF, tokenizerHF)
return sQuestion, sAnswer
def GetAnswer(Question):
sQuestion, sAnswer = FetchQwenFromSpace(Question)
# sAnswer = "Qwen 1.5-0.5B"
return sAnswer
# Call Gradio
GradioCall = gr.Interface(fn=GetAnswer,
inputs=gr.TextArea(value=sDefaultQuestion,lines=7, max_lines=20, label="Ask a question related to AIML!",autoscroll=True), #Input - Question Asked
outputs=[gr.TextArea(label="Qwen's Response - ")],
title=sTitle,
description=sDescription,)
# Launch - Qwen's QnA!
GradioCall.launch()
'''
from pickle import TRUE
import gradio as gr
from transformers import Qwen2Tokenizer, Qwen2ForCausalLM
sHuggingFacePath = "RohiniPS/Qwen1B-QnA-3-5"
# Initialize title and description
sDefaultQuestion = "What is K-means clustering?"
sDescription = "Question & Answer"
sTitle = "Question & Answer application using Qwen"
def ask_question_HFModel1(question, modelHF, tokenizerHF):
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
modelHF = modelHF.to(device)
inputs = tokenizerHF.encode('Q: ' + question + ' A:', return_tensors='pt')
#inputs = tokenizerHF.encode('Q: ' + question + ' A:', return_tensors='pt').to(device)
attention_mask = torch.ones(inputs.shape, device=device)
outputs = modelHF.generate(inputs, attention_mask = attention_mask, max_new_tokens=100, num_return_sequences=1, )
gen_text = tokenizerHF.decode(outputs[0], skip_special_tokens=True)
question, answer = gen_text.split(' A:')
return question, answer
def FetchQwenFromSpace(sQ):
#Initialize Tokenizer & Model
modelHF = Qwen2ForCausalLM.from_pretrained(sHuggingFacePath)
tokenizerHF = Qwen2Tokenizer.from_pretrained(sHuggingFacePath)
sQuestion, sAnswer = ask_question_HFModel1(sQ, modelHF, tokenizerHF)
return sQuestion, sAnswer
def GetAnswer(Question):
sQuestion, sAnswer = FetchQwenFromSpace(Question)
# sAnswer += "convnet_from_scratch_with_augmentation.keras is our trained Tensorflow model file. app.py contains our Gradio app (similar to the code that launched the app).Note: app.py is the default filename used for Hugging Face Spaces, if you deploy your app there, Spaces will by default look for a file called app.py to run. This is changable in settings. examples/ contains example images to use with our Gradio app. model.py contains the model defintion as well as any transforms assosciated with the model. requirements.txt contains the dependencies to run our app such as tensorflow, numpy and gradio."
# sAnswer += sAnswer
return sQuestion, sAnswer
# Call Gradio
GradioCall = gr.Interface(fn=GetAnswer,
inputs=gr.TextArea(value=sDefaultQuestion,lines=7, max_lines=20, label="Ask a question related to AIML!",autoscroll=True),
outputs=[gr.Label(label="You asked,"),
gr.TextArea(label="Qwen's Response - ")],
title=sTitle,
description=sDescription,)
# Launch the Gradio!
GradioCall.launch(inline=False, share=True)
'''