Spaces:
Running
Running
llm project
#1
by
aja061
- opened
- Gemma.py +11 -47
- News.py +4 -4
- __pycache__/Gemma.cpython-312.pyc +0 -0
- __pycache__/News.cpython-312.pyc +0 -0
- app.py +10 -26
- requirements.txt +1 -4
Gemma.py
CHANGED
@@ -1,40 +1,29 @@
|
|
1 |
from transformers import pipeline
|
2 |
from huggingface_hub import login
|
3 |
import spaces
|
|
|
4 |
import json
|
5 |
import os
|
6 |
-
import openai
|
7 |
|
8 |
__export__ = ["GemmaLLM"]
|
9 |
|
10 |
class GemmaLLM:
|
|
|
11 |
def __init__(self):
|
12 |
login(token=os.environ.get("GEMMA_TOKEN"))
|
13 |
-
openai.api_key = os.environ.get("OPENAI_API_KEY")
|
14 |
-
|
15 |
-
# quant_config = quantization_config.BitsAndBytesConfig(
|
16 |
-
# load_in_8bit=True,
|
17 |
-
# llm_int8_threshold=6.0,
|
18 |
-
# llm_int8_has_fp16_weight=False,
|
19 |
-
# )
|
20 |
|
21 |
-
|
22 |
-
# model_id = "google/gemma-3n-E4B-it-litert-preview"
|
23 |
-
# model = AutoModelForCausalLM.from_pretrained(model_id, quantization_config=quant_config)
|
24 |
-
# tokenizer = AutoTokenizer.from_pretrained(model_id)
|
25 |
|
26 |
-
|
27 |
|
28 |
@spaces.GPU
|
29 |
def generate(self, message) -> str:
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
return response.choices[0].message.content
|
34 |
-
# outputs = self.model(message, max_new_tokens=1024)[0]["generated_text"]
|
35 |
-
# return outputs
|
36 |
|
37 |
def _get_summary_message(self, article, num_paragraphs) -> dict:
|
|
|
38 |
summarize = "You are a helpful assistant. Your main task is to summarize articles. You will be given an article that you will generate a summary for. The summary should include all the key points of the article. ONLY RESPOND WITH THE SUMMARY!!!"
|
39 |
|
40 |
summary = f"Summarize the data in the following JSON into {num_paragraphs} paragraph(s) so that it is easy to read and understand:\n"
|
@@ -47,7 +36,8 @@ class GemmaLLM:
|
|
47 |
def get_summary(self, article, num_paragraphs) -> str:
|
48 |
message = self._get_summary_message(article, num_paragraphs)
|
49 |
summary = self.generate(message)
|
50 |
-
|
|
|
51 |
|
52 |
def _get_questions_message(self, summary, num_questions, difficulty) -> dict:
|
53 |
question = f"""
|
@@ -65,33 +55,7 @@ class GemmaLLM:
|
|
65 |
return message
|
66 |
|
67 |
def get_questions(self, summary, num_questions, difficulty) -> dict:
|
68 |
-
# print("Getting questions message...")
|
69 |
message = self._get_questions_message(summary, num_questions, difficulty)
|
70 |
-
# print("Generating questions...")
|
71 |
questions = self.generate(message)
|
72 |
-
# print(questions)
|
73 |
-
questions = questions.strip("```")
|
74 |
-
questions = questions.replace("json\n", "")
|
75 |
-
questions = json.loads(questions)
|
76 |
-
print(questions)
|
77 |
-
return questions
|
78 |
-
|
79 |
-
if __name__ == "__main__":
|
80 |
-
gemma = GemmaLLM()
|
81 |
-
summary = gemma.get_summary('''
|
82 |
-
Iran could reach an agreement similar to the 2015 nuclear deal to end its current conflict with Israel, the Iranian foreign minister said Saturday, according to state media.
|
83 |
-
|
84 |
-
“It is clear the Israelis are against diplomacy and do not want the current crisis to be resolved peacefully,” Abbas Araghchi said, according to state-run news agency IRNA.
|
85 |
-
|
86 |
-
“We are, however, certainly prepared to reach a negotiated solution, similar to the one we reached in 2015,” the foreign minister said, according to IRNA.
|
87 |
-
|
88 |
-
Remember: The 2015 deal, formally known as the Joint Comprehensive Plan of Action, required Iran to limit its nuclear program in return for provisions including relief on sanctions. US President Donald Trump withdrew from the landmark agreement in 2018 and Iran has increasingly grown its nuclear program since then.
|
89 |
-
|
90 |
-
More from the foreign minister: “That agreement was the result of two years of tireless negotiations, and when it was signed, it was welcomed by the entire world,” Araghchi said of the 2015 deal, according to IRNA.
|
91 |
|
92 |
-
|
93 |
-
''', 1)
|
94 |
-
print(summary)
|
95 |
-
questions = gemma.get_questions(summary, 3, "Easy")
|
96 |
-
print(json.dumps(questions, indent=4))
|
97 |
-
|
|
|
1 |
from transformers import pipeline
|
2 |
from huggingface_hub import login
|
3 |
import spaces
|
4 |
+
import torch
|
5 |
import json
|
6 |
import os
|
|
|
7 |
|
8 |
__export__ = ["GemmaLLM"]
|
9 |
|
10 |
class GemmaLLM:
|
11 |
+
|
12 |
def __init__(self):
|
13 |
login(token=os.environ.get("GEMMA_TOKEN"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
model_id = "google/gemma-3-4b-it"
|
|
|
|
|
|
|
16 |
|
17 |
+
self.model = pipeline("text-generation", model=model_id, torch_dtype=torch.bfloat16, device="cuda")
|
18 |
|
19 |
@spaces.GPU
|
20 |
def generate(self, message) -> str:
|
21 |
+
outputs = self.model(message, max_new_tokens=1024)[0]["generated_text"]
|
22 |
+
|
23 |
+
return outputs
|
|
|
|
|
|
|
24 |
|
25 |
def _get_summary_message(self, article, num_paragraphs) -> dict:
|
26 |
+
|
27 |
summarize = "You are a helpful assistant. Your main task is to summarize articles. You will be given an article that you will generate a summary for. The summary should include all the key points of the article. ONLY RESPOND WITH THE SUMMARY!!!"
|
28 |
|
29 |
summary = f"Summarize the data in the following JSON into {num_paragraphs} paragraph(s) so that it is easy to read and understand:\n"
|
|
|
36 |
def get_summary(self, article, num_paragraphs) -> str:
|
37 |
message = self._get_summary_message(article, num_paragraphs)
|
38 |
summary = self.generate(message)
|
39 |
+
|
40 |
+
return summary[2]["content"]
|
41 |
|
42 |
def _get_questions_message(self, summary, num_questions, difficulty) -> dict:
|
43 |
question = f"""
|
|
|
55 |
return message
|
56 |
|
57 |
def get_questions(self, summary, num_questions, difficulty) -> dict:
|
|
|
58 |
message = self._get_questions_message(summary, num_questions, difficulty)
|
|
|
59 |
questions = self.generate(message)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
+
return json.loads(questions[2]["content"].strip("```").replace("json\n", ""))
|
|
|
|
|
|
|
|
|
|
News.py
CHANGED
@@ -8,10 +8,10 @@ class News:
|
|
8 |
__EX_SOURCES__ = ["ABC News", "Bloomberg", "The Hill", "Fox Sports", "Google News", "Newsweek", "Politico"]
|
9 |
__CATEGORIES__ = [
|
10 |
"General",
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
"Technology"
|
16 |
]
|
17 |
|
|
|
8 |
__EX_SOURCES__ = ["ABC News", "Bloomberg", "The Hill", "Fox Sports", "Google News", "Newsweek", "Politico"]
|
9 |
__CATEGORIES__ = [
|
10 |
"General",
|
11 |
+
"Business",
|
12 |
+
"Entertainment",
|
13 |
+
"Health",
|
14 |
+
"Science",
|
15 |
"Technology"
|
16 |
]
|
17 |
|
__pycache__/Gemma.cpython-312.pyc
DELETED
Binary file (6.78 kB)
|
|
__pycache__/News.cpython-312.pyc
DELETED
Binary file (3.01 kB)
|
|
app.py
CHANGED
@@ -3,7 +3,6 @@ import random
|
|
3 |
import gradio as gr
|
4 |
from News import News
|
5 |
from Gemma import GemmaLLM
|
6 |
-
# import matplotlib.pyplot as plt
|
7 |
|
8 |
# %%
|
9 |
class Cooldown:
|
@@ -123,6 +122,9 @@ with gr.Blocks() as demo:
|
|
123 |
|
124 |
options = random.shuffle([answer] + false_answers)
|
125 |
|
|
|
|
|
|
|
126 |
return question, options, answer
|
127 |
|
128 |
def hide_quiz(): ################################### Hide quiz-related components
|
@@ -139,8 +141,12 @@ with gr.Blocks() as demo:
|
|
139 |
quiz = [(question, random.sample(distractors + [answer], 4), answer) for question, distractors, answer in quiz]
|
140 |
questions, options, answers = zip(*quiz) if quiz else ([], [], [])
|
141 |
|
142 |
-
|
143 |
-
|
|
|
|
|
|
|
|
|
144 |
|
145 |
submit = gr.Button("Submit Answers", interactive=bool(answers), visible=True)
|
146 |
|
@@ -263,26 +269,4 @@ with gr.Blocks() as demo:
|
|
263 |
read.click(read_articles, outputs=[heading, num_headlines, category, get, read, evaluation, submit, *quiz])
|
264 |
|
265 |
demo.launch()
|
266 |
-
|
267 |
-
# def visualize_quiz_answers(answers, *quiz_items):
|
268 |
-
# """
|
269 |
-
# Visualization of correct/incorrect answers from the quiz
|
270 |
-
# """
|
271 |
-
# if not answers:
|
272 |
-
# return None
|
273 |
-
|
274 |
-
# correct = 0
|
275 |
-
|
276 |
-
# for user_ans, question in zip(answers, quiz_items):
|
277 |
-
# if user_ans == question["correct_answer"]:
|
278 |
-
# correct += 1
|
279 |
-
|
280 |
-
# incorrect = len(answers) - correct
|
281 |
-
|
282 |
-
# fig, ax = plt.subplots()
|
283 |
-
# ax.bar(["Correct", "Incorrect"], [correct, incorrect])
|
284 |
-
# ax.set_ylabel("Questions")
|
285 |
-
# ax.set_title("Quiz Score Summary")
|
286 |
-
|
287 |
-
# return fig
|
288 |
-
|
|
|
3 |
import gradio as gr
|
4 |
from News import News
|
5 |
from Gemma import GemmaLLM
|
|
|
6 |
|
7 |
# %%
|
8 |
class Cooldown:
|
|
|
122 |
|
123 |
options = random.shuffle([answer] + false_answers)
|
124 |
|
125 |
+
print("Question:", question)
|
126 |
+
print(f"Formatted options: {options}")
|
127 |
+
|
128 |
return question, options, answer
|
129 |
|
130 |
def hide_quiz(): ################################### Hide quiz-related components
|
|
|
141 |
quiz = [(question, random.sample(distractors + [answer], 4), answer) for question, distractors, answer in quiz]
|
142 |
questions, options, answers = zip(*quiz) if quiz else ([], [], [])
|
143 |
|
144 |
+
print("options", len(options))
|
145 |
+
|
146 |
+
quiz = [gr.Radio(label=f"{i + 1}: {questions[i]}", choices=options[i], visible=True) for i in range(len(mcqs))]\
|
147 |
+
+ [gr.Radio(visible=False) for _ in range(10 - len(mcqs))]
|
148 |
+
|
149 |
+
print("quiz", len(quiz))
|
150 |
|
151 |
submit = gr.Button("Submit Answers", interactive=bool(answers), visible=True)
|
152 |
|
|
|
269 |
read.click(read_articles, outputs=[heading, num_headlines, category, get, read, evaluation, submit, *quiz])
|
270 |
|
271 |
demo.launch()
|
272 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
@@ -5,7 +5,4 @@ newspaper3k==0.2.8
|
|
5 |
transformers==4.50.0
|
6 |
lxml_html_clean==0.4.1
|
7 |
accelerate==1.5.2
|
8 |
-
|
9 |
-
spaces
|
10 |
-
matplotlib==3.10.3
|
11 |
-
openai==1.88.0
|
|
|
5 |
transformers==4.50.0
|
6 |
lxml_html_clean==0.4.1
|
7 |
accelerate==1.5.2
|
8 |
+
spaces
|
|
|
|
|
|