Wendgan commited on
Commit
c737f07
·
verified ·
1 Parent(s): 4c6309b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -43
app.py CHANGED
@@ -1,46 +1,12 @@
1
-
2
  import gradio as gr
3
  import google.generativeai as genai
4
- import requests
5
- import json
6
  import os
7
  import time
8
-
9
  genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
10
  gemini_model = genai.GenerativeModel("gemini-2.0-flash")
11
 
12
- class OpenRouter:
13
- def __init__(self, model_name, key, role="user"):
14
- self.model_name = model_name
15
- self.key = key
16
- self.api_url = "https://openrouter.ai/api/v1/chat/completions"
17
- self.role = role
18
-
19
- def get_response(self, prompt):
20
- headers = {
21
- "Authorization": f"Bearer {self.key}",
22
- "Content-Type": "application/json"
23
- }
24
- payload = {
25
- "model": self.model_name,
26
- "messages": [{"role": self.role, "content": prompt}]
27
- }
28
- response = requests.post(self.api_url, headers=headers, data=json.dumps(payload))
29
- if response.status_code == 200:
30
- return response.json()["choices"][0]["message"]["content"]
31
- else:
32
- raise Exception(f"Request failed: {response.status_code} - {response.text}")
33
-
34
- def call_openrouter(llm, prompt, retries=5, delay=5):
35
- for attempt in range(retries):
36
- try:
37
- return llm.get_response(prompt)
38
- except Exception as e:
39
- print(f"Retry {attempt+1}: {e}")
40
- time.sleep(delay)
41
- return "ERROR"
42
-
43
- def prepare_claude_ranking_prompt(question, correct, distractors):
44
  distractor_text = "\n".join([f"Choice {i+1}: {d}" for i, d in enumerate(distractors)])
45
  return f"""You are a helpful assistant evaluating multiple-choice distractors.
46
 
@@ -55,7 +21,6 @@ Your task: Rank the distractors from most to least confusing.
55
  Only respond with one line in this format: 2 > 1 > 3
56
  """
57
 
58
-
59
  def generate_and_rank(question):
60
  gemini_prompt = f"""You are an assistant generating multiple-choice questions.
61
 
@@ -73,6 +38,7 @@ Question: {question}
73
  """
74
  gemini_response = gemini_model.generate_content(gemini_prompt).text.strip()
75
 
 
76
  lines = [line.strip() for line in gemini_response.splitlines() if line.strip()]
77
  correct = next((line.split(":", 1)[1].strip() for line in lines if line.startswith("Correct Answer")), "N/A")
78
  distractors = [line.split(":", 1)[1].strip() for line in lines if line.startswith("Distractor")]
@@ -80,20 +46,19 @@ Question: {question}
80
  if len(distractors) != 3:
81
  return gemini_response, "ERROR (Invalid distractor count)"
82
 
83
- claude_llm = OpenRouter("anthropic/claude-3.5-sonnet-20240612", os.getenv("OPENROUTER_API_KEY"))
84
- claude_prompt = prepare_claude_ranking_prompt(question, correct, distractors)
85
- ranking = call_openrouter(claude_llm, claude_prompt)
86
 
87
- return gemini_response, ranking.strip()
88
 
89
  iface = gr.Interface(
90
  fn=generate_and_rank,
91
  inputs=gr.Textbox(label="Enter your MCQ Question"),
92
  outputs=[
93
  gr.Textbox(label="Generated Answer and Distractors (Gemini)"),
94
- gr.Textbox(label="Claude's Ranking")
95
  ],
96
- title="Confusing Distractor Generator + Claude Ranking"
97
  )
98
 
99
  iface.launch()
 
 
1
  import gradio as gr
2
  import google.generativeai as genai
 
 
3
  import os
4
  import time
5
+ \
6
  genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
7
  gemini_model = genai.GenerativeModel("gemini-2.0-flash")
8
 
9
+ def prepare_ranking_prompt(question, correct, distractors):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  distractor_text = "\n".join([f"Choice {i+1}: {d}" for i, d in enumerate(distractors)])
11
  return f"""You are a helpful assistant evaluating multiple-choice distractors.
12
 
 
21
  Only respond with one line in this format: 2 > 1 > 3
22
  """
23
 
 
24
  def generate_and_rank(question):
25
  gemini_prompt = f"""You are an assistant generating multiple-choice questions.
26
 
 
38
  """
39
  gemini_response = gemini_model.generate_content(gemini_prompt).text.strip()
40
 
41
+ # Parse output
42
  lines = [line.strip() for line in gemini_response.splitlines() if line.strip()]
43
  correct = next((line.split(":", 1)[1].strip() for line in lines if line.startswith("Correct Answer")), "N/A")
44
  distractors = [line.split(":", 1)[1].strip() for line in lines if line.startswith("Distractor")]
 
46
  if len(distractors) != 3:
47
  return gemini_response, "ERROR (Invalid distractor count)"
48
 
49
+ ranking_prompt = prepare_ranking_prompt(question, correct, distractors)
50
+ ranking_response = gemini_model.generate_content(ranking_prompt).text.strip()
 
51
 
52
+ return gemini_response, ranking_response
53
 
54
  iface = gr.Interface(
55
  fn=generate_and_rank,
56
  inputs=gr.Textbox(label="Enter your MCQ Question"),
57
  outputs=[
58
  gr.Textbox(label="Generated Answer and Distractors (Gemini)"),
59
+ gr.Textbox(label="Gemini's Ranking of Distractors")
60
  ],
61
+ title="Confusing Distractor Generator + Ranking (Gemini-only)"
62
  )
63
 
64
  iface.launch()