Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse filesFiles changed (2)
- inference.py+22-29
- requirements.txt+1
1234567891011121314151617181949 unmodified lines6970717273747512 unmodified lines88899091929394959697989910010110210310410510610710810911011111211311410 unmodified lines125126127128129130131import osimport requestsimport jsonAPI_BASE_URL = os.getenv("API_BASE_URL", "http://localhost:7860")MODEL_NAME = os.getenv("MODEL_NAME", "google/flan-t5-base")HF_TOKEN = os.getenv("HF_TOKEN")# If no HF token, use fallback modeUSE_FALLBACK = not HF_TOKENHF_API_URL = f"https://api-inference.huggingface.co/models/{MODEL_NAME}" if HF_TOKEN else Noneif not USE_FALLBACK:print(f"[INFO] Using HuggingFace API with model: {MODEL_NAME}", flush=True)else:print(f"[INFO] HF_TOKEN not set. Using fallback heuristic agent.", flush=True)SYSTEM_PROMPT = """You are an expert code reviewer AI agent. Your task is to analyze code snippets for bugs and issues.49 unmodified linesdef get_action_from_model(observation):"""Get action from HuggingFace API or use fallback."""if USE_FALLBACK:return get_fallback_action(observation, 0)12 unmodified linesWhat action do you take next? Respond with JSON action."""full_prompt = f"{SYSTEM_PROMPT}\n\n{user_prompt}"headers = {"Authorization": f"Bearer {HF_TOKEN}"}payload = {"inputs": full_prompt,"parameters": {"max_new_tokens": 200,"temperature": 0.1,"do_sample": True}}try:response = requests.post(HF_API_URL, headers=headers, json=payload, timeout=30)response.raise_for_status()result = response.json()if isinstance(result, list) and result:generated_text = result[0].get("generated_text", "")else:generated_text = str(result)# Extract JSON from the responsestart = generated_text.find("{")10 unmodified lines"comment": "Model error"}except Exception as e:print(f"[WARN] Error calling HF API: {e}, using fallback", flush=True)return get_fallback_action(observation, 0)123456789101112131415161718192049 unmodified lines7071727374757612 unmodified lines899091929394959697989910010110210310410510610710 unmodified lines118119120121122123124import osimport requestsimport jsonimport openaiAPI_BASE_URL = os.getenv("API_BASE_URL", "http://localhost:7860")MODEL_NAME = os.getenv("MODEL_NAME", "gpt-3.5-turbo")API_KEY = os.getenv("API_KEY")# If no API key, use fallback modeUSE_FALLBACK = not API_KEYif not USE_FALLBACK:print(f"[INFO] Using proxy API with model: {MODEL_NAME}", flush=True)openai.api_base = API_BASE_URLopenai.api_key = API_KEYelse:print(f"[INFO] API_KEY not set. Using fallback heuristic agent.", flush=True)SYSTEM_PROMPT = """You are an expert code reviewer AI agent. Your task is to analyze code snippets for bugs and issues.49 unmodified linesdef get_action_from_model(observation):"""Get action from proxy API or use fallback."""if USE_FALLBACK:return get_fallback_action(observation, 0)12 unmodified linesWhat action do you take next? Respond with JSON action."""messages = [{"role": "system", "content": SYSTEM_PROMPT},{"role": "user", "content": user_prompt}]try:response = openai.ChatCompletion.create(model=MODEL_NAME,messages=messages,max_tokens=200,temperature=0.1)generated_text = response.choices[0].message.content# Extract JSON from the responsestart = generated_text.find("{")10 unmodified lines"comment": "Model error"}except Exception as e:print(f"[WARN] Error calling proxy API: {e}, using fallback", flush=True)return get_fallback_action(observation, 0)
1 unmodified line23451 unmodified lineuvicornpydanticrequestsstable-baselines3[extra]1 unmodified line234561 unmodified lineuvicornpydanticrequestsopenaistable-baselines3[extra]
inference.py+22-29
requirements.txt+1