Spaces:
Running
Running
Update lunacode.py
Browse files- lunacode.py +21 -37
lunacode.py
CHANGED
@@ -5,23 +5,26 @@ from duckduckgo_search import DDGS
|
|
5 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
6 |
import wikipedia
|
7 |
|
8 |
-
# ✅ Load Luna model from
|
9 |
-
model_path = "/
|
10 |
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
11 |
-
model = AutoModelForCausalLM.from_pretrained(model_path, torch_dtype=torch.bfloat16).to("cuda")
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
17 |
return tokenizer.decode(output[0], skip_special_tokens=True)
|
18 |
|
19 |
-
# ✅ Check if query is about coding
|
20 |
def is_code_related(question):
|
21 |
keywords = ["code", "program", "python", "javascript", "build a function", "algorithm", "write a", "html", "css"]
|
22 |
return any(kw in question.lower() for kw in keywords)
|
23 |
|
24 |
-
# ✅ Format prompt for coding
|
25 |
def code_prompt_from_question(question):
|
26 |
return f'''You are a helpful AI programmer. Your task is to generate complete and clean code with explanations.
|
27 |
|
@@ -36,7 +39,6 @@ Requirements:
|
|
36 |
Code:
|
37 |
'''
|
38 |
|
39 |
-
# ✅ Wikipedia fallback first
|
40 |
def try_wikipedia(query):
|
41 |
try:
|
42 |
wikipedia.set_lang("en")
|
@@ -50,7 +52,6 @@ def try_wikipedia(query):
|
|
50 |
except:
|
51 |
return None, None
|
52 |
|
53 |
-
# ✅ DuckDuckGo search
|
54 |
def get_top_webpages(query, max_results=3):
|
55 |
urls = []
|
56 |
with DDGS() as ddgs:
|
@@ -59,7 +60,6 @@ def get_top_webpages(query, max_results=3):
|
|
59 |
urls.append(result["href"])
|
60 |
return urls
|
61 |
|
62 |
-
# ✅ Web scraper
|
63 |
def scrape_first_good_content(urls):
|
64 |
for url in urls:
|
65 |
try:
|
@@ -72,14 +72,12 @@ def scrape_first_good_content(urls):
|
|
72 |
continue
|
73 |
return None, None
|
74 |
|
75 |
-
|
76 |
-
|
77 |
-
# Step 0: Check if it's code related
|
78 |
if is_code_related(user_question):
|
79 |
prompt = code_prompt_from_question(user_question)
|
80 |
-
code_response = luna_generate(prompt, max_tokens=
|
81 |
|
82 |
-
# If code is too short or seems weak, enhance with web context
|
83 |
if len(code_response.strip()) < 100 or "not sure" in code_response.lower():
|
84 |
urls = get_top_webpages(user_question)
|
85 |
context, url_used = scrape_first_good_content(urls)
|
@@ -94,17 +92,15 @@ Generate well-structured code with inline comments and clear function design. On
|
|
94 |
|
95 |
Code:
|
96 |
'''
|
97 |
-
code_response = luna_generate(enhanced_prompt, max_tokens=
|
98 |
return f"Luna (code + web-enhanced):\n{code_response.strip()}\n\n(Source: {url_used})"
|
99 |
-
|
100 |
return f"Luna (code):\n{code_response.strip()}"
|
101 |
|
102 |
-
#
|
103 |
base_prompt = f"User: {user_question}\nLuna:"
|
104 |
-
base_answer = luna_generate(base_prompt)
|
105 |
|
106 |
if any(kw in base_answer.lower() for kw in ["i don't know", "not sure", "as of", "unknown"]) or len(base_answer) < 20:
|
107 |
-
# Step 2: Try Wikipedia
|
108 |
wiki_summary, wiki_url = try_wikipedia(user_question)
|
109 |
if wiki_summary:
|
110 |
prompt = f'''Use the following Wikipedia info to answer:
|
@@ -113,13 +109,11 @@ Context: {wiki_summary}
|
|
113 |
|
114 |
Question: {user_question}
|
115 |
Answer:'''
|
116 |
-
wiki_answer = luna_generate(prompt)
|
117 |
return f"Luna (Wikipedia): {wiki_answer.strip()}\n\n(Source: {wiki_url or 'Wikipedia'})"
|
118 |
|
119 |
-
# Step 3: Try web scraping fallback
|
120 |
urls = get_top_webpages(user_question)
|
121 |
context, url_used = scrape_first_good_content(urls)
|
122 |
-
|
123 |
if context:
|
124 |
prompt = f'''Use the following context to answer:
|
125 |
|
@@ -127,19 +121,9 @@ Context: {context}
|
|
127 |
|
128 |
Question: {user_question}
|
129 |
Answer:'''
|
130 |
-
web_answer = luna_generate(prompt)
|
131 |
return f"Luna (web-enhanced): {web_answer.strip()}\n\n(Source: {url_used})"
|
132 |
|
133 |
return "Luna: I couldn’t find good info online right now."
|
134 |
|
135 |
return f"Luna: {base_answer.strip()}"
|
136 |
-
|
137 |
-
# ✅ Chat loop
|
138 |
-
print("Chat with Luna (type 'exit' to quit):")
|
139 |
-
while True:
|
140 |
-
user_input = input("You: ")
|
141 |
-
if user_input.lower() in ["exit", "quit"]:
|
142 |
-
print("Luna: Bye! See you again.")
|
143 |
-
break
|
144 |
-
response = smart_luna_answer(user_input)
|
145 |
-
print(response)
|
|
|
5 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
6 |
import wikipedia
|
7 |
|
8 |
+
# ✅ Load Luna model from Hugging Face
|
9 |
+
model_path = "cosmosai471/Luna-v2"
|
10 |
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
11 |
+
model = AutoModelForCausalLM.from_pretrained(model_path, torch_dtype=torch.bfloat16).to("cuda" if torch.cuda.is_available() else "cpu")
|
12 |
+
|
13 |
+
def luna_generate(prompt, max_tokens=200, temperature=0.7, top_p=0.95):
|
14 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
15 |
+
output = model.generate(
|
16 |
+
**inputs,
|
17 |
+
max_new_tokens=max_tokens,
|
18 |
+
do_sample=True,
|
19 |
+
temperature=temperature,
|
20 |
+
top_p=top_p
|
21 |
+
)
|
22 |
return tokenizer.decode(output[0], skip_special_tokens=True)
|
23 |
|
|
|
24 |
def is_code_related(question):
|
25 |
keywords = ["code", "program", "python", "javascript", "build a function", "algorithm", "write a", "html", "css"]
|
26 |
return any(kw in question.lower() for kw in keywords)
|
27 |
|
|
|
28 |
def code_prompt_from_question(question):
|
29 |
return f'''You are a helpful AI programmer. Your task is to generate complete and clean code with explanations.
|
30 |
|
|
|
39 |
Code:
|
40 |
'''
|
41 |
|
|
|
42 |
def try_wikipedia(query):
|
43 |
try:
|
44 |
wikipedia.set_lang("en")
|
|
|
52 |
except:
|
53 |
return None, None
|
54 |
|
|
|
55 |
def get_top_webpages(query, max_results=3):
|
56 |
urls = []
|
57 |
with DDGS() as ddgs:
|
|
|
60 |
urls.append(result["href"])
|
61 |
return urls
|
62 |
|
|
|
63 |
def scrape_first_good_content(urls):
|
64 |
for url in urls:
|
65 |
try:
|
|
|
72 |
continue
|
73 |
return None, None
|
74 |
|
75 |
+
def smart_luna_answer(user_question, max_tokens=512, temperature=0.7, top_p=0.95):
|
76 |
+
# Code check
|
|
|
77 |
if is_code_related(user_question):
|
78 |
prompt = code_prompt_from_question(user_question)
|
79 |
+
code_response = luna_generate(prompt, max_tokens=max_tokens, temperature=temperature, top_p=top_p)
|
80 |
|
|
|
81 |
if len(code_response.strip()) < 100 or "not sure" in code_response.lower():
|
82 |
urls = get_top_webpages(user_question)
|
83 |
context, url_used = scrape_first_good_content(urls)
|
|
|
92 |
|
93 |
Code:
|
94 |
'''
|
95 |
+
code_response = luna_generate(enhanced_prompt, max_tokens=max_tokens, temperature=temperature, top_p=top_p)
|
96 |
return f"Luna (code + web-enhanced):\n{code_response.strip()}\n\n(Source: {url_used})"
|
|
|
97 |
return f"Luna (code):\n{code_response.strip()}"
|
98 |
|
99 |
+
# Base Luna response
|
100 |
base_prompt = f"User: {user_question}\nLuna:"
|
101 |
+
base_answer = luna_generate(base_prompt, max_tokens=max_tokens, temperature=temperature, top_p=top_p)
|
102 |
|
103 |
if any(kw in base_answer.lower() for kw in ["i don't know", "not sure", "as of", "unknown"]) or len(base_answer) < 20:
|
|
|
104 |
wiki_summary, wiki_url = try_wikipedia(user_question)
|
105 |
if wiki_summary:
|
106 |
prompt = f'''Use the following Wikipedia info to answer:
|
|
|
109 |
|
110 |
Question: {user_question}
|
111 |
Answer:'''
|
112 |
+
wiki_answer = luna_generate(prompt, max_tokens=max_tokens, temperature=temperature, top_p=top_p)
|
113 |
return f"Luna (Wikipedia): {wiki_answer.strip()}\n\n(Source: {wiki_url or 'Wikipedia'})"
|
114 |
|
|
|
115 |
urls = get_top_webpages(user_question)
|
116 |
context, url_used = scrape_first_good_content(urls)
|
|
|
117 |
if context:
|
118 |
prompt = f'''Use the following context to answer:
|
119 |
|
|
|
121 |
|
122 |
Question: {user_question}
|
123 |
Answer:'''
|
124 |
+
web_answer = luna_generate(prompt, max_tokens=max_tokens, temperature=temperature, top_p=top_p)
|
125 |
return f"Luna (web-enhanced): {web_answer.strip()}\n\n(Source: {url_used})"
|
126 |
|
127 |
return "Luna: I couldn’t find good info online right now."
|
128 |
|
129 |
return f"Luna: {base_answer.strip()}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|