Первая проблема: я использовал gpt-neo для создания чат-бота, который будет получать запросы от приложения Flutter, но когда я тестирую его, производительность не очень хорошая, может быть, на некоторые вопросы есть ответы, но есть вопросы, ответы неструктурированы, и кажется типа копировать с сайта или разговора, это нормально? например, я попросил его исправить грамматику в этом предложении «Я хлеб ем много», и ответ был такой
«ответ»: «Здесь глагол «есть» (в моем случае), а НЕ «быть» есть». Теперь единственная ситуация, когда глагол «хлеб» является правильным, — это когда он относится к хлебу. В таких случаях проблему следует решить, добавив отрицание (-n) после хлеба или хлеба-n. В вашем случае « «Напиток» может быть правильным, поскольку «напиток» означает «напиток, напиток» ИЛИ «жидкость (бутылка), называемая соком». «Я пью много сока». Та же ситуация; добавьте отрицательный знак (-n) и «напиток». становится "Я сок не пью много".",
Это производительность gpt-neo или я что-то упускаю? также я попытался добавить их в выходные данные: «Я имел в виду гиперпараметры при генерации выходных данных», но это ухудшило ситуацию, поэтому я прокомментировал их, теперь вторая проблема - это основная проблема, заключающаяся в том, что я использовал две предварительно обученные модели для извлечения текста и обнаружения объектов, но когда я отправляю какое-либо изображение в почтальон, делаю данные формы и загружаю изображение, я уверен, что я ввел и сделал все правильно, но это выдало мне 500 Internal Server Error, здесь я предоставлю код, и ошибка произошла, когда я получил 500 Внутренняя ошибка сервера
Это мой код на Python:
efficientdet_model = hub.load("https://tfhub.dev/tensorflow/efficientdet/d0/1")
# Load GPT-Neo model and tokenizer for text generation
gpt_neo_model = GPTNeoForCausalLM.from_pretrained("EleutherAI/gpt-neo-1.3B")
tokenizer = GPT2Tokenizer.from_pretrained("EleutherAI/gpt-neo-1.3B")
# Load Sentence-BERT model for semantic similarity
similarity_model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
# Store session context for each user
user_context = {}
# Generate a unique user ID using UUID (Version 4)
def generate_user_id():
return str(uuid.uuid4())
# Token counting utility to prevent exceeding limits
def count_tokens(text):
tokens = tokenizer.encode(text)
return len(tokens)
# Semantic similarity check function
def is_message_related(new_message, previous_message, threshold=0.7):
if not previous_message:
return True # If no previous message, assume it's related
# Calculate embeddings for both messages
embeddings = similarity_model.encode([new_message, previous_message], convert_to_tensor=True)
similarity_score = util.pytorch_cos_sim(embeddings[0], embeddings[1]).item()
return similarity_score >= threshold
# Function to manage conversation context within token limits
def update_context(user_id, new_message, max_tokens=2048):
if user_id not in user_context:
user_context[user_id] = []
context = user_context[user_id]
# Check if the new message is related to the last context message
if context and not is_message_related(new_message, context[-1]):
context.clear() # Clear context if the message is unrelated
context.append(new_message)
# Remove old messages if the token limit is exceeded
total_tokens = count_tokens(' '.join(context))
while total_tokens > max_tokens:
context.pop(0)
total_tokens = count_tokens(' '.join(context))
return ' '.join(context)
def clean_response(prompt, response):
# Find where the prompt ends and extract the remaining part of the response
if response.startswith(prompt):
response = response[len(prompt):].strip()
if not response.endswith('.'):
response = response.rsplit('.', 1)[0]
return response
def format_response(response):
# Replace newline characters and any excess whitespace
response = response.replace("\n", " ").strip()
return ' '.join(response.split()) # Clean up any extra spaces
# GPT-Neo: Text generation based on prompt and token management
def generate_response(user_id, prompt, include_context=True, max_response_length=300):
if include_context:
context = update_context(user_id, prompt)
else:
context = prompt # Ignore previous context for unrelated questions
# tokenizer.pad_token = tokenizer.eos_token
inputs = tokenizer(context, return_tensors="pt",
# padding=True,truncation=True
)
outputs = gpt_neo_model.generate(
inputs["input_ids"],
# attention_mask=inputs['attention_mask'],
max_length=max_response_length
,do_sample=True
# ,temperature=0.7,top_p=0.9,top_k=50,
# ,pad_token_id=tokenizer.eos_token_id
)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
# Clean up the response
cleaned_response = clean_response(prompt, response)
formatted_response = format_response(cleaned_response)
# Update context with the model's response if related
if include_context:
update_context(user_id, formatted_response)
return formatted_response
# EfficientDet: Object detection in images
def detect_objects(image_path):
image = Image.open(image_path)
image_np = np.array(image)
input_tensor = tf.convert_to_tensor(image_np)
input_tensor = input_tensor[tf.newaxis, ...]
detections = efficientdet_model(input_tensor)
detection_classes = detections['detection_classes'][0].numpy().astype(np.int64)
detection_scores = detections['detection_scores'][0].numpy()
detected_objects = [str(detection_classes) for i in range(len(detection_classes)) if detection_scores > 0.5]
return detected_objects
# PyTesseract: Text extraction from images
def extract_text_from_image(image_path):
image = Image.open(image_path)
extracted_text = pytesseract.image_to_string(image)
return extracted_text
# SpeechRecognition: Speech-to-text
def handle_speech_to_text(audio_file):
recognizer = sr.Recognizer()
with sr.AudioFile(audio_file) as source:
audio = recognizer.record(source)
return recognizer.recognize_google(audio)
# Route for text input
@app.post("/text")
async def chat_text( text_input : TextInput):
if not text_input.user_id:
text_input.user_id = generate_user_id()
response = generate_response(text_input.user_id, text_input.input_text, include_context = text_input.is_related)
return {"response": response, "user_id": text_input.user_id}
# Route for speech input
@app.post("/speech")
async def chat_speech(speech_input: SpeechInput):
if not speech_input.user_id:
speech_input.user_id = generate_user_id()
with open("temp_audio.wav", "wb") as buffer:
buffer.write(await speech_input.audio_file.read())
text_from_speech = handle_speech_to_text("temp_audio.wav")
response = generate_response(speech_input.user_id, text_from_speech, include_context=speech_input.is_related)
return {"response": response, "user_id": speech_input.user_id, "transcribed_text": text_from_speech}
# Route for image input
@app.post("/image")
async def chat_image(image_input: ImageInput):
if not image_input.user_id:
image_input.user_id = generate_user_id()
image_extention = image_input.image_file.filename.split('.')[-1]
image_path = f"temp_image.{image_extention}"
with open(image_path, "wb") as buffer:
buffer.write(await image_input.image_file.read())
extracted_text = extract_text_from_image(image_path)
if extracted_text: # If text is detected
if image_input.optional_text:
extracted_text += f" User's comment: {image_input.optional_text}"
response = generate_response(image_input.user_id, extracted_text, include_context=image_input.is_related)
return {"extracted_text": extracted_text, "response": response, "user_id": image_input.user_id}
else: # Object detection
objects = detect_objects(image_path)
if image_input.optional_text:
objects.append(f"User's comment: {image_input.optional_text}")
response = generate_response(image_input.user_id, ' '.join(objects), include_context=image_input.is_related)
return {"objects_detected": objects, "response": response, "user_id": image_input.user_id}
И вот какая ошибка:
Exception in ASGI application
Traceback (most recent call last):
File "C:\Python311\Lib\site-packages\starlette\_exception_handler.py", line 51, in wrapped_app
await app(scope, receive, sender)
File "C:\Python311\Lib\site-packages\starlette\routing.py", line 73, in app
response = await f(request)
^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\fastapi\routing.py", line 346, in app
raise validation_error
fastapi.exceptions.RequestValidationError: [{'type': 'model_attributes_type', 'loc': ('body',), 'msg': 'Input should be a valid dictionary or object to extract
fields from', 'input': b'----------------------------080352262336949113385073\r\nContent-Disposition: form-data; name="image_file"; filename="Funny.webp"\r\nContent-Type: image/webp\r\n\r\nRIFF\x96.\x00\x00WEBPVP8 \x8a.\x00\x00\x10\x14\x01\x9d\x01*e\x02\x80\x02>m6\x97I$"\xa2"!t\x89@\x80\r\x89in\xe1e\xc3\xf7\xa5\xe1\x0c\xe4;\x9a\xdd\'\x97/
Подробнее здесь: https://stackoverflow.com/questions/790 ... on-fastapi
Проблемы в модели эффективного определения и gpt-neo с использованием Python FastAPI [закрыто] ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение