Spaces:
Sleeping
Sleeping
| import requests | |
| import os | |
| GEMINI_API_KEY = os.getenv("GOOGLE_API_KEY") | |
| def generate_answer(context, question): | |
| prompt = f""" | |
| You are answering exam questions. | |
| Use the information in the context to answer the question directly. | |
| Do NOT describe the context. | |
| Do NOT say "the context says". | |
| Give the final answer. | |
| Context: | |
| {context} | |
| Question: | |
| {question} | |
| Answer: | |
| """ | |
| url = f"https://generativelanguage.googleapis.com/v1/models/gemini-2.5-flash:generateContent?key={GEMINI_API_KEY}" | |
| headers = { | |
| "Content-Type": "application/json" | |
| } | |
| data = { | |
| "contents":[ | |
| { | |
| "parts":[ | |
| {"text": prompt} | |
| ] | |
| } | |
| ], | |
| "generationConfig":{ | |
| "temperature":0.3, | |
| "maxOutputTokens":300 | |
| } | |
| } | |
| response = requests.post(url, headers=headers, json=data) | |
| print("Gemini status:", response.status_code) | |
| if response.status_code != 200: | |
| print(response.text) | |
| raise Exception("LLM failed") | |
| result = response.json() | |
| return result["candidates"][0]["content"]["parts"][0]["text"] |