sitammeur commited on
Commit
d49d27b
·
verified ·
1 Parent(s): 3329ceb

Update src/app/response.py

Browse files
Files changed (1) hide show
  1. src/app/response.py +72 -67
src/app/response.py CHANGED
@@ -1,67 +1,72 @@
1
- # Necessary imports
2
- import sys
3
- import spaces
4
-
5
- # Local imports
6
- from src.config import (
7
- device,
8
- model_name,
9
- system_prompt,
10
- sampling,
11
- stream,
12
- top_p,
13
- top_k,
14
- temperature,
15
- repetition_penalty,
16
- max_new_tokens,
17
- )
18
- from src.app.model import load_model_and_tokenizer
19
- from src.logger import logging
20
- from src.exception import CustomExceptionHandling
21
-
22
-
23
- # Model and tokenizer
24
- model, tokenizer = load_model_and_tokenizer(model_name, device)
25
-
26
-
27
- @spaces.GPU(duration=120)
28
- def describe_image(image: str, question: str) -> str:
29
- """
30
- Generates an answer to a given question based on the provided image and question.
31
-
32
- Args:
33
- - image (str): The path to the image file.
34
- - question (str): The question text.
35
-
36
- Returns:
37
- str: The generated answer to the question.
38
- """
39
- try:
40
- # Message format for the model
41
- msgs = [{"role": "user", "content": [image, question]}]
42
-
43
- # Generate the answer
44
- answer = model.chat(
45
- image=None,
46
- msgs=msgs,
47
- tokenizer=tokenizer,
48
- sampling=sampling,
49
- stream=stream,
50
- top_p=top_p,
51
- top_k=top_k,
52
- temperature=temperature,
53
- repetition_penalty=repetition_penalty,
54
- max_new_tokens=max_new_tokens,
55
- system_prompt=system_prompt,
56
- )
57
-
58
- # Log the successful generation of the answer
59
- logging.info("Answer generated successfully.")
60
-
61
- # Return the answer
62
- return "".join(answer)
63
-
64
- # Handle exceptions that may occur during answer generation
65
- except Exception as e:
66
- # Custom exception handling
67
- raise CustomExceptionHandling(e, sys) from e
 
 
 
 
 
 
1
+ # Necessary imports
2
+ import sys
3
+ import gradio as gr
4
+ import spaces
5
+
6
+ # Local imports
7
+ from src.config import (
8
+ device,
9
+ model_name,
10
+ system_prompt,
11
+ sampling,
12
+ stream,
13
+ top_p,
14
+ top_k,
15
+ temperature,
16
+ repetition_penalty,
17
+ max_new_tokens,
18
+ )
19
+ from src.app.model import load_model_and_tokenizer
20
+ from src.logger import logging
21
+ from src.exception import CustomExceptionHandling
22
+
23
+
24
+ # Model and tokenizer
25
+ model, tokenizer = load_model_and_tokenizer(model_name, device)
26
+
27
+
28
+ @spaces.GPU(duration=120)
29
+ def describe_image(image: str, question: str) -> str:
30
+ """
31
+ Generates an answer to a given question based on the provided image and question.
32
+
33
+ Args:
34
+ - image (str): The path to the image file.
35
+ - question (str): The question text.
36
+
37
+ Returns:
38
+ str: The generated answer to the question.
39
+ """
40
+ try:
41
+ # Check if video or question is None
42
+ if image is None or question is None:
43
+ raise gr.Error("Image or question cannot be None.")
44
+
45
+ # Message format for the model
46
+ msgs = [{"role": "user", "content": [image, question]}]
47
+
48
+ # Generate the answer
49
+ answer = model.chat(
50
+ image=None,
51
+ msgs=msgs,
52
+ tokenizer=tokenizer,
53
+ sampling=sampling,
54
+ stream=stream,
55
+ top_p=top_p,
56
+ top_k=top_k,
57
+ temperature=temperature,
58
+ repetition_penalty=repetition_penalty,
59
+ max_new_tokens=max_new_tokens,
60
+ system_prompt=system_prompt,
61
+ )
62
+
63
+ # Log the successful generation of the answer
64
+ logging.info("Answer generated successfully.")
65
+
66
+ # Return the answer
67
+ return "".join(answer)
68
+
69
+ # Handle exceptions that may occur during answer generation
70
+ except Exception as e:
71
+ # Custom exception handling
72
+ raise CustomExceptionHandling(e, sys) from e