Spaces:
Runtime error
Runtime error
Create hf_integration.py
Browse files- hf_integration.py +47 -0
hf_integration.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
+
import streamlit as st
|
4 |
+
|
5 |
+
class HuggingFaceIntegration:
|
6 |
+
def __init__(self):
|
7 |
+
self.api_key = st.secrets.get("HF_API_KEY", os.getenv("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"))
|
8 |
+
self.base_url = "https://api-inference.huggingface.co/models"
|
9 |
+
self.model_map = {
|
10 |
+
"english": "google/flan-t5-xxl",
|
11 |
+
"math": "EleutherAI/gpt-neo-2.7B",
|
12 |
+
"science": "google/gemma-7b"
|
13 |
+
}
|
14 |
+
|
15 |
+
def generate_questions(self, subject, difficulty="medium"):
|
16 |
+
try:
|
17 |
+
headers = {"Authorization": f"Bearer {self.api_key}"}
|
18 |
+
prompt = (
|
19 |
+
f"Generate a {difficulty} difficulty {subject} question for IELTS preparation "
|
20 |
+
f"with 4 multiple choice options in this exact format: "
|
21 |
+
"Question: [question]\nA) [option1]\nB) [option2]\nC) [option3]\nD) [option4]"
|
22 |
+
)
|
23 |
+
|
24 |
+
response = requests.post(
|
25 |
+
f"{self.base_url}/{self.model_map[subject]}",
|
26 |
+
headers=headers,
|
27 |
+
json={"inputs": prompt, "parameters": {"max_new_tokens": 200}}
|
28 |
+
)
|
29 |
+
|
30 |
+
return response.json()
|
31 |
+
except Exception as e:
|
32 |
+
return {"error": str(e)}
|
33 |
+
|
34 |
+
def evaluate_writing(self, text):
|
35 |
+
try:
|
36 |
+
headers = {"Authorization": f"Bearer {self.api_key}"}
|
37 |
+
response = requests.post(
|
38 |
+
f"{self.base_url}/openai-community/gpt2",
|
39 |
+
headers=headers,
|
40 |
+
json={
|
41 |
+
"inputs": f"Evaluate this IELTS essay for grammar, vocabulary, coherence, and task achievement:\n\n{text}",
|
42 |
+
"parameters": {"temperature": 0.7, "max_length": 500}
|
43 |
+
}
|
44 |
+
)
|
45 |
+
return response.json()
|
46 |
+
except Exception as e:
|
47 |
+
return {"error": str(e)} # Added missing return statement
|