Rename gpt_model.py to qwen_model.py
Browse files- gpt_model.py +0 -30
- qwen_model.py +52 -0
gpt_model.py
DELETED
|
@@ -1,30 +0,0 @@
|
|
| 1 |
-
import openai
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
def generate_response(retrieved_texts, query, max_tokens=150):
|
| 5 |
-
"""
|
| 6 |
-
Generates a response based on the retrieved texts and query.
|
| 7 |
-
|
| 8 |
-
Args:
|
| 9 |
-
retrieved_texts (list): List of retrieved text strings.
|
| 10 |
-
query (str): Query string.
|
| 11 |
-
max_tokens (int): Maximum number of tokens for the response.
|
| 12 |
-
|
| 13 |
-
Returns:
|
| 14 |
-
str: Generated response.
|
| 15 |
-
"""
|
| 16 |
-
context = "\n".join(retrieved_texts)
|
| 17 |
-
prompt = f"This is the detail about the image: {context}\n\nQuestion: {query}\n\nAnswer:"
|
| 18 |
-
|
| 19 |
-
response = openai.ChatCompletion.create(
|
| 20 |
-
model="gpt-3.5-turbo",
|
| 21 |
-
messages=[
|
| 22 |
-
{"role": "system", "content": "You are a helpful assistant."},
|
| 23 |
-
{"role": "user", "content": prompt}
|
| 24 |
-
],
|
| 25 |
-
max_tokens=max_tokens,
|
| 26 |
-
n=1,
|
| 27 |
-
stop=None,
|
| 28 |
-
temperature=0.5,
|
| 29 |
-
)
|
| 30 |
-
return response.choices[0].message['content']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
qwen_model.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 2 |
+
|
| 3 |
+
# Replace with your target Qwen model on Hugging Face
|
| 4 |
+
MODEL_NAME = "Qwen/Qwen2.5-7B-Instruct-1M"
|
| 5 |
+
|
| 6 |
+
# Initialize tokenizer and model
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
+
MODEL_NAME,
|
| 10 |
+
device_map="auto", # or "cuda", etc. if you want to specify
|
| 11 |
+
trust_remote_code=True
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
# Create pipeline
|
| 15 |
+
qwen_pipeline = pipeline(
|
| 16 |
+
"text-generation",
|
| 17 |
+
model=model,
|
| 18 |
+
tokenizer=tokenizer
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
def generate_response(retrieved_texts, query, max_new_tokens=200):
|
| 22 |
+
"""
|
| 23 |
+
Generates a response based on the retrieved texts and query using Qwen.
|
| 24 |
+
Args:
|
| 25 |
+
retrieved_texts (list): List of retrieved text strings (e.g., from BLIP).
|
| 26 |
+
query (str): The user's question about the image.
|
| 27 |
+
max_new_tokens (int): Maximum tokens to generate for the answer.
|
| 28 |
+
Returns:
|
| 29 |
+
str: The generated answer.
|
| 30 |
+
"""
|
| 31 |
+
# Construct a prompt that includes the image details as context
|
| 32 |
+
context = "\n".join(retrieved_texts)
|
| 33 |
+
prompt = f"This is the detail about the image:\n{context}\n\nQuestion: {query}\nAnswer:"
|
| 34 |
+
|
| 35 |
+
# Generate the text
|
| 36 |
+
result = qwen_pipeline(
|
| 37 |
+
prompt,
|
| 38 |
+
max_new_tokens=max_new_tokens,
|
| 39 |
+
do_sample=True, # or False if you want deterministic output
|
| 40 |
+
temperature=0.7, # tweak as needed
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
# The pipeline returns a list of dicts with key "generated_text"
|
| 44 |
+
full_generation = result[0]["generated_text"]
|
| 45 |
+
|
| 46 |
+
# Optionally parse out the final answer if the model repeats the prompt
|
| 47 |
+
if "Answer:" in full_generation:
|
| 48 |
+
final_answer = full_generation.split("Answer:")[-1].strip()
|
| 49 |
+
else:
|
| 50 |
+
final_answer = full_generation
|
| 51 |
+
|
| 52 |
+
return final_answer
|